Skip to content

[Bug]: NextAuth basic middleware causes 500 internal error on all page .json file requests #1679

Closed
@Joroze

Description

@Joroze

Summary

I'm deploying a basic NextJS project using NextAuth basic middleware as documented here: https://next-auth.js.org/configuration/nextjs#basic-usage

When loading any page route, I get the page's .json internal server errors as a response multiple times.

  • I added function logs of the errors I'm seeing below.
  • Side note: I'm deploying from a NX mono repo - but I doubt this is why the issue occurs.

Screen Shot 2022-10-12 at 11 06 55 AM

Steps to reproduce

https://next-auth.js.org/configuration/nextjs#basic-usage
middleware.tsx

import { withAuth } from 'next-auth/middleware';

export default withAuth({
  pages: {
    signIn: '/auth/signin',
  },
});

Relevant dependency versions:

    "next": "12.3.1",
    "next-auth": "4.12.3",
    "@netlify/plugin-nextjs": "4.25.0",
  1. Use NextAuth middleware (latest version of NextAuth)
  2. Deploy Netlify site
  3. Load a page and you'll see the .json files are returning 500 internal errors

A link to a reproduction repository

No response

Next Runtime version

4.25.0

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
[build.environment]
  NPM_FLAGS = "--legacy-peer-deps"
[[plugins]]
package = "@netlify/plugin-nextjs"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
//@ts-check

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { withNx } = require('@nrwl/next/plugins/with-nx');

/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
  reactStrictMode: true,
  nx: {
    // Set this to true if you would like to to use SVGR
    // See: https://github.com/gregberge/svgr
    svgr: false,
  },
};

module.exports = withNx(nextConfig);

Builds logs (or link to your logs)

Build logs
10:51:44 AM: Build ready to start
10:51:46 AM: build-image version: 9289b698a503f132b265bd089ab40ce1d6f9e65e (focal)
10:51:46 AM: build-image tag: v4.12.0
10:51:46 AM: buildbot version: c87074660cd9a4568c0ac759bc083987e94204b3
10:51:46 AM: Fetching cached dependencies
10:51:46 AM: Starting to download cache of 684.8MB
10:51:50 AM: Finished downloading cache in 4.661007194s
10:51:50 AM: Starting to extract cache
10:51:58 AM: Finished extracting cache in 7.262633705s
10:51:58 AM: Finished fetching cache in 12.04428989s
10:51:58 AM: Starting to prepare the repo for build
10:51:58 AM: Preparing Git Reference refs/heads/main
10:51:59 AM: Parsing package.json dependencies
10:52:00 AM: Starting build script
10:52:00 AM: Installing dependencies
10:52:00 AM: Python version set to 2.7
10:52:00 AM: Started restoring cached node version
10:52:01 AM: Finished restoring cached node version
10:52:01 AM: Downloading and installing node v16.18.0...
10:52:01 AM: Downloading https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.xz...
10:52:02 AM: Computing checksum with sha256sum
10:52:02 AM: Checksums matched!
10:52:04 AM: Now using node v16.18.0 (npm v8.19.2)
10:52:04 AM: Started restoring cached build plugins
10:52:04 AM: Finished restoring cached build plugins
10:52:04 AM: Attempting ruby version 2.7.2, read from environment
10:52:05 AM: Using ruby version 2.7.2
10:52:05 AM: Using PHP version 8.0
10:52:05 AM: No npm workspaces detected
10:52:05 AM: Started restoring cached node modules
10:52:05 AM: Finished restoring cached node modules
10:52:06 AM: Installing NPM modules using NPM version 8.19.2
10:52:06 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:06 AM: npm WARN config location in the cache, and they are managed by
10:52:06 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:06 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:06 AM: npm WARN config location in the cache, and they are managed by
10:52:06 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:16 AM: up to date, audited 2319 packages in 10s
10:52:16 AM: 239 packages are looking for funding
10:52:16 AM:   run `npm fund` for details
10:52:17 AM: 23 vulnerabilities (3 moderate, 18 high, 2 critical)
10:52:17 AM: To address issues that do not require attention, run:
10:52:17 AM:   npm audit fix
10:52:17 AM: To address all issues (including breaking changes), run:
10:52:17 AM:   npm audit fix --force
10:52:17 AM: Run `npm audit` for details.
10:52:17 AM: NPM modules installed
10:52:17 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:17 AM: npm WARN config location in the cache, and they are managed by
10:52:17 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:17 AM: Started restoring cached go cache
10:52:17 AM: Finished restoring cached go cache
10:52:17 AM: Installing Go version 1.17 (requested 1.17)
10:52:21 AM: unset GOOS;
10:52:21 AM: unset GOARCH;
10:52:21 AM: export GOROOT='/opt/buildhome/.gimme/versions/go1.17.linux.amd64';
10:52:21 AM: export PATH="/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}";
10:52:21 AM: go version >&2;
10:52:21 AM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.17.linux.amd64.env"
10:52:21 AM: go version go1.17 linux/amd64
10:52:21 AM: Installing missing commands
10:52:21 AM: Verify run directory
10:52:23 AM: ​
10:52:23 AM: ────────────────────────────────────────────────────────────────
10:52:23 AM:   Netlify Build                                                 
10:52:23 AM: ────────────────────────────────────────────────────────────────
10:52:23 AM: ​
10:52:23 AM: ❯ Version
10:52:23 AM:   @netlify/build 27.20.1
10:52:23 AM: ​
10:52:23 AM: ❯ Flags
10:52:23 AM:   baseRelDir: true
10:52:23 AM:   buildId: 6346d480cda4aa0008891767
10:52:23 AM:   deployId: 6346d480cda4aa0008891769
10:52:23 AM: ​
10:52:23 AM: ❯ Current directory
10:52:23 AM:   /opt/build/repo
10:52:23 AM: ​
10:52:23 AM: ❯ Config file
10:52:23 AM:   /opt/build/repo/netlify.toml
10:52:23 AM: ​
10:52:23 AM: ❯ Context
10:52:23 AM:   production
10:52:23 AM: ​
10:52:23 AM: ❯ Using Next.js Runtime - v4.25.0
10:52:24 AM: ​
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM: ​
10:52:24 AM: Next.js cache restored.
10:52:24 AM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
10:52:24 AM: ​
10:52:24 AM: (@netlify/plugin-nextjs onPreBuild completed in 201ms)
10:52:24 AM: ​
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM:   2. Build command from Netlify app                             
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM: ​
10:52:24 AM: $ nx run admin:build
10:52:24 AM:  >  NX   Running target build for project admin and 1 task(s) it depends on
10:52:24 AM: > nx run admin:codegen
10:52:25 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:25 AM: npm WARN config location in the cache, and they are managed by
10:52:25 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:25 AM: > th@0.0.0 graphql:codegen
10:52:25 AM: > graphql-codegen --config libs/api/src/lib/graphql/codegen.yml && barrelsby --delete --singleQuotes --directory libs/api/src/lib/graphql --exclude 'test.ts|types.generated.ts'
10:52:26 AM: [STARTED] Parse Configuration
10:52:26 AM: [SUCCESS] Parse Configuration
10:52:26 AM: [STARTED] Generate outputs
10:52:26 AM: [STARTED] Generate to libs/api/src/lib/graphql/types.generated.ts
10:52:26 AM: [STARTED] Load GraphQL schemas
10:52:26 AM: [SUCCESS] Load GraphQL schemas
10:52:26 AM: [STARTED] Load GraphQL documents
10:52:26 AM: [SUCCESS] Load GraphQL documents
10:52:26 AM: [STARTED] Generate
10:52:27 AM: [SUCCESS] Generate
10:52:27 AM: [SUCCESS] Generate to libs/api/src/lib/graphql/types.generated.ts
10:52:27 AM: [STARTED] Generate to libs/api/src/lib/graphql/
10:52:27 AM: [STARTED] Load GraphQL schemas
10:52:27 AM: [SUCCESS] Load GraphQL schemas
10:52:27 AM: [STARTED] Load GraphQL documents
10:52:27 AM: [SUCCESS] Load GraphQL documents
10:52:27 AM: [STARTED] Generate
10:52:27 AM: [SUCCESS] Generate
10:52:27 AM: [SUCCESS] Generate to libs/api/src/lib/graphql/
10:52:27 AM: [SUCCESS] Generate outputs
10:52:27 AM: > nx run admin:build:production
10:52:29 AM: info  - Skipping linting
10:52:29 AM: info  - Checking validity of types...
10:52:35 AM: info  - Creating an optimized production build...
10:52:47 AM: info  - Compiled successfully
10:52:47 AM: info  - Collecting page data...
10:52:49 AM: info  - Generating static pages (0/5)
10:52:50 AM: info  - Generating static pages (1/5)
10:52:50 AM: info  - Generating static pages (2/5)
10:52:50 AM: info  - Generating static pages (3/5)
10:52:50 AM: info  - Generating static pages (5/5)
10:52:51 AM: info  - Finalizing page optimization...
10:52:51 AM: Route (pages)                              Size     First Load JS
10:52:51 AM: ┌ ○ / (856 ms)                             5.1 kB          170 kB
10:52:51 AM: ├   /_app                                  0 B             164 kB
10:52:51 AM: ├ ○ /404                                   193 B           165 kB
10:52:51 AM: ├ λ /api/auth/[...nextauth]                0 B             164 kB
10:52:51 AM: ├ ○ /auth/signin (937 ms)                  14 kB           182 kB
10:52:51 AM: └ ○ /oms/patients (822 ms)                 34.8 kB         203 kB
10:52:51 AM: + First Load JS shared by all              164 kB
10:52:51 AM:   ├ chunks/framework-c02f198d58d34563.js   45.3 kB
10:52:51 AM:   ├ chunks/main-7f51edf094ce88c6.js        30.9 kB
10:52:51 AM:   ├ chunks/pages/_app-d4dc28059f74008a.js  87.2 kB
10:52:51 AM:   └ chunks/webpack-df4cf1c8d23aa877.js     1.03 kB
10:52:51 AM: ƒ Middleware                               53.6 kB
10:52:51 AM: λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
10:52:51 AM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
10:52:51 AM:  >  NX   Successfully ran target build for project admin
10:52:51 AM: ​
10:52:51 AM: (build.command completed in 26.9s)
10:52:51 AM: ​
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM:   3. @netlify/plugin-nextjs (onBuild event)                     
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM: ​
10:52:51 AM: NextAuth package detected, NEXTAUTH_URL environment variable set by user in Netlify configuration to https://th-admin.netlify.app/api/auth
10:52:51 AM: Patching /opt/build/repo/node_modules/next/dist/server/base-server.js
10:52:51 AM: Done
10:52:51 AM: Patching /opt/build/repo/node_modules/next/dist/server/next-server.js
10:52:51 AM: Done
10:52:51 AM: Moving static page files to serve from CDN...
10:52:51 AM: Moved 3 files
10:52:51 AM: Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.
10:52:51 AM: ✨ Deploying middleware and functions to Netlify Edge Functions ✨
10:52:51 AM: This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
10:52:51 AM: Netlify configuration property "redirects" value changed to [
10:52:51 AM:   { from: '/_next/static/*', to: '/static/:splat', status: 200 },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/image*',
10:52:51 AM:     query: { url: ':url', w: ':width', q: ':quality' },
10:52:51 AM:     to: '/_ipx/w_:width,q_:quality/:url',
10:52:51 AM:     status: 301
10:52:51 AM:   },
10:52:51 AM:   { from: '/_ipx/*', to: '/.netlify/builders/_ipx', status: 200 },
10:52:51 AM:   { from: '/cache/*', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/server/*', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/serverless/*', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/trace', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/traces', to: '/404.html', status: 404, force: true },
10:52:51 AM:   {
10:52:51 AM:     from: '/routes-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/build-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/prerender-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/react-loadable-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   { from: '/BUILD_ID', to: '/404.html', status: 404, force: true },
10:52:51 AM:   {
10:52:51 AM:     from: '/api',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/api/*',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/*',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     conditions: { Cookie: [Array] },
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/data/yF0HNNoJhSpsl_DFYgQLO/index.json',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/data/yF0HNNoJhSpsl_DFYgQLO/auth/signin.json',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/auth/signin',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/data/yF0HNNoJhSpsl_DFYgQLO/oms/patients.json',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/oms/patients',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/*',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200
10:52:51 AM:   }
10:52:51 AM: ].
10:52:51 AM: ​
10:52:51 AM: (@netlify/plugin-nextjs onBuild completed in 248ms)
10:52:51 AM: ​
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM:   4. Functions bundling                                         
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM: ​
10:52:51 AM: Packaging Functions from .netlify/functions-internal directory:
10:52:51 AM:  - ___netlify-handler/___netlify-handler.js
10:52:51 AM:  - ___netlify-odb-handler/___netlify-odb-handler.js
10:52:51 AM:  - _ipx/_ipx.js
10:52:51 AM: ​
10:53:09 AM: ​
10:53:09 AM: (Functions bundling completed in 17.8s)
10:53:09 AM: ​
10:53:09 AM: ────────────────────────────────────────────────────────────────
10:53:09 AM:   5. Edge Functions bundling                                    
10:53:09 AM: ────────────────────────────────────────────────────────────────
10:53:09 AM: ​
10:53:09 AM: Packaging Edge Functions from .netlify/edge-functions directory:
10:53:09 AM:  - ipx
10:53:09 AM:  - next_middleware
10:53:10 AM: ​
10:53:10 AM: (Edge Functions bundling completed in 1.3s)
10:53:10 AM: ​
10:53:10 AM: ────────────────────────────────────────────────────────────────
10:53:10 AM:   6. @netlify/plugin-nextjs (onPostBuild event)                 
10:53:10 AM: ────────────────────────────────────────────────────────────────
10:53:10 AM: ​
10:53:11 AM: Next.js cache saved.
10:53:11 AM: ​
10:53:11 AM: (@netlify/plugin-nextjs onPostBuild completed in 378ms)
10:53:11 AM: ​
10:53:11 AM: ────────────────────────────────────────────────────────────────
10:53:11 AM:   7. Deploy site                                                
10:53:11 AM: ────────────────────────────────────────────────────────────────
10:53:11 AM: ​
10:53:11 AM: Starting to deploy site from 'dist/apps/admin/.next'
10:53:23 AM: Creating deploy tree 
10:53:23 AM: Creating deploy upload records
10:53:23 AM: 23 new files to upload
10:53:23 AM: 2 new functions to upload
10:53:30 AM: Site deploy was successfully initiated
10:53:30 AM: ​
10:53:30 AM: (Deploy site completed in 19.4s)
10:53:30 AM: ​
10:53:30 AM: ────────────────────────────────────────────────────────────────
10:53:30 AM:   Netlify Build Complete                                        
10:53:30 AM: ────────────────────────────────────────────────────────────────
10:53:30 AM: ​
10:53:30 AM: (Netlify Build completed in 1m 7.5s)
10:53:30 AM: Caching artifacts
10:53:30 AM: Started saving node modules
10:53:30 AM: Finished saving node modules
10:53:30 AM: Started saving build plugins
10:53:30 AM: Finished saving build plugins
10:53:30 AM: Started saving pip cache
10:53:30 AM: Finished saving pip cache
10:53:30 AM: Started saving emacs cask dependencies
10:53:30 AM: Finished saving emacs cask dependencies
10:53:30 AM: Started saving maven dependencies
10:53:30 AM: Finished saving maven dependencies
10:53:30 AM: Started saving boot dependencies
10:53:30 AM: Finished saving boot dependencies
10:53:30 AM: Started saving rust rustup cache
10:53:30 AM: Finished saving rust rustup cache
10:53:30 AM: Started saving go dependencies
10:53:30 AM: Finished saving go dependencies
10:53:31 AM: Starting post processing
10:53:31 AM: Post processing - HTML
10:53:31 AM: Post processing - header rules
10:53:32 AM: Build script success
10:53:32 AM: Post processing - redirect rules
10:53:32 AM: Post processing done
10:53:37 AM: Site is live ✨
10:54:42 AM: Uploading Cache of size 747.9MB
10:54:47 AM: Finished processing build request in 3m1.40115615s

Function logs

Function logs
Oct 12, 10:58:46 AM: f810df9e ERROR  Invariant: failed to load static page {
  "page": "/",
  "url": "/_next/data/yF0HNNoJhSpsl_DFYgQLO/index.json",
  "initUrl": "http://6346d480cda4aa0008891769--th-admin.netlify.app:80/_next/data/yF0HNNoJhSpsl_DFYgQLO/index.json"
}Oct 12, 10:58:46 AM: f810df9e ERROR  MissingStaticPage [Error]: Failed to load static file for page: / Failed to download https://6346d480cda4aa0008891769--th-admin.netlify.app/index.html: 307 Temporary Redirect
    at /var/task/node_modules/next/dist/server/require.js:55:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.loadComponents (/var/task/node_modules/next/dist/server/load-components.js:61:26)
    at async NextNodeServer.findPageComponents (/var/task/node_modules/next/dist/server/next-server.js:555:36)
    at async NextNodeServer.renderPageComponent (/var/task/node_modules/next/dist/server/base-server.js:891:24)
    at async NextNodeServer.renderToResponse (/var/task/node_modules/next/dist/server/base-server.js:920:32)
    at async NextNodeServer.pipe (/var/task/node_modules/next/dist/server/base-server.js:369:25)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:743:21)
    at async Router.checkFsRoutes (/var/task/node_modules/next/dist/server/router.js:169:35)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:265:40)

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions