Use relative URLs instead of absolute #32216
Replies: 60 comments 12 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.
-
@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.
-
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.
-
@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.
-
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.
-
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.
-
Just had to urgently re-write an entire NextJS app project in Create React App with React Router because of this issue. file:// based routing is definitely still a thing with some large clients that distribute their app offline using secure intranet systems. Huge blow to the project, since we wanted to use a CMS with it using SSG, and this won't be possible with CRA. Would be a great addition for NextJS to have some sort of config to handle this type of use case. Also, this would work for simple electron builds, which we build a lot of for conferences where we have to pass off a single executable to the conference team to run and display on a television. My opinion is that if Vercel is going to present NextJS as the best and universal way to build a react app, then it should support these valid use cases as well, not only the ones that directly benefit their business model. Ultimately, we are still your customers using vercel for various apps, but we have to build these types of apps as well. And if that can't be done, then we have to look for alternatives and consider just making all of our apps using those alternatives. |
Beta Was this translation helpful? Give feedback.
-
@mikecardwell Did you ever find a solution? I'm in the same boat where the site is being distributed via zip file and ran locally by opening in Windows Explorer. Total nightmare scenario as I previously had it working on a plain vanilla HTML/CSS/JS site and client is wondering why I can't make it work. Is it easy to convert project to a react app? |
Beta Was this translation helpful? Give feedback.
-
You'll might just have to convert all the pages to React Router and CRA or
Vite if you want a quick solution. It might take a few hours depending on
the size of the app.
…On Tue, Aug 15, 2023, 5:24 PM Ben Rudolph ***@***.***> wrote:
Yes, especially government entities. Still very much a thing with them.
How did you go about converting to a React app? Was it easy? I'm in a bind
and a client is beating down my door wondering why I can't make this work
since the previous site did. I thought I was pushing this project forward
moving to Next.js but simply not the case in this scenario.
—
Reply to this email directly, view it on GitHub
<#32216 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAY5LZ3TZFVFCXH6XJG66YLXVPLJJANCNFSM5VYIEUBQ>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
+1 from me. I'm also running into this issue where I need to provide the client with a zip file that contains the application as standalone html/css/js and I can't control where or how it is deployed. |
Beta Was this translation helpful? Give feedback.
-
Just ran into this extremely annoying problem as well. Are there any plugins that could do this sort of transformation automatically? If this is possible through plugins, could you refer me to the relevant code sections? |
Beta Was this translation helpful? Give feedback.
-
Oh man is there no workaround for this? I can't even post-process the HTMLs because I have links in some CSS stylesheets to some media |
Beta Was this translation helpful? Give feedback.
-
I wanted to package nextjs as an electron application, but nextjs couldn't export using relative paths, so the css and js files couldn't be found on the page of the packaged electron application |
Beta Was this translation helpful? Give feedback.
-
I used logic similar to the following to get around this issue in an electron app:
|
Beta Was this translation helpful? Give feedback.
-
got told this just now by a volunteer building disaster relief content. i dont mean to be dramatic, but i hope you see their point. a simple solution would be simply enable a Framework Preset for static simple pages and Not "build" them. would also save some resources for vercel. or maybe just point that vercel is not able to support such pages and refer people to a competitor like github pages ;) |
Beta Was this translation helpful? Give feedback.
-
Export NextJS to the folder where Electron looks at when building a project or during development, you will get static files. Next, add your own scheme, for example, 'app', register it. Run your app fully. That's the whole solution.
|
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.
All reactions