next.js: Can not navigate in development (Router hangs after some time)
Bug report
Describe the bug
Either Link or Router stops working when doing client side routing. It seems that HMR may be interrupting the transition between pages. It occurs most frequently if the app is left idle or in the background for a bit of time (although I have experienced it happening whilst clicking around without it being idle)
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Clone this repo (https://github.com/malimccalla/next-routing-issue)
- Install dependencies
npm install - Run the server
npm run dev - Visit all pages by clicking the links
- Go make a coffee (leave page idle for ~2mins)
- Attempt to visit all pages by clicking the links
- Certain links do not navigate to their respective page š
Expected behavior
I expect to be able to visit all the individual pages
Actual behaviour
Page does not navigate to certain routes. Refreshing the page will fix it
Screenshots
Both the āaboutā and ācontactā links do not work (notice the HMR log on the first attempt of each route). After a page refresh they work as expected. If I leave the app idle for roughly 2mins again it will start over with the issue

System information
- mac Mojave 10.14
- Chrome
- next.js v7.0.2
Additional context
With the console open you can see that the Router event routeChangeStart fires but routeChangeComplete never does.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 133
- Comments: 55 (14 by maintainers)
Commits related to this issue
- Temporary fix for https://github.com/zeit/next.js/issues/5598 — committed to BioMediaLab/synapse by andrewdavidkay 8 years ago
- feat(PageHeader): Allow passing of backlink component - Default to Link - In Skills this doesn't work directly, which I believe is related to https://github.com/zeit/next.js/issues/5598 ... so I exp... — committed to sprucelabsai/workspace.sprucebot-skills-kit by gcpantazis 8 years ago
- Move RichUI CSS to page.js cause of navigation bug https://github.com/zeit/next.js/issues/5598 — committed to flytaly/RssDT by flytaly 7 years ago
- hack css handling solves https://github.com/zeit/next.js/issues/5598 — committed to matiaskantele/rsvp by deleted user 7 years ago
- Fix bug where link was not working when target page imported CSS (?!?) - Links on index page were mysteriously not working - Tracked problem to the fact that target page was importing CSS - Solution:... — committed to splitgraph/splitgraph.com by milesrichardson 7 years ago
- Fix bug where link was not working when target page imported CSS (?!?) - Links on index page were mysteriously not working - Tracked problem to the fact that target page was importing CSS - Solution:... — committed to splitgraph/splitgraph.com by milesrichardson 7 years ago
- Fixed bug with Links using https://github.com/vercel/next.js/issues/5598#issuecomment-439678578 — committed to jacquelinemarchal/schefs by chriswang030 5 years ago
- Fixed bug with Links using https://github.com/vercel/next.js/issues/5598#issuecomment-439678578 — committed to jacquelinemarchal/schefs by chriswang030 5 years ago
Iām investigating this issue right now.
@roytsang Instead of saying āsame issueā use š on the issue. Your manual
+1doesnāt end up being taken into account when sorting on šSolved in #5994
I have this in both dev and production. Doesnāt matter if the page is inactive, etc. I can have a fresh load and it wonāt work. In dev, the server console
> Building page: /contactWAIT Compling...success client complied in 462msbut the page never renders in the client. At the same time, on the client console:[HMR] bundle rebuildingmessage, then the server console gives:Disposing of inactive page(s): /, /contactIf I type mysite.com/contact into the browser, it will load just fine. Iāve tried the work around in
next.config.jsofmaxInactiveAgeandpagesBufferLengthto no avail.[was hoping it was just in dev as many were saying]
EDIT: I did have an
import 'node-mod/dist/theme.cssin a component. I moved that import to_app.jsand thatLinkis working.@malimccalla It seems my issue is different from yours, however the navigation behavior is the same. When the page I navigate to has a css import
import 'react-alice-carousel/lib/alice-carousel.css';the navigation behaviour is the same as you reported. Seems to be an issue with ā@zeit/next-cssā
Seems to be reported already in #5291 , can be āsolvedā temporarily by importing and empty css file in _app.js
I can get this to reliably reproduce across dev, production and static-export modes and none of the
onDemandEntriesor empty css file workarounds helped.The only way to fix the problem was to remove
import 'highlight.js/styles/color-brewer.css';from the Body component on my blog post pages.Any
<Link>instance that pointed to a blog post page would fail to load buthttp://localhost:3000/_next/static/development/pages/post.jsstill would get dropped on the page on<Link>click. Comment out that css import,<Link>s to blog posts work perf. Uncomment and all<Link>s fail to load blog posts.Hitting any of those blog post page routes directly work fine, the bug is only related to client-side <Link>-based routing. This happened after upgrading from version 6 to version 8.
I hit initially this with
@zeit/next-cssbut I can replicate this issue in the same way after swapping in@zeit/next-sassand importing a .scss file. For now Iām just gunna load these styles usingstyled-componentsdrop@zeit/next-css. After reviewing https://www.npmjs.com/package/@zeit/next-css and https://www.npmjs.com/package/@zeit/next-sass, it seems like those Usage examples would break <Link> š¤I did a bit of digging and it seems it happens due to the pages being disposed of. As soon as this log appears the disposed pages can no longer be navigated to
I have a bit of time tonight to do a deeper dive and can hopefully get a PR open to fix it. @timneutkens Thanks for the initial insight, let me know if you think theres any part of that file I should be looking at in particular
I did have a look at trying to fix this but there is a lot going on in the related files and unfortunately I couldnāt quite get to the bottom of it. For anyone else having this issue, as a current workaround Iām using this configuration in my
next.config.jsto extend the duration before pages get disposed.@jon64digital I wrote a complete writeup of this issue in #5994, you could work around it by upping the disposal time limit.
I have the same issue. It only started today after I started using Head from next/head in my _app to apply a title and favicon.
Iāve now replied about 3 times with the exact same message. Please provide a reproduction.
I havenāt gotten around to it yet, but I will soon. I just thought more information on when the problem arose for me might be useful to someone looking into a fix.
Update: Iāve been doing some testing this morning and hereās what Iāve come across.
Problems started yesterday, the only new thing I had implemented is the Head from next. I realized I changed the head in _document, used to load in styles, to Head. I also added Head to _app.js to set title and favicon. Removing Head in _app.js did not fix this for me. Removing Head in both _app and _document also didnāt resolve the issue. I tried one last thing, I reverted Head in _document back to head and left the Head in _app to set title and favicon, and now the issue is gone.
If for any reason the issue returns Iāll update here again. I hope this info might help lead to a fix.
Update 2: The problem persists, though it arises less frequently then it did. I have noticed that when it does happen, I see the script tag that loads the js for that page is duplicated. For example, first time I load page āxā it adds <script src="/_next/static/development/pages/x.js"></script> to the body. Now this script tag should only be added to the body once, but on the times it fails to load I see the script tag added a 2nd, and then if I click the link again it is added a 3rd time, and so on. These additional script tags are only added a few times while staying on the same page and clicking the link, however if I navigate to another page and back to where the link was, I can click it and add several more script tags.
Update 3: Problem happens after a while on a single link. Tested loading my home page and not clicking anything, after a couple minutes the Elements tab in Chrome dev tools flashes, Iām assuming a HMR, and ALL Links were broken. I hope all of this information wasnāt just a waste of time for you guys. The issue doesnāt seem to be happening in production, so I guess Iāll just ignore it for the moment.
In production mode its ok, after Iāve changed ENV to production everything is going ok
Iām also seeing this in a production environment, so Iām not sure HMR is the sole responsible for the issue
Before trying the workaround and other stuff, Try this
Fortunately, this worked for me, just try this before trying something elseā¦
The good news is that I was able to pinpoint the error. The bad news is that the issue with my project was no different to what other people, in this thread, have reported.
So if the issue is with CSS and disposal limit, and it supposedly has been solved, I must be doing something wrong. Is this where you put these?
module.exports = withCSS({ onDemandEntries: { maxInactiveAge: 1000 * 60 * 60 } })Reproduction: https://github.com/dydokamil/next-js-router-bug
I have the same issue with the latest version (8.1.0).
The same happened to me in development (production works as expected), and the issue is when we link to pages (or have components inside) that import sass files.
My
next.config.jsfile:So then, one of the components in depth have this import:
When I comment the import, the link starts working. So it seems to be an issue with
withSassplugin.@dydokamil It is known bug: https://github.com/zeit/next-plugins/issues/282 You can āsolveā it importing an empty CSS file.
@timneutkens iām using
8.1.1-canary.24and the bug still exists for me. Any idea?Edit: found out whats happening. Not sure why thoā¦: I had an index.tsx and index.less file. I was using cssModules in my netxt.config.js file (using withLess). When loading a
/subpageand having a link that references/(which is importing styles like this:import css from './index.less';) then the script will be dynamically loaded (http://localhost:3000/_next/static/development/pages/index.js) but it is not rendered. In fact the url doesnāt change at all, itās like stuck⦠Removing the less file import resolved the issue.Wat do?
Edit2: Seems Iām running into https://github.com/zeit/next-plugins/issues/282
Is it possible to let us know what the problem was and how we might be able to work around the issue if we canāt upgrade right now? For instance, is it to do with css loading? Is it something that can be changed in config?
Is someone looking into this? I also have this issue but not necessarily to all the routes. Itās random which route/routes become unresponsive but they all do eventually.