Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js Error Getting 404 When Fetching JS Resources After Refresh

Tags:

next.js

I'm running a Next.js dev server. When I go to page #1, then click a link to page #2, it works great. (Just to clarify, I'm not using the as prop of Next's <Link> tag, just the regular old href.)

However, if I then refresh page #2, the page itself loads, as do all the images ... but all of the Javascript files fail with a 404:

http://localhost:3000/page2/_next/static/chunks/main.js?ts=1612664646023 net::ERR_ABORTED 404 (Not Found) 2localhost/:196 GET

http://localhost:3000/page2/_next/static/development/_buildManifest.js?ts=1612664646023 net::ERR_ABORTED 404 (Not Found) 2localhost/:1 GET

http://localhost:3000/page2/_next/static/chunks/webpack.js?ts=1612664646023 net::ERR_ABORTED 404 (Not Found) 7localhost/:196 GET

http://localhost:3000/page2/_next/static/chunks/main.js?ts=1612664646023 net::ERR_ABORTED 404 (Not Found)

Does anyone know what I can do to fix this?

like image 504
machineghost Avatar asked Feb 07 '21 02:02

machineghost


People also ask

How do I handle 404 in next JS?

js comes with a default 404 page. It is a static page, and you can use getStaticProps for data fetching inside this page. However, the default 404 page doesn't have a header, footer, or other links. So, it is recommended to use the normal layout of your Next.

What is Javascript error 404?

HTML Error Appears. When an HTTP 404 appears on your screen, it means that although the server is reachable, the specific page you are looking for is not. The web page is either broken, or it no longer exists. The 404 error code can appear in any browser, regardless of the type of browser you are using.


2 Answers

Ran into similar problem with inherited code, search a million times this was the top result. My answer for all other frustrated dev out there, check your 'Link' and your 'router.push()' especially if you are using a custom router. The Link and router.push() should be pointing to the server url. You can pass 'as' to mask the url for the browser.

like image 64
Señor Méndez Avatar answered Nov 09 '22 23:11

Señor Méndez


In our case we fixed it by skipping the next.js router for handling the URL:

  <Link
    onClick={() => {
      const url = '/path/to/url';
      Router.events.emit("routeChangeStart", url);
      window.location.href = url;
    }}
  >
like image 39
Nicu Surdu Avatar answered Nov 10 '22 01:11

Nicu Surdu