- 
                Notifications
    
You must be signed in to change notification settings  - Fork 94
 
feat: Next.js SSR Improvements #877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
944a484    to
    e278f9f      
    Compare
  
    
          size-limit report 📦
  | 
    
b306f85    to
    ad89bbe      
    Compare
  
    8231792    to
    7c23d35      
    Compare
  
    ebadbed    to
    3f9b519      
    Compare
  
    Update the function and add types Add more info about the proposal Account for pages directory in the function implementation Update types and clean things up Add comments Add nextjs init function Add the jose library Fix refreshing and add example Fix build and token config Fix payload processing Load config fron env variables Add a redirect location during refresh Refresh token from middleware Move everything in the library Rename files and update function singatures Use the correct refresh path Use normal responses instead of nextresponse Use normal request instead of nextrequest Cleanup implementation Revoke session inside the middleware Code review fixes Add tests for getSSRSession Add tests for middleware
3f9b519    to
    0797deb      
    Compare
  
    578dc07    to
    22365e9      
    Compare
  
    22365e9    to
    5bfbbd1      
    Compare
  
    | 
           Nextjs is adding an  With that in mind I think we can drop the   | 
    
40a1c7c    to
    fde0f49      
    Compare
  
    175f1d3    to
    7a5630a      
    Compare
  
    a380890    to
    52ce968      
    Compare
  
    52ce968    to
    3eaa59d      
    Compare
  
            
          
                lib/ts/nextjs/ssr.ts
              
                Outdated
          
        
      | export const getServerComponentSessionWithoutClaims = | ||
| SuperTokensNextjsSSRAPIWrapper.getServerComponentSessionWithoutClaims; | ||
| export const getServerActionSessionWithoutClaims = SuperTokensNextjsSSRAPIWrapper.getServerActionSessionWithoutClaims; | ||
| export const getServerSidePropsSessionWithoutClaims = | ||
| SuperTokensNextjsSSRAPIWrapper.getServerSidePropsSessionWithoutClaims; | ||
| export const ensureSessionAndCall = SuperTokensNextjsSSRAPIWrapper.ensureSessionAndCall; | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| export const getServerComponentSessionWithoutClaims = | |
| SuperTokensNextjsSSRAPIWrapper.getServerComponentSessionWithoutClaims; | |
| export const getServerActionSessionWithoutClaims = SuperTokensNextjsSSRAPIWrapper.getServerActionSessionWithoutClaims; | |
| export const getServerSidePropsSessionWithoutClaims = | |
| SuperTokensNextjsSSRAPIWrapper.getServerSidePropsSessionWithoutClaims; | |
| export const ensureSessionAndCall = SuperTokensNextjsSSRAPIWrapper.ensureSessionAndCall; | |
| export const getServerComponentSessionWithoutClaimValidation = | |
| SuperTokensNextjsSSRAPIWrapper.getServerComponentSessionWithoutClaimValidation; | |
| export const getServerActionSessionWithoutClaimValidation = SuperTokensNextjsSSRAPIWrapper.getServerActionSessionWithoutClaimValidation; | |
| export const getServerSidePropsSessionWithoutClaimValidation = | |
| SuperTokensNextjsSSRAPIWrapper.getServerSidePropsSessionWithoutClaimValidation; | |
| export const ensureSessionAndCall = SuperTokensNextjsSSRAPIWrapper.ensureSessionAndCall; | 
        
          
                lib/ts/nextjs/ssr.ts
              
                Outdated
          
        
      | cookies: CookiesStore | ||
| static async getServerActionSessionWithoutClaims( | ||
| cookies: CookiesStore, | ||
| requireAuth: boolean = false | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| requireAuth: boolean = false | |
| requireAuth: boolean = true | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it should be on getServerComponentSessionWithoutClaims
        
          
                lib/ts/nextjs/ssr.ts
              
                Outdated
          
        
      | if (requireAuth) { | ||
| const redirectPath = cookies.get(CURRENT_PATH_COOKIE_NAME)?.value || "/"; | ||
| const authPagePath = getAuthPagePath(redirectPath); | ||
| return redirect(authPagePath); | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work here?
        
          
                lib/ts/nextjs/ssr.ts
              
                Outdated
          
        
      | * @returns The session context value or directly redirects the user to either the login page or the refresh API | ||
| **/ | ||
| static async getServerComponentSession(cookies: CookiesStore): Promise<LoadedSessionContext> { | ||
| static async getServerComponentSessionWithoutClaims(cookies: CookiesStore): Promise<SSRSessionContext> { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be the one taking the requireAuth param
Summary of change
The following PR improves SSR support in Next.js applications.
To get the session data in the context of server side rendering you now have to call
getSSRSessioneither inside aServer Component, or ingetServerSideProps(for older next applications).There's two parts to this change:
Fetching the session during SSR
The logic for this is kept in the
ssr.tsfile. The function reads the active tokens and has 3 possible outcomes:Refreshing a session
The
getSSRSessionfunction redirects to arefreshendpoint that we do not expose from our existing API.Hence, this change adds logic inside the next.js middleware to handle such a request. The custom middleware calls the backend API using a fetch and updates the response with the new tokens.
This part is now kept mostly in the
middleware.tsfile, in the react library. It's there just to make it easy to review during this stage. It should be moved in the node SDK package as a separate, Nextjs specific API.Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos!)
Documentation changes
(If relevant, please create a PR in our docs repo, or create a checklist here highlighting the necessary changes)
Checklist for important updates
frontendDriverInterfaceSupported.jsonfile has been updated (if needed)package.jsonpackage-lock.jsonlib/ts/version.tsnpm run build-prettygit tag) in the formatvX.Y.Z, and then find the latest branch (git branch --all) whoseX.Yis greater than the latest released tag.someFunc: function () {..}).size-limitsection ofpackage.jsonwith the size limit set to the current size rounded up.rollup.config.mjslib/ts/types.tslib/ts/recipe/multifactorauth/types.tsRemaining TODOs for this PR