-
Notifications
You must be signed in to change notification settings - Fork 27.6k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use relative URLs instead of absolute #2581
Comments
@mikecardwell Other issues are talking about this. The solution is to use assetPrefix. |
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. |
@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? |
Making the urls relative won't fix the issue. Since then you'd have issues when there's nested pages, |
@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. |
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. |
+1 Will be like, "woah, man" once Mike's ideas become reality. |
@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. |
@TooTallNate we use path alias for |
@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. |
I'd be totally happy with a manual solution to this like @mikecardwell suggests. |
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 |
Any solutions to this? I am running nextjs static export from cordova which doesn't have webserver and serves pages as |
I think the only current solution is to post process the exported HTML to modify the links. |
Thanks, the main issue with pathanmes like |
Yeah, it's rubbish. I'd be tempted to leverage headless chrome via the Puppeteer library to modify the links. I guess the alternative would be to modify NextJS to give it the support I described earlier in this thread. I don't think the core devs are interested in making those changes themselves. |
Same issue here, I have used |
An option to export as relative paths (just don't prefix everything with |
for cordova i solved this using cordova local webserver |
@Yuripetusko |
@tky5622 ok good call, will write something more formal in next few days |
@Yuripetusko Can you explain fast how you did? :) |
+1 on this issue My use case is a bit different. I'd like to use Next to build an app and distribute that over IPFS. When an app is running through IPFS, it's impossible to know the path it will be running in, so using For example, my app could be running in any of the following:
|
Can't this be solved with the |
No, because the base path is not always known or fixed. See my comment above. |
Also my comment from a couple months ago #2581 (comment) references the base tag. |
I too am having this issue. I'm contracting for the government in Australia creating educational content using next, part of the requirement (antiquated though it may be) when we want to publish this educational website is that it should be a 'self-contained zip'. Is there no export option that can be added to make this 'just work'? Find and replace or custom post processing is cumbersome. Also, it's strange that the documentation doesn't mention that a HTTP server is required. For next users like myself, it'd be great if you were more explicit about the fact that the output of next export is not self-contained and requires a server |
I too am having this issue. My case is that I need to place static files generated by next.js to server in Hybrid APP where html files are visited through file:// protocol and the absolute path is not a fixed value. |
If you use ionic webview plugin then you can make it work since ionic runs local webserver serving your files to http://localhost:8081 then you just copy nextjs static export files to www folder and only export index.html and let other routes resolve using client side (SSR not needed) |
Thank you @Yuripetusko, I can confirm it works for me. I just created a new Cordova project, added the plugin, copied the static files in
I also had to add
I'm still testing, so it's possible there are other issues. |
To host on IPFS it is possible to use a hack where you dynamically inject // ./pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
const scriptTxt = `
(function () {
const { pathname } = window.location
const ipfsMatch = /.*\\/Qm\\w{44}\\//.exec(pathname)
const base = document.createElement('base')
base.href = ipfsMatch ? ipfsMatch[0] : '/'
document.head.append(base)
})();
`
class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<script dangerouslySetInnerHTML={{__html: scriptTxt}}/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument There are also other issues that arise with it such as relative assetPrefix and routing, which all need their own workarounds. You can see a small example of a Next.js app deployable to IPFS (with my current workarounds) here: https://github.com/Velenir/nextjs-ipfs-example/ |
(slightly contrary opinion) I found the current behaviour more favourable for my flow where I keep on moving my components around while organising and using an absolute link means I can easily move the components that use |
I don't know if this works for any case (I just have an index page), but this is what I ended up doing:
Now I can serve the files anywhere I want to 🤷♀️ |
still cant find any solutions after next js 9 in 2020 :(( |
How it worked for me you can also give it a try.Main points
--- imp
|
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. |
In my case, this config helped:
|
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. |
This comment has been minimized.
This comment has been minimized.
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 |
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? |
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. |
Kind of annoying situation honestly, |
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? |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
The text was updated successfully, but these errors were encountered: