Amplify-js: 1.2.3 version is incompatible with Server Side Rendering using Next.js

Created on 26 Oct 2019  路  9Comments  路  Source: aws-amplify/amplify-js

Describe the bug
1.2.3 update causes incompatibility issues with Next.js.

Error output:
ReferenceError: window is not defined

To Reproduce
Steps to reproduce the behavior:

  1. Create a new Next.js project
npm install -g create-next-app
create-next-app new
cd new
  1. Install aws-amplify
    npm install aws-amplify
  2. Import amplify in the newly created project
    add import Amplify from 'aws-amplify' into /pages/index.js.

  3. Start dev server
    npm run dev

Expected behavior
aws-amplify should be SSR compatible.

Screenshots
a complete dump of the error output is provided below:

$ npm run dev

> create-next-example-app@ dev /Users/Ted/Desktop/test/new
> next

[ wait ]  starting the development server ...
[ info ]  waiting on http://localhost:3000 ...
The static directory has been deprecated in favor of the public directory. https://err.sh/zeit/next.js/static-dir-deprecated
[ ready ] compiled successfully - ready on http://localhost:3000
[ wait ]  compiling ...
[ event ] client pings, but there's no entry for page: /
[ ready ] compiled successfully - ready on http://localhost:3000
[ event ] build page: /
[ wait ]  compiling ...
[ ready ] compiled successfully - ready on http://localhost:3000
ReferenceError: window is not defined
    at Object.<anonymous> (/Users/Ted/Desktop/test/new/node_modules/aws-amplify/dist/aws-amplify.js:10:4)
    at Module._compile (module.js:662:30)
    at Object.Module._extensions..js (module.js:673:10)
    at Module.load (module.js:575:32)
    at tryModuleLoad (module.js:515:12)
    at Function.Module._load (module.js:507:3)
    at Module.require (module.js:606:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/Ted/Desktop/test/new/node_modules/aws-amplify/index.js:6:19)
    at Module._compile (module.js:662:30)
    at Object.Module._extensions..js (module.js:673:10)
    at Module.load (module.js:575:32)
    at tryModuleLoad (module.js:515:12)
    at Function.Module._load (module.js:507:3)
    at Module.require (module.js:606:17)
    at require (internal/module.js:11:18)
[ event ] build page: /next/dist/pages/_error

Desktop (please complete the following information):

  • OS: macOS X 10.15
  • Browser: chrome
  • Version: 77.0.3865.120

Amplify context

  • Framework: Next.js
  • Versions of Amplify you are using: 1.2.3

Additional context
N.A.

Sample code
N.A.

SSR bug

Most helpful comment

@hello2all @anarerdene We did do a release yesterday, version 1.2.4 and the Pull request that I referenced resolves this issue. I do want to state that Amplify doesn't officially support SSR at this time. Please add a reaction or a +1 for SSR as this is an open feature request for that. I am going to resolve this issue, please feel free to reopen this issue if were are still experiencing this after installing the latest version released.

All 9 comments

+1

This is same as #4243 and has been fixed in unstable release. We will release it soon.

@anarerdene, can you confirm in your node_modules directory, what version of aws-amplify you are using, stable 1.2.3 or 1.2.4-unstable.5

hi @Amplifiyer ,

im using stable 1.2.3 and 1.2.4-unstable.5. But still window not working error.

@hello2all @anarerdene We did do a release yesterday, version 1.2.4 and the Pull request that I referenced resolves this issue. I do want to state that Amplify doesn't officially support SSR at this time. Please add a reaction or a +1 for SSR as this is an open feature request for that. I am going to resolve this issue, please feel free to reopen this issue if were are still experiencing this after installing the latest version released.

Amplify needs SSR. I've been trying to deploy a NextJS app to Amplify and I will probably have to abandon this idea soon enough if it's an impossible concept

@samstr, what issues are you facing while deploying the NextJS app? Can you create another issue with a detailed repro steps?

@Amplifiyer I've tried to do the same, the deploy is successful but when I got to the app it says there is an error and I've been redirected too many times.

Here are my build deets

version: 0.1
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- npm run build
artifacts:
baseDirectory: .next
files:
- '/'
cache:
paths:
- node_modules/
/

npm run build runs "next build"

Was this page helpful?
0 / 5 - 0 ratings