Skip to content
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

NextJS/Authenticator error: "Missing getServerSnapshot, which is required for server-rendered content." #1780

Closed
2 tasks done
nickarocho opened this issue Apr 26, 2022 · 5 comments · Fixed by #1798
Closed
2 tasks done
Labels
React An issue or a feature-request for React platform

Comments

@nickarocho
Copy link

Before creating a new issue, please confirm:

On which framework/platform are you having an issue?

React

Which UI component?

Authenticator

How is your app built?

Create Next App

Please describe your bug.

When scaffolding a new Next.js app via npx create-next-app and trying to use the Authenticator component as documented, the app immediately breaks with a 500 internal server error:

Error: Missing getServerSnapshot, which is required for server-rendered content. Will revert to client rendering.

Screen Shot 2022-04-26 at 3 43 00 PM

What's the expected behaviour?

The Authenticator component should render without any error and the app should build properly with yarn dev.

When the Authenticator is removed from index.js the app renders as expected.

Also, I tried both with/without wrapping _app.js with Authenticator.Provider and the error occurs in both approaches.

I feel like I'm maybe doing something wrong with my setup but can't put my finger on it. The Amplify "Getting Started" docs for Next.js still uses import { AmplifyAuthenticator } from '@aws-amplify/ui-react-v1', but I wanted to work on some use cases for the new Authenticator with Next and haven't found any relevant documentation on either the JS or UI docs site. I will update docs to use the new Authenticator component, but first want to make sure I am setting things up properly myself first.

Thanks for the help!

Help us reproduce the bug!

  1. npx create-next-app@latest next-example
  2. cd next-example && code .
  3. yarn add aws-amplify @aws-amplify/ui-react
  4. amplify add auth
  5. amplify push
  6. amplify init (default options)
  7. Import the Authenticator component into index.js (snippet attached)
  8. yarn dev
  9. The error occurs immediately

Code Snippet

// pages/index.js

import { Authenticator } from "@aws-amplify/ui-react";
import "@aws-amplify/ui-react/styles.css";

export default function Home() {
  return (
    <Authenticator>
      {({ signOut, user }) => (
        <main>
          <h1>Hello {user.username}</h1>
          <button onClick={signOut}>Sign out</button>
        </main>
      )}
    </Authenticator>
  );
}
// pages/_app.js

import { Amplify } from "aws-amplify";
import { Authenticator } from "@aws-amplify/ui-react";

import "../styles/globals.css";
import "@aws-amplify/ui-react/styles.css"; // default theme
import awsExports from "../src/aws-exports";

Amplify.configure({ ...awsExports, ssr: true });

export default function App({ Component, pageProps }) {
  return (
    <Authenticator.Provider>
      <Component {...pageProps} />
    </Authenticator.Provider>
  );
}

Additional information and screenshots

node -v
v16.14.2
{
  "name": "webpack-5-testing",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@aws-amplify/ui-react": "^2.15.5",
    "aws-amplify": "^4.3.20",
    "next": "12.1.5",
    "react": "18.0.0",
    "react-dom": "18.0.0"
  },
  "devDependencies": {
    "eslint": "8.14.0",
    "eslint-config-next": "12.1.5"
  }
}
@nickarocho nickarocho added Authenticator An issue or a feature-request for an Authenticator UI Component Next.js An issue or a feature-request for Next.js support labels Apr 26, 2022
@slaymance slaymance added the pending-triage Issue is pending triage label Apr 27, 2022
@slaymance
Copy link
Contributor

I've narrowed this issue down to React v18. We're working on this issue now. In the meantime, the fix is to downgrade to React 17:

npm install react@17 react-dom@17

or, using yarn:

yarn add react@17 react-dom@17

@slaymance slaymance added being-investigated An issue or a feature-request is being investigated React An issue or a feature-request for React platform and removed pending-triage Issue is pending triage Authenticator An issue or a feature-request for an Authenticator UI Component Next.js An issue or a feature-request for Next.js support labels Apr 27, 2022
@nickarocho
Copy link
Author

Nice, downgrading to React@17 fixed it for me. Thanks @slaymance!

@wlee221
Copy link
Contributor

wlee221 commented Apr 29, 2022

Hey @nickarocho and those who found this issue, we just merged a fix #1798 and will be in the next release. I'll let you know once it's released!

@wlee221 wlee221 added pending-release Code has been merged but pending release and removed being-investigated An issue or a feature-request is being investigated labels Apr 29, 2022
@slaymance
Copy link
Contributor

Fixed in @aws-amplify/ui-react@2.15.8.

@wsasouza
Copy link

wsasouza commented May 2, 2022

Eu reduzi esse problema para React v18. Estamos trabalhando neste problema agora. Enquanto isso, a correção é fazer o downgrade para o React 17:

npm install react@17 react-dom@17

ou, usando fios:

yarn add react@17 react-dom@17

this solved it for me, thanks!

@slaymance slaymance removed the pending-release Code has been merged but pending release label May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React An issue or a feature-request for React platform
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants