next.js: Generated static files html files have wrong assets paths
Bug report
Describe the bug
I’m tried to generate a static site with Next js v9.0.2 and got assets linking by default to /_next/static/something/something.js. But by default build and export create a folder .next.
I think its related to: https://github.com/zeit/next.js/blob/4bcf6aabe7af477a8e58893f03622d84047abf2b/packages/next/pages/_document.tsx#L410
To Reproduce
- Create a static page
- Build it
- Open the html page generated
- See the source of every script inside the html page
Expected behavior
Generated path should begin with ./next/
Screenshots
System information
- OS: macOS
- Version of Next.js: 9.0.2
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 10
- Comments: 34 (13 by maintainers)
Thanks @cargallo 👍 Was trying to serve static exported files through an Electron app and was wondering why all the styles were missing. Post build-export find/replace will do for now.
[EDIT] After poking around a bit, there’s an attribute in the next.config.js called assetPrefix that lets you prefix a path to the static resources in the index.html (see https://nextjs.org/docs/api-reference/next.config.js/cdn-support-with-asset-prefix + https://github.com/vercel/next.js/pull/9916)
I’ve set my config to the following and it did the trick:
next.config.js
Generated output: index.html
@cargallo if you haven’t done so already, you might want to use this instead of npm: replace-in-files-cli method 👍
DO NOT USE THIS SOLUTION ANY MORE !!! USE THE SOLUTION BELOW INSTEAD https://github.com/vercel/next.js/issues/8158#issuecomment-687707467
To automate the steps commented above:
yarn add --dev replace-in-files-cliThen, add this two scripts in your package.json
"build-static": "next build && next export && npm run build-static-repair-index","build-static-repair-index": "replace-in-files --string \"/_next/static\" --replacement \"./_next/static\" out/index.html"Then just call
npm run build-staticExplanation: The build-static-repair-index script will replace /_next/static with ./_next/static in the index.html file inside out folder wich is generated when you export your entire site to plain html wich can run standalone without any server.
Hope this helps. Reggards,
Hey folks. The issue being experienced by most of the comments above appears to be because once a
next exportis done and you have theoutfolder, if you serve theoutfolder under another folder, the urls to the assets will no longer resolve.Imagine a basic project folder set up, something like:
Then start an http server with the
My Projectfolder as the root. Theoutfolder from the browser url is then something like:http://localhost:8000/out/The asset path Next.js uses ASSUMES that the out folder will be the root of your web server host.
Modifying the assetPrefix to “.” works for the above layout, but when you try to nest your pages any further it fails again.
e.g.
Now when you access
http://localhost:8000/out/login/the page is 404.Modifying the
trailingSlashoption solves this so:With a next.config.js of
But then, when you access
http://localhost:8000/out/login/, the assets all try to load fromhttp://localhost:8000/out/login/_next/***The _next folder is assumed to be
./_next, relative to the current page URL path (/out/login), rather than the root of theoutfolder (/out).Furthermore, client-side routing will work and render the pages but refreshing will 404 because the navigated URL changes from
http://localhost:8000/out/login/tohttp://localhost:8000/login/when you click on a link client-side./logindoesn’t exist at the project folder root, only inside/out, as/out/login.All of this is down to the relative vs absolute paths being used or not.
The question becomes, how do you configure Next.js to output HTML with paths that will operate without knowing ahead of time what folder or sub-folder they will be served from? I think many people commenting assume that the paths would take into account their position within the file tree when they are exported.
At the compile time it seems totally possible to know the relative path to the root of the _next folder from any page or component that might be using it but the URLs do not resolve that way at present unless I’m missing some config flag? I read the docs extensively to try and solve this one!
Essentially, at present as far as I can tell, you need to modify the config according to the place the code is going to be deployed (see
basePathdocumentation). This seems pretty counterintuitive, although I realise that it is a non-issue for Vercel since they are deploying to known root paths in all cases.I don’t understand how more people are not complaining about this. @cargallo - thank you for the work around 👍
well, like every one else I had faced this issue of wrong assets paths and it did not get solved by adding
.to start of the string. I realized that I need to remove the/from the start of the path entirely. Thanks to @cargallo solution, I came up with my own script. So first you need to install the library:npm i --save replace-in-fileEdit: The document of the library uses--saveflag for installation but we don’t need to install this as a dependency in our next.js project. Use-Dor--save-devinstead.Then create a
replacer.jsat the root of your project and paste the below code in it:Add the followings to your
package.json"scripts":now, after runing the scripts you must be able to serve the
outfolder even from a different root.Edit-2: as @peterDuot-stationfive mentioned it should be
node replacer.jsinstead ofnode replaceInFiles.js.@OHHAKO Sorry for the late reply.
Are you putting your image resources under ./public folder? (see https://nextjs.org/docs/basic-features/static-file-serving)
Example
When you layout the public folder like the following:
Performing a “next build” + “next export”, the images will produce an out folder with:
Then you can reference the images in your project like:
pages/index.js
OR via a component ./component/MyImage.js
./pages/index.js
NOTE
You will get an error if you attempt to use the inbuilt “next\Image” during an export
pages/index.js
Error:
should be
node replacer.jsbut I used this and it’s working in my local. Thank you 😃It’s still happening! @spudmashmedia your advice really helpful to me. but it works only to specific element. (Doesn’t work to img tag path) thanks!
I don’t understand therefore what is next export command for? And it’s just easy to implement it has no sense at all.