question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

500 error page throws client-side exception if it has a getStaticProps and there is a middleware.ts file

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
Binaries:
  Node: 16.13.2
  npm: 8.7.0
  Yarn: 1.22.15
  pnpm: 6.11.0
Relevant packages:
  next: 12.2.3-canary.12
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

n/a

How are you deploying your application? (if relevant)

using next start

Describe the Bug

In production, if my app has a 500.tsx file that has getStaticProps exported and it also has a middleware.ts file, and the URL visited contains a query parameter, the app will throw a client-side exception after rehydrating, but before any ErrorBoundary defined in _app could catch it.

The 500 page is first correctly rendered serverside, then is replaced by this message in the browser:

Application error: a client-side exception has occurred (see the browser console for more information).

A number of superfluous requests seem to be being made:

Screenshot 2022-07-18 at 14 12 21

The issue only exists on 12.2 and above, it works correctly if I downgrade to 12.1.6 and convert back to the pre-release middleware usage. When the url did not have a query parameter, this issue was resolved in 12.2.4.

Expected Behavior

I expect to see the static error page render successfully.

Link to reproduction

https://codesandbox.io/s/gifted-alex-0hlens?a=b

To Reproduce

  • create a pages/500.ts file that has a getStaticProps
  • create a middleware.ts file, it does not need to do anything (but also tested with always returning a NextResponse.next())
  • throw in pages/index.ts when visiting the page with a query parameter

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
LuudJanssencommented, Sep 19, 2022

v12.3.0 does not seem to have fixed the issue for us. The issue @clarkeverdel mentioned is still happening:

  • Using middleware
  • Using getStaticProps
  • Without query parameters
  • Client side error: Error: Failed to load static props
  • Both in dev and build mode
2reactions
abbathawcommented, Oct 25, 2022

Same issue. deleting the middleware file for test purposes would render the custom 500 error page as expected without throwing errors on client side.

Issue is still happening on next 12.3.1, the custom 500 page is still showing the error on the browser console after what it seems a few duplicated GET requests to the page that was throwing the error.

Screenshot 2022-10-25 at 13 27 48

My middleware file is just a rewrite for a one of the paths

export async function middleware(req: NextRequest) {
  const { pathname } = req.nextUrl;
  if (pathname == '/') {
    const url = req.nextUrl.clone();
    url.pathname = '/lists';
    return NextResponse.rewrite(url);
  }
  return NextResponse.next();
}

And my 500.tsx get static props is just for grabbing the locales

export const getStaticProps = async ({ locale }: { locale: string }) => ({
  props: {
    ...(await serverSideTranslations(locale, ['common'])),
  },
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Error Handling - Next.js
Having a 500 page in your application does not show specific errors to the app user. You can also use 404 page to...
Read more >
How to throw a 500 error from getStaticProps - Stack Overflow
I fetch data from a headless CMS to populate the 404 error page. If I don't get a reasonable response from the CMS...
Read more >
Testing and error handling patterns in Next.js - LogRocket Blog
Check out these different ways to run tests and handle errors in Next.js apps using Cypress, a JavaScript-based testing framework.
Read more >
next-i18next - npm
TypeScript icon, indicating that this package has built-in type ... to include their translation content as JSON files and don't have to ...
Read more >
GraphQL error handling to the max with Typescript, codegen ...
Contrary to REST, GraphQL does not use Status Code to differentiate a successful result from an exception. In REST, one can send a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found