next.js: Custom error page being reloaded every ~3 seconds

Bug report

I am currently using nextjs alongside express I have a custom error page (_error.js) and each time the error page is triggered it reloads itself every 3 seconds or so for some strange reason

I believe this is a bug with nextjs as i dont have anything special in my express app outside of next which would suggest it should reload every 3 seconds.

Describe the bug

I have a custom error page (_error.js) and each time the error page is triggered it reloads itself every 3 seconds or so for some strange reason

To Reproduce

Create an express app which has a router and route like:

const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });

const router = new express.Router({
  strict: true,
  caseSensitive: true,
});

// for simplicity use a basic route here /unknown/
router.get(`/unknown/`, (req, res) =>
  nextApp.render(req, res, '/error', {}),
);

then in my error page i have something like:

function Error({ statusCode, message }) {
  return <div>Error Message: {message}</div>;
}

Error.getInitialProps = ({ res, err }) => {
  const statusCode = res ? res.statusCode : err ? err.statusCode : 404;

  return {
    statusCode,
    message: err ? err.message : 'Oops...',
  };
};

export default Error;

As soon as i goto /unknown/ route - the error page displays but next seems to reload it every ~3 seconds for some weird reason.

Expected behavior

I expect it to goto the custom error page and not reload every 3 seconds. This is bad because I dont want to overload the server with never ending requests from users to error pages (they should only get a loaded error page once)

System information

  • OS: macOS
  • Browser: any
  • Version of Next.js: ^9.1.7

NEXT-1372

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 32
  • Comments: 38 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I got the same on Next 13.4.6

This issue has already been triaged, we’re going to work on it, there’s other issues that have a higher priority though. Please don’t post further comments if there is no additional information being provided. That includes not posting “version x still has this”, it’s not helpful, the issue is still open so you can assume it’s not fixed in version x. Every comment pings everyone on the thread and takes time away from us investigating/fixing this issue. These will be hidden automatically.

Same problem here. I’m not using a custom express server, but I noticed the _error page seems to trigger getInitialProps in the _app.js file over and over while in development.

Example repo: https://github.com/mattcarlotta/nextjs-ssr-kit

Steps to reproduce:

  1. Clone the repo and install dependencies.
  2. Add a console.log("triggered _app.js getInitialProps") to the _app.js file in its getInitialProps.
  3. Go to http://localhost:3000/error
  4. Check console for repetitive getInitialProps calls.

Encountering the same issue in Next 13.4.4. Very annoying tbh. Anything on this yet?

This one can be reproduced, can we remove the “Can’t Reproduce” tag please?

Experiencing this issue on 13.4.5, here are the packages I have installed if that helps:

  • @headlessui/react v1.7.15
  • @heroicons/react v2.0.18
  • autoprefixer v10.4.14
  • eslint v8.42.0
  • eslint-config-next v13.4.5
  • next v13.4.5
  • postcss v8.4.24
  • react v18.2.0
  • react-dom v18.2.0
  • react-hot-toast v2.4.1
  • tailwindcss v3.3.2

Also running into this issue (Next.js 12.2.5).

Next 13.4.7 which is the latest today (2023-06-22) still has the same issue.

Allow me to be this week’s commenter to report I am experiencing this behavior on 13.4.7

also encountering the same in Next.js v13.4.7

Just ran into this issue after upgrading to 13.4.7 from 13.1.1 and migrating to app dir. Never had this happen before with pages

Hi @rlvk-tc I use p5 on my not-found.js page, I thought it might come from that, but I also created a component just with a Hello and I have the same result, it reloads constantly. However, I also tried to build as you indicated in your previous message and the reloading stopped, strange!!!

I’m experiencing the same issue in Next 13.1.1 however it only happens during development. If I do npm run build && npm start it only renders once. I’m not using turbo.

FYI this bug is still happening with v4.2.8

A workaround I’ve found is to set up a custom 404 page so that if a user navigates to a page that doesn’t exist, then they will land on the 404 page instead of the server-side 500 page. That said, if the server itself has an error, then the problem will still occur in development.