Use relative URLs instead of absolute #32216
Replies: 49 comments 4 replies
-
@mikecardwell Other issues are talking about this. The solution is to use assetPrefix. |
Beta Was this translation helpful? Give feedback.
-
That is not a very good solution as it would require me to build one set of files to be served by a webserver, and then a different set of files to be accessed via a file:/// url. A better solution would be the one I described where relative URLs are used throughout. |
Beta Was this translation helpful? Give feedback.
-
@mikecardwell I meant that you can use But I still can't see why is it required to preview locally via file URI? Couldn't you use serve or any other http server locally? |
Beta Was this translation helpful? Give feedback.
-
Making the urls relative won't fix the issue. Since then you'd have issues when there's nested pages, |
Beta Was this translation helpful? Give feedback.
{{editor}}'s edit
{{editor}}'s edit
-
@timneutkens - Your example doesn't work because @blistok's solution was wrong and would not work. When I say to use relative URLs, I mean that each html page which is generated would know where it is relative to the content it is trying to link to. For example when generating "some-dir/some-route", it would know that it is 2 levels deep, and it would know that the _next directory is one level deep, so when generating that particular page of HTML, it would use "../_next/blah". When generating another page for the same site, it might need to use "../../../_next/blah". This isn't just the "_next" directory I'm talking about either, it's all resources, e.g. link tags etc. As it stands, "next export" doesn't produce a website which can be served from anywhere, it spits out a website that can be served from a particular subset of URL structures. Here is a real life example of a website where that is a problem: http://www.exim.org - That is a statically generated website. It also has a large number of mirrors - http://www.exim.org/mirmon/www_mirrors.html - Many of these mirror a website from a sub url. E.g http://mirror.easyname.at/exim/ - The mirrors are updated by simply rsyncing the static files from the source on a schedule. This setup could not work with the files that nextjs spits out, because they are not portable. Here's another use case: I want to build a nextjs app which I can just export to disk, zip up and distribute it as an application for people to unzip on their desktops/laptops and open in their web browser using "File -> Open". I can't do that with next export as it stands, because everyone will be opening the project from a different directory. HTML documentation that comes packaged along with the application in a .tgz... Step 1 for reading this documentation, install a web server? If nextjs used relative urls then you wouldn't need to tell people who want to serve up their site on things like github pages to add assetPrefix to their nextjs configuration. It would "just work". [edit] You closed this issue prematurely IMO. I hope that doesn't mean my response is ignored. |
Beta Was this translation helpful? Give feedback.
{{editor}}'s edit
{{editor}}'s edit
-
Support for exporting a static site with internal links and asset references all being relative urls would be rad for statically hosting next.js apps on ipfs or, as @mikecardwell explains, any situation where the site needs to be self-contained and can't assume a specific mounting path. |
Beta Was this translation helpful? Give feedback.
-
+1 Will be like, "woah, man" once Mike's ideas become reality. |
Beta Was this translation helpful? Give feedback.
{{editor}}'s edit
{{editor}}'s edit
-
@timneutkens I am re-opening this. It's causing Next.js apps that are routed through a Path Alias rule to return the incorrect hostname which leads to potential CSP issues, etc. It would be nice to have a good solution for this. |
Beta Was this translation helpful? Give feedback.
-
@TooTallNate we use path alias for |
Beta Was this translation helpful? Give feedback.
-
@arunoda The router could be extended with a function to allow you to look up the current path to the root of the app. Then people could just wrap such components in the withRouter HOC and then do things like: <img src={ `${this.props.router.pathToRoot}/static/images/foo.png` }/> Which would sometimes spit out Of course, people who are happy with absolute URLs can just continue doing what they're doing right now, but for all of the use cases which require relative URLs, a solution would exist. |
Beta Was this translation helpful? Give feedback.
-
I'd be totally happy with a manual solution to this like @mikecardwell suggests. |
Beta Was this translation helpful? Give feedback.
-
Gonna +1 this issue. Perhaps this is a specific use case (but I have seen others complaining). My situation is that I am running multiple nextJS apps in a Kubernetes cluster with a NGINX ingress module getting requests from an AWS ELB. Each app is assigned a namespace such as To make links work (see: This of course, doesn't work with nextJS as the paths are all absolute instead of relative ( Would love to see support for either |
Beta Was this translation helpful? Give feedback.
-
Any solutions to this? I am running nextjs static export from cordova which doesn't have webserver and serves pages as |
Beta Was this translation helpful? Give feedback.
-
I think the only current solution is to post process the exported HTML to modify the links. |
Beta Was this translation helpful? Give feedback.
-
Thanks, the main issue with pathanmes like |
Beta Was this translation helpful? Give feedback.
-
How it worked for me you can also give it a try.Main points
--- imp
|
Beta Was this translation helpful? Give feedback.
-
I guess as of right now the issue is that the javascript code have no way of figuring out the base without extra configuration, after all the url are written as /xxx/yyy/zz instead of xxx#yyy/zzz, and because of that, it is difficult to figure out where the actual base path is just from the url itself. |
Beta Was this translation helpful? Give feedback.
-
In my case, this config helped:
|
Beta Was this translation helpful? Give feedback.
-
We find ourselves in a similar situation to @tanishq-dubey, where some path flexibility currently keeps us from being able to migrate to Next. Currently we package our React apps as Docker images which allow for the If we could get past the base path hangup, our only other issue is more of a nitpick. Just that including the entire Next package in the image for serving the app feels pretty heavy at ~80MB uncompressed. Would be nice if the production server could be broken out into its own module, but maybe that's crazy talk. TBH I haven't researched the options much. |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
This feature would be great in Electron apps. In order to use Next.js with Electron in production, one would need to start a custom server on localhost, making it available in the browser as well, which is not always desirable. If this is implemented, we could just |
Beta Was this translation helpful? Give feedback.
-
Currently facing this in my first react project as well. Are we on the same boat or is there a method / workaround for this in react? |
Beta Was this translation helpful? Give feedback.
-
I don't think there are any generally useable solutions/workarounds. I assume that, given the amount of demand for this functionality, and the lack of any movement on it in the 4 years this issue has been open, it's just too difficult for the devs to bolt on now. It should have been there from the beginning. |
Beta Was this translation helpful? Give feedback.
-
Kind of annoying situation honestly, |
Beta Was this translation helpful? Give feedback.
-
I'm running into this problem as well as I'm trying to figure out how to render my exported Next.js app within Electron.
Has anyone evaluated whether these options are able to solve this problem? |
Beta Was this translation helpful? Give feedback.
-
Commenting just to say that this is such an obvious default next.js should come with that I'm absolutely flabbergasted. Flabbergasted! |
Beta Was this translation helpful? Give feedback.
{{editor}}'s edit
{{editor}}'s edit
-
This is really unfortunate. The requirement to use absolute URLs for assets prevents the Build Once, Run Anywhere promise of Docker from being realized. If you build a Docker image using built/exported Next.JS code, you end up with an image that only works when the app is hosted at a specific path. It is easily broken once you put it in an environment where it sits at a different place in the URL hierarchy, which is very common thing to do because people often want to have a reverse proxy that routes to different apps based on their path prefix. |
Beta Was this translation helpful? Give feedback.
-
i am facing the same wall as well. I have deployed my next app on fleek, everything is fine on the home page but others pages are breaking after refresh with an error message that says the path is incorrect. |
Beta Was this translation helpful? Give feedback.
{{editor}}'s edit
{{editor}}'s edit
-
I have the same requirement for using My workaround is: Instead of config Here is the example, when page is laded, the script will check the protocol, and iterate all of <script>
if (window.location.protocol === 'file:') {
const styleSheets = document.styleSheets;
let head = document.getElementsByTagName('head')[0];
for (let i = 0; i < styleSheets.length; i++) {
let link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = `./${styleSheets[i].href.split('/').pop()}`;
head.appendChild(link);
}
}
</script> Please notice, the script will cause infinite loop if you append the link with a workable href that is found and already loaded The same approach could also be applied to other assets or |
Beta Was this translation helpful? Give feedback.
-
I'm working on a static Next.js site and ran into this issue today. Being new to Next and coming from a Jekyll background, I'm surprised this is still a problem after 5+ years, Jekyll handles this out of the box with minimal configuration tweaks or awkward workarounds. |
Beta Was this translation helpful? Give feedback.
-
There are many tags in the HTML which NextJS generates which contain absolute URLs. For example:
I've added selenium + chrome headless testing to my Next project, and it works fine when I test http://localhost:3000, but when I do a
next export
, and then try to to testfile:///home/user/project_dir/out/index.html
, all of those absolute links are broken.I.e, it tries to fetchfile:///_next/blah
instead of:file:///home/user/project_dir/out/_next/blah
.If I post-edit the generated HTML to make those links relative, then it all works fine.
I need to be able to run my tests against the static files which are generated by export to ensure that they have been generated correctly before publishing.
I suspect this is also an issue when people simply wish to run their nextjs application from a location other than the root of their website.
Your Environment
Beta Was this translation helpful? Give feedback.