Next.js Application on Netlify - 500 Errors on Non-Existing Routes in Production

I am experiencing issues with my Next.js application hosted on Netlify, where non-existing routes are leading to 500 errors in production, while in development, they correctly redirect to a custom 404 page. Additionally, there are discrepancies in behavior between the development and production environments.

site name: https://dev--techt-integrity.netlify.app/

Details

  1. Non-Existing Routes Issue: When accessing a non-existing route like /fee in production, the server returns a 500 error instead of redirecting to the custom 404 page. This behavior differs from the development environment, where the 404 page is displayed as expected.
  2. Sentry Integration: There are also issues with Sentry error tracking. For example, POST requests to Sentry’s /monitoring?o=<ID>&p=<ID> endpoint return a 500 error in production but work normally in development.
  3. Development vs. Production Discrepancy: The application behaves differently in the Netlify production environment compared to the local development environment. This includes differences in routing and error handling.

Troubleshooting Steps Taken:

  • Checked for environment variable discrepancies between DEV and PROD.
  • Reviewed Netlify redirects and rewrites in netlify.toml and _redirects.
  • Verified Sentry configuration for production.
  • Examined Netlify build logs for errors or warnings.

Packages

    "@sentry/nextjs": "^7.81.1",
    "next": "13.5.6",
    "react": "18.2.0",

SSR Function log

  • Non-exsiting route
Nov 29, 08:28:55 AM: 41bd0dd4 ERROR  Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/html-context'
Require stack:
- /var/task/node_modules/next/dist/pages/_document.js
- /var/task/node_modules/next/document.js
- /var/task/node_modules/@mantine/next/cjs/create-get-initial-props.js
- /var/task/node_modules/@mantine/next/cjs/index.js
- /var/task/.next/server/pages/_document.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/pages/_document.js:43:35)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/pages/_document.js',
    '/var/task/node_modules/next/document.js',
    '/var/task/node_modules/@mantine/next/cjs/create-get-initial-props.js',
    '/var/task/node_modules/@mantine/next/cjs/index.js',
    '/var/task/.next/server/pages/_document.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 29, 08:28:55 AM: 41bd0dd4 INFO   [GET] /987213 (SSR)
Nov 29, 08:28:55 AM: 41bd0dd4 Duration: 74.64 ms	Memory Usage: 106 MB	
  • Sentry /monitoring route
Nov 29, 08:30:28 AM: 6e207d79 ERROR  Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/html-context'
Require stack:
- /var/task/node_modules/next/dist/pages/_document.js
- /var/task/node_modules/next/document.js
- /var/task/node_modules/@mantine/next/cjs/create-get-initial-props.js
- /var/task/node_modules/@mantine/next/cjs/index.js
- /var/task/.next/server/pages/_document.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/pages/_document.js:43:35)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/pages/_document.js',
    '/var/task/node_modules/next/document.js',
    '/var/task/node_modules/@mantine/next/cjs/create-get-initial-props.js',
    '/var/task/node_modules/@mantine/next/cjs/index.js',
    '/var/task/.next/server/pages/_document.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 29, 08:30:28 AM: 6e207d79 INFO   [POST] /monitoring?o=4505073877385216&p=4505683682394112 (SSR)
Nov 29, 08:30:28 AM: 6e207d79 Duration: 10.70 ms	Memory Usage: 106 MB	

    Docs
    Pricing
    Support
    Blog
    Changelog
    Terms

Any insights or guidance on these issues would be greatly appreciated.

Related to:

Fix by downgrade Next.js to 13.4.19

1 Like

thanks for sharing this with the community.