500 Internal Server Error on some Next.js routes

Some of the routes in our Next.js applications throw a 500 Internal Server Error even when they exist and work in development mode.
Link - Playground | Crio.Do | Build internship-grade projects in a real developer environment and accelerate your full stack or backend developer career with a job-ready project portfolio. Enroll and start for free.

If I visit the above link via some other link like our homepage or so (client side routing), then it opens up fine. But when I hit the URL directly, it fails.

Points to note:

  1. We don’t do SSG or SSR on any of our pages. It’s a plain vanilla next.js application.
  2. We don’t have any custom netlify functions.
  3. netlify.toml
[build]
  command = "yarn run build"
  publish = ".next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

yarn run build is basically next build
4. next.config.js

const path = require('path');
const withOptimizedImages = require('next-optimized-images');
const { withSentryConfig } = require('@sentry/nextjs');
const isProd = process.env.NODE_ENV === 'production';
const { withPlaiceholder } = require('@plaiceholder/next');

const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});

const moduleExports = withBundleAnalyzer(
  withOptimizedImages(
    withPlaiceholder({
      images: {
        disableStaticImages: true,
      },
      trailingSlash: true,
      basePath: "learn/v2",
      assetPrefix:  "learn/v2",
      publicRuntimeConfig: {
        staticFolder:  "learn/v2",
      },
      optimizeImagesInDev: true,
      sassOptions: {
        includePaths: [path.join(__dirname, 'styles')],
      },
      experimental: {
        removeConsole: isProd ? true : false,
      },
    }),
  ),
);

const SentryWebpackPluginOptions = {

  silent: true, // Suppresses all logs
};

module.exports = withSentryConfig(moduleExports, SentryWebpackPluginOptions);
  1. Deploy logs
────────────────────────────────────────────────────────────────
  Netlify Build                                                 
────────────────────────────────────────────────────────────────

❯ Version
  @netlify/build 26.2.3

❯ Flags
  {}

❯ Current directory
  stage-crio

❯ Config file
  stage-crio/netlify.toml

❯ Context
  production

❯ Loading plugins
   - @netlify/plugin-nextjs@4.2.3 from netlify.toml and package.json

❯ Outdated plugins
   - @netlify/plugin-nextjs@4.2.3: latest version is 4.2.5
     Migration guide: https://ntl.fyi/next-plugin-migration
     To upgrade this plugin, please update its version in "package.json"

────────────────────────────────────────────────────────────────
  1. @netlify/plugin-nextjs (onPreBuild event)                  
────────────────────────────────────────────────────────────────

Next.js cache restored.
Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.

(@netlify/plugin-nextjs onPreBuild completed in 1.6s)

────────────────────────────────────────────────────────────────
  2. build.command from netlify.toml                            
────────────────────────────────────────────────────────────────

$ yarn run build
yarn run v1.22.17
$ yarn startup
$ node startup.js
$ next build
warn  - You have enabled experimental feature(s).
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use them at your own risk.

info  - Checking validity of types  

info  - Collecting page data  
info  - Generating static pages (26/26)
info  - Finalizing page optimization  

Page                                                                        Size     First Load JS
β”Œ β—‹ /                                                                       794 B           379 kB
β”œ   /_app                                                                   0 B             378 kB
β”œ β—‹ /404 (387 ms)                                                           5.19 kB         386 kB
β”œ β—‹ /activities                                                             75.4 kB         650 kB
β”œ β—‹ /impersonate/[id]                                                       462 B           441 kB
β”œ β—‹ /login                                                                  5.4 kB          386 kB
β”œ β—‹ /magic/[id] (385 ms)                                                    46.8 kB         641 kB
β”œ β—‹ /me/[meId]                                                              2.39 kB         766 kB                                            252 B
β”œ β—‹ /PSDS/playground (637 ms)                                               12.1 kB                                                12.3 kB         465 kB
+ First Load JS shared by all                                               378 kB
  β”œ chunks/framework-eeba1b405d308ec4.js                                    42.3 kB
  β”œ chunks/main-53e0cba5d7770f8c.js                                         26.8 kB
  β”œ chunks/pages/_app-3b8b53267a8228ec.js                                   306 kB
  β”œ chunks/webpack-4f6506a321871006.js                                      2.75 kB
  β”” css/77e7be7280b21608.css                                                11.6 kB

β—‹  (Static)  automatically rendered as static HTML (uses no initial props)

✨  Done in 141.64s.

(build.command completed in 2m 21.8s)

────────────────────────────────────────────────────────────────
  3. @netlify/plugin-nextjs (onBuild event)                     
────────────────────────────────────────────────────────────────

Patching undefined
Moving static page files to serve from CDN...
Moved 5 files
Netlify configuration property "redirects" value changed to [
  {
    from: '/learn/v2/_next/static/*',
    to: '/static/:splat',
    status: 200
  },
  {
    from: '/_next/image/*',
    query: { url: ':url', w: ':width', q: ':quality' },
    to: '/learn/v2/_ipx/w_:width,q_:quality/:url',
    status: 301
  },
  {
    from: '/learn/v2/_ipx/*',
    to: '/.netlify/builders/_ipx',
    status: 200
  },
  {
    from: '/_next/static/image/*',
    to: '/static/image/:splat',
    status: 200
  },
  {
    from: '/learn/v2/cache/*',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/server/*',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/serverless/*',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/traces',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/routes-manifest.json',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/build-manifest.json',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/prerender-manifest.json',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/react-loadable-manifest.json',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/BUILD_ID',
    to: '/404.html',
    status: 404,
    force: true
  },
  {
    from: '/learn/v2/api',
    to: '/.netlify/functions/___netlify-handler',
    status: 200
  },
  {
    from: '/learn/v2/api/*',
    to: '/.netlify/functions/___netlify-handler',
    status: 200
  },
  {
    from: '/learn/v2/favicon.ico',
    to: '/learn/v2/favicon.ico',
    conditions: { Cookie: [Array] },
    status: 200
  },
  {
    from: '/learn/v2/login-banner-desktop.png',
    to: '/learn/v2/login-banner-desktop.png',
    conditions: { Cookie: [Array] },
    status: 200
  },
  {
    from: '/learn/v2/login-banner-mobile.png',
    to: '/learn/v2/login-banner-mobile.png',
    conditions: { Cookie: [Array] },
    status: 200
  },
  {
    from: '/learn/v2/robots.txt',
    to: '/learn/v2/robots.txt',
    conditions: { Cookie: [Array] },
    status: 200
  },
  {
    from: '/learn/v2/vercel.svg',
    to: '/learn/v2/vercel.svg',
    conditions: { Cookie: [Array] },
    status: 200
  },
  {
    from: '/learn/v2/images/doodle-white.png',
    to: '/learn/v2/images/doodle-white.png',
    conditions: { Cookie: [Array] },
    status: 200
  },
  {
    from: '/learn/v2/*',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    conditions: { Cookie: [Array] },
    force: true
  },
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/index.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/404.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/404',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/PSDS/playground.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/PSDS/playground',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/activities.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/activities',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/login.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/login',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/not-found.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/not-found',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/impersonate/:id.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/impersonate/:id',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/_next/data/CBfHwQcumAqF7u6SOF_9j/magic/:id.json',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },
  {
    from: '/learn/v2/magic/:id',
    to: '/.netlify/functions/___netlify-handler',
    status: 200,
    force: false
  },

  {
    from: '/learn/v2/*',
    to: '/.netlify/functions/___netlify-handler',
    status: 200
  }
].

(@netlify/plugin-nextjs onBuild completed in 777ms)

────────────────────────────────────────────────────────────────
  4. Functions bundling                                         
────────────────────────────────────────────────────────────────

Packaging Functions from .netlify/functions-internal directory:
 - ___netlify-handler/___netlify-handler.js
 - ___netlify-odb-handler/___netlify-odb-handler.js
 - _ipx/_ipx.js
No Functions were found in netlify/functions directory


(Functions bundling completed in 1m 31.7s)

────────────────────────────────────────────────────────────────
  5. @netlify/plugin-nextjs (onPostBuild event)                 
────────────────────────────────────────────────────────────────

Next.js cache saved.

(@netlify/plugin-nextjs onPostBuild completed in 1s)

────────────────────────────────────────────────────────────────
  Netlify Build Complete                                        
────────────────────────────────────────────────────────────────

(Netlify Build completed in 3m 59s)

I’m afraid sharing repo might not be feasible, but I am open to share any info that you may require in order to help us resolve :slight_smile:

1 Like

Hi @rahul-crio,

It appears you’re building using the CLI. So there’s not a lot of checking we can do on our end.

However, since it’s a static site, I’d assume you should use next export instead of next build. Would you try that?