Closed tomaswitek closed 4 years ago
cc @jxnblk
cc @alexindigo @DullReferenceException
Would love to have your feedback ๐
After playing with the code I realized that it would be much easier to split assetPrefix
into multiple parts:
module.exports = {
host: NOW_URL ? `https://${alias}` : 'http://localhost:3000',
basePath: '/account',
}
We can still keep the assetPrefix
variable internaly, but the user should define more precisely what he needs.
For the asset part is really ok to provide these two variables together. For routing etc we need them separately. Or maybe we can even provide it together in a config file and then split it in the next.js codebase. In this case assetPrefix is not the right name I am afraid.
As a sideffect this also leads to less code changes. It's quite obvious if you compare those two PRs: https://github.com/panter/next.js/pull/2 (split) https://github.com/panter/next.js/pull/1 (pass both)
In my opinion, they should be separate, the reason for this is that it's not breaking and more flexible to keep assetPrefix
and have basePath
seperately.
Is assetPrefix
the right name then? Both variables are actually a prefix right?
assetPrefix
is for assets eg: the page bundles. basePath will be for the router.
The way it should work is:
assetPrefix
is defined use assetPrefix
to load bundles, don't touch the router (current behavior)assetPrefix
and basePath
are provided use assetPrefix
to load bundles, add basePath
to routerassetPrefix
is not defined and basePath
is, use basePath
to load bundles, and add basePath
to routerassetPrefix
nor basePath
is defined we do nothing different (current behavior when assetPrefix
is not provided)cc @alexindigo @DullReferenceException @3rd-Eden
Could you give feedback on the above proposal: https://github.com/zeit/next.js/issues/4998#issuecomment-414978297
@tomaswitek Not sure what exactly didn't work for you with current assetPrefix
, this is asset prefix we're using in production: "assetPrefix":"https://static.trulia-cdn.com/javascript"
and it works as expected.
And in general, we're using multiple zones (we call them islands) on the same domain and "basePathing" each island never came to our minds, since it'd complicate interoperability between the islands. Let me elaborate on tat a little bit more:
So we have two islands A
and B
, and the main idea with it is transparency for the users that navigate island to island as part of their one-website experience. So there should be links between the islands. Then there is deployment concern vs. application concern.
Deployment concern vs. application concern โ application has no idea where it could be deployed, it just know how to handle incoming http requests โย it has set out routes it can respond to. When it's deployed somewhere โย it could be different domains, different ports, and yes theoretically it could be different basePath, that will be made transparent for the app via proxy or other means.
Cross links between the islands โ to keep the spirit of the islands as a separate deployable entities there shouldn't be any internal implementation knowledge leaks between different islands.
So best way for islands to reference pages of each other, is for them to export available routes for other island(s) to consume (and in the nextjs world it looks like custom <IslandALink>
kind of components would be a preferred way).
So far it's all straight forward โ all islands assume to share the same domain and have their set of absolute paths (/path1
, path2
, etc). That way second island imports that list of paths and relies on it to be stable. In the same time it's pretty minimal requirement for each island to keep their paths backward compatible (which is good thing in web anyway) :)
When we add deployment specific basePath, we automatically increase complexity of the whole system โย should each island know (and maybe dictate) it's own deployment basePath? Then how is it different from the way thing work currently? Or should island A be agnostic of it's deployment path? Then how island B will find deployed island A, since it only knows what island A knows about itself? Or you'd have to supply basePath for all the deployed island to all other islands? And with modern way off deploying things, it means redeploying all the islands when you need to add new one.
Or how you envisioned that part of the story?
Thank you.
^ it was written before morning coffee, so please let me know if you need more coherent explanation for any parts of it. :)
First of all thank you guys that you took the time for reviewing my issue.
@timneutkens Yes assetPrefix
has priority over basePath
, that is exactly what we've discussed at the beginning. After I saw how many files I had to change I thought the second way would be cleaner. But I'll rollback to the first solution. Let's keep it totally separate, no problem at all. I was just loudly thinking.
@alexindigo Thx for your detailed answer. Let me try to answer your questions ๐
Not sure what exactly didn't work for you with current assetPrefix
I have two problems here:
assetPrefix
on a single domain requires more adjustments in proxy routing, static files etc. We could reduce this adjustments by introducing basePath
. It won't brake anything and it won't increase complexity because you don't have to provide the basePath
as @timneutkens already mentioned.application has no idea where it could be deployed
We have the same goal here of course! We are defining assetPrefixes dynamically in the current solution we have. It is provided via request headers by proxy.
Then how is it different from the way thing work currently?
Router will be aware of contextPath and will reduce the amount of custom code.
should each island know (and maybe dictate) it's own deployment basePath? Or should island A be agnostic of it's deployment path?
It doesn't have to be. The developer should have freedom here. It should be possible to provide basePath dynamically the same way as assetPrefix.
Then how island B will find deployed island A, since it only knows what island A knows about itself? Or you'd have to supply basePath for all the deployed island to all other islands? And with modern way off deploying things, it means redeploying all the islands when you need to add new one.
Maybe you could also add the basePath into the routes export. I don't know. I am not saying that the basePath variable is important for every use case. It looks like it's not the best solution for you. But that's totally fine. The thing is that you can still use just assetPrefix
and nothing will change for your islands. It looks like you have your own routing anyway. Cross links between zones is not even important for our project, our zones are really independent and isolated from each other.
And with modern way off deploying things, it means redeploying all the islands when you need to add new one.
I don't see a reason why. I can even imagine that some zones have basePaths and some not. And maybe some apps will use the basePath config even without multi zones setup.
@alexindigo could you pls provide us with a two real island urls, which are rendered by next.js so I could see it in action? I tried to find one, but could't find a page on your domain with _next
requests ๐
Do all of your islands have the same configuration?
"assetPrefix":"https://static.trulia-cdn.com/javascript"
@tomaswitek
I can't work with multiple domains nor subdomains in the current project. (Domain restrictions and no wildcard SSL certificate)
Oh, so you don't use CDN in the classical sense, but rely on assets being fetched from each app directly? I see.
The current implementation of assetPrefix on a single domain requires more adjustments in proxy routing, static files etc. We could reduce this adjustments by introducing basePath. It won't brake anything and it won't increase complexity because you don't have to provide the basePath as @timneutkens already mentioned.
Btw, it wasn't "no, don't add that feature" :) It was more like โย "Probably we can think about this approach more holistically" :)
It doesn't have to be. The developer should have freedom here. It should be possible to provide basePath dynamically the same way as assetPrefix.
Yes. It only works though when there is no linking between the islands. And sounds like this is your use case. In the same time, I'm having hard time understanding what makes them islands instead of just being a bunch of standalone applications then, if they're 100% independent? :)
Maybe you could also add the basePath into the routes export.
I don't se how it could be done (easily), since routes export happens at build time, and basePath being defined at deployment time, and there could be more than one deployment of the same code artifact (stage, preprod, prod, testing env, etc).
Do all of your islands have the same configuration? "assetPrefix":"https://static.trulia-cdn.com/javascript"
Yes, all islands share their assets, since next does content hashing, it's not only non-issue, but actually very beneficial. (We extract built assets from each artifact and publish on CDN at deployment time).
And that way we have only "regular html" requests to our app servers, this is why I won't see any "_next" paths on trulia.com
As for the islands examples:
Our fresh brand new island โ Neighborhoods page โย https://www.trulia.com/n/ca/san-francisco/pacific-heights/81571 (and you can find more of them here: http://www.trulia.com/neighborhoods)
This island is responsible for all /n/*
paths.
And another island is our login page โย https://login.trulia.com/login โ it looks like different domain, but it's really not, it looks that way for different set of reasons, but technically it's the same deployment. :)
And this island handles urls like /login
, /signup
.
Let me know if you have more questions.
@alexindigo thank you very much for your examples. I have a few questions after anaylzing the examples ๐
You still do server rendering for every island, but you try to extract as much possibles assets into a common CDN right?
Can you pls describe a little bit more what exactly happens when https://www.trulia.com/n/ca/san-francisco/pacific-heights/81571 is called? Does your proxy know that /n
stands for neighborhood overview and forwards it to the right island? Does it somehow affects the request before it arrives at the island?
Do you use built-in routing from next inside an island or do you have a custom solution?
I wanted to check the routing inside your island. Unfortunately Neighborhood overview
has more or less just modal navigation without changing the url. In Login there seems to be a completely custom solution.
I hope I'll answer all your questions in this comment ๐
Btw, it wasn't "no, don't add that feature" :) It was more like โ "Probably we can think about this approach more holistically" :)
Sure, it would be great to find a solution where I don't have to touch next.js ๐
Yes. It only works though when there is no linking between the islands. And sounds like this is your use case. In the same time, I'm having hard time understanding what makes them islands instead of just being a bunch of standalone applications then, if they're 100% independent? :)
I never wrote nor said that I search for an "island" solution. I just had a chat with @timneutkens where I described my problem and Tim's answer was basically next.js
does not support basepaths. And after googling a little bit I realized I am not the only one searching for it. So I thought I could contribute a little bit. Afterwards Tim pinged you to give a me a feedback and I am very thankful for your feedback.
I don't se how it could be done (easily), since routes export happens at build time, and basePath being defined at deployment time, and there could be more than one deployment of the same code artifact (stage, preprod, prod, testing env, etc).
Well if you want to export routes at build time and make them available for other islands then the only straightforward way is probably to hardcode the basePath in the config. I get your point. On the other side, is that really such a problem? You could still deploy the app to different domains and ports and you could use the same basePath for each env.
Good morning @tomaswitek :)
My experience with the "basePath" functionality, that it very deceiving in it's complexity, and it's usually better to implement that kind of things without rushing into it with one specific problem, but looking at it from multiple angles. Similar to how you'd approach deep merging โ outline multiple use cases and see how (and if) they all fall under one umbrella. Since having incompatible features between (even major) versions of the framework is very annoying :)
You could still deploy the app to different domains and ports and you could use the same basePath for each env.
Sounds like you'd be ok with the solution where that "basePath" is part of your routing code, something that you mentioned in the beginning โ like subfolder inside pages
directory (btw, that approach would signal to developers chosen basePath pretty well). But the only thing that stopped you is that internal nextjs path for assets _next
isn't configurable.
And that sounds like more narrow problem we can solve with less long term side effects.
And it could bring us even further, like if we can configure assetPath per asset (e.g. with next.config map of some sort) โ it will allow us to have shared assets between the apps, which will improve performance, and other things.
And there is open PR for that feature. ;) /cc @timneutkens sounds like it's time to get back to that puppy. :)
If you're not going to add this any time soon, could we get an example express based server.js added to the readme that does this and works? I've tried a few that have been floating around in these issues but couldn't get them to work. Thanks.
Hi @ccarse I have a working fork which we use in production already: https://github.com/panter/next.js/pull/2
I am also ready to invest time to open a PR for this feature.
@timneutkens @alexindigo is there a other way how to solve this problem?
If we don't need a basePath
config, can you pls give us a minimal example using assetPath
?
My company is also coming up against this.
We're slowly taking over a legacy app, section by section, and replacing it with Next.js.
As a simplified example:
URL | App |
---|---|
example.com | legacy |
example.com/shop | next |
example.com/search | legacy |
example.com/members | next |
That means we want everything to be prefixed within each Next.js app... Pages, routes, assets, etc.
It's also worth noting that we're not using Now, so we cannot take advantage of now.json
routing. We have our own load balancer sitting out in front of the whole domain and then routing traffic based on subpath.
We're also using a custom server (hapi), so it'd be nice if we could leverage whatever is create here within a custom server too.
Maybe there's some combination of now.config.json
settings or some use of micro-proxy we can use to accomplish this same thing, but we haven't figured out the right combination yet.
We're running into, I think, the same problem with multiple statically exported Next.js apps hosted on Now v2.
URL | App |
---|---|
example.com | next |
example.com/dashboard | next |
As expected, the root app works just fine. Things go awry in the second one though. We're currently wrapping next/link
which, combined with assetPrefix
, solves most of the problem:
export default ({ children, href, ...rest }) => (
<Link href={process.env.NODE_ENV === "production" ? `/dashboard${href}` : href} {...rest}>
{children}
</Link>
);
However, this breaks prefetch
because then it tries to look for .js
files at the wrong URL:
Our current workaround is to disable prefetch
, which isn't ideal.
What's the status on this?
Also looking for an update on this please.
@timneutkens I am ready to invest time to open a PR for it if the community is interested. We are already using a solution (https://github.com/panter/next.js/pull/1) in production and we are very happy with it.
We also need a solution for this
We're going to introduce a new API soon that will make this proposal obsolete.
Also impacted by this. Need to run the next project under a subdirectory path. Looking forward to the official feature. Is there an ETA?
API
So how is it going? :D
Please don't spam the thread and use GitHub's ๐ feature on the issue itself.
@timneutkens Can you provide any more info? What is the API that will make this obsolete? What do you consider "soon"? Thank you.
This may not be exactly related to multi-zones but may help...
I solved something similar to this by creating a custom server and using proxy middleware
for example: @Zertz Please note: you still need to solve the link issue - Again, i solved this by creating a link component and passing the prefix down to the app via config and if a prefix exists use that or use nothing, same for static images.
const proxy = require('http-proxy-middleware');
app.setAssetPrefix('/dashboard');
// Express custom server
// Proxy so it works with prefix and without...
// So if asset prefix is set then it still works
const server = express();
server.use(
proxy('/dashboard', {
target: 'http://localhost:3000',
changeOrigin: true,
pathRewrite: {
[`^/dashboard`]: '',
},
}),
);
The proposal I was mentioning is #7329
The proposal I was mentioning is #7329
@timneutkens
Can you pls provide more details on how the proposed hook will solve our basepath problems?
And what about router redirects like Router.push('/about')
will this be replaced with a hook as well?
Thank you for your time ๐
The router api would change too, as it'd need a component to link to. At that point you could use relative paths for the url itself.
Any update on when we can get a solution or at least a workaround for this?
Use ๐ on the initial issue instead of posting any update.
@MMT-LD Your solution kind of works for me, but now on every Link click or Router push event the page reloads โน๏ธ
I tried @Zertz 's solution and it worked perfectly!
Also I could fix prefetch
problem by copying output files to the prefetched paths.
https://github.com/fand/MDMT/blob/master/scripts/copy-preload.js
... it's a dirty trick, but it's working anyways๐คช
@nicholasbraun
Now on every Link click or Router push event the page reloads โน๏ธ
I had this issue but fixed using the 'as' parameter on the link, so the link points to the internal file, but the 'as' is relative to path
eg:
<Link href={"/${item.link}"} as={"./${item.link}"}>
@nicholasbraun
Your solution kind of works for me, but now on every Link click or Router push event the page reloads โน๏ธ
This is kind of what i meant. This is from memory.... but im sure you cant get to what you need from the below.
// WithConfig component
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
const WithConfig = ({ children }) =>
children && children({ config: publicRuntimeConfig });
export default WithConfig;
// Extended Link component
import React from 'react';
import PropTypes from 'prop-types';
import Link from 'next/link';
import { WithConfig } from '../WithConfig';
/*
<Link> component has two main props:
href: the path inside pages directory + query string. e.g. /page/querystring?id=1
as: the path that will be rendered in the browser URL bar. e.g. /page/querystring/1
*/
const NextLink = ({
browserHref,
pagesHref,
whatever,
}) => {
return (
<WithConfig>
{({ config: { pathPrefix } = {} }) => (
<Link
as={pathPrefix ? `${pathPrefix}${browserHref}` : browserHref}
href={pagesHref}
passHref
>
<a>{whatever}</a> // this bit is up to you - children or whatever
</Link>
)}
</WithConfig>
);
};
NextLink.propTypes = {
browserHref: PropTypes.string.isRequired,
pagesHref: PropTypes.string,
};
NextLink.defaultProps = {
pagesHref: undefined,
};
export default NextLink;
Usage:
import NextLink from '../NextLink'
<NextLink browserHref={`/page/1`} pagesHref={`/page?querystring=1`} whatever='I'm the link' />
Good luck :smiley:
As the useLink
RFC is now declined (#7329) and having basePath
support would greatly help us, is the Next.js project happy to accept PRs implementing it? I'm willing to do it.
Looking at this implementation by @tomaswitek, it seems to be going in the right direction, most importantly making the router aware of basePath
. Are there other non-obvious things that would make basePath
support difficult?
Overall, I think that the design is clear, just a single config variable:
module.exports = {
basePath: '/demo'
}
Interactions with assetPrefix
are well-defined here: https://github.com/zeit/next.js/issues/4998#issuecomment-414978297.
UPDATE: I was also thinking whether it would be possible to implement this by creating a custom router and somehow swapping the default one but it doesn't seem to be possible, Next.js hardcodes its router, see e.g. here. I'm also skeptical that "only" replacing the router would be enough; the feature probably needs to be supported by Next.js as a whole.
This problem is around since 2017, is there any workaround? Or an official response to our basePath request?
So after trying to make this work with the combination of assetPrefix
and a custom <Link>
component as suggested in e.g. https://github.com/zeit/next.js/issues/4998#issuecomment-464345554 or https://github.com/zeit/next.js/issues/4998#issuecomment-521189412, I don't believe it can be done, unfortunately.
Defining assetPrefix
was relatively straightforward, something like this in next.config.js
:
const assetPrefix = process.env.DEPLOYMENT_BUILD ? '/subdir' : '';
module.exports = {
assetPrefix,
env: {
ASSET_PREFIX: assetPrefix,
},
}
The next step is a custom Link
component. The first idea, given e.g. in https://github.com/zeit/next.js/issues/4998#issuecomment-464345554, is to prefix href like this (simplified):
export default ({ children, href, ...rest }) => (
<Link href={`${process.env.ASSET_PREFIX}${href}`} {...rest}>
{children}
</Link>
);
As reported by others in this thread, this breaks prefetching as the requests are suddenly to /subdir/_next/static/.../pages/subdir/example.js โ the other "subdir" should not be there. But with our custom Link
component, we're setting href to /subdir/example
, so no wonder Next.js is requesting a bundle of the pages/subdir/example.js
page.
So OK, problematic prefetching doesn't sound like the end of the world (though the UX is quite ugly) but in our app, things get worse as we're using Next.js 9's dynamic routing. For that, we need to set as
properly so the evolution of the custom Link
component looks like this:
export default ({ children, href, as, ...rest }) => (
<Link
href={`${process.env.ASSET_PREFIX}${href}`}
as={`${process.env.ASSET_PREFIX}${as}`}
{...rest}
>
{children}
</Link>
);
Usage is:
<CustomLink href='/post/[id]' as='/post/1'>...</CustomLink>
which gets converted to:
<Link href='/subdir/post/[id]' as='/subdir/post/1'>...</Link>
and that was not working for me when deployed on Now at all โ trying to navigate to https://deployment-id.now.sh/subdir/post/1
lead to 404. I'm not entirely sure why, maybe it's also an issue with (UPDATE: it's because of https://github.com/zeit/next.js/pull/8426#issuecomment-522801831) but ultimately, we're confusing Next.js' router when we're asking for @now/next
builder/subdir/post/[id]
component when no such file exists on the disk.
There's another example in this thread, https://github.com/zeit/next.js/issues/4998#issuecomment-521189412, that prefixes only as, not href, like this (simplified):
export default ({ children, href, as, ...rest }) => (
<Link href={href} as={`${process.env.ASSET_PREFIX}${as}`} {...rest}>
{children}
</Link>
);
but that will throw this error in the browser:
Your
<Link>
'sas
value is incompatible with thehref
value. This is invalid.
It's a problem reported in https://github.com/zeit/next.js/issues/7488.
After all this, I don't think there's a solution until something like basePath
is supported, which I'd be happy to help with.
@borekb I am also ready to help as I mentioned couple of times before. All workarounds I saw until now solve only part of the problem. Right now we are using a fork of next.js in production which implements basePath.
We're going to introduce a new API soon that will make this proposal obsolete.
@tim Is it still the case or was the new proposal for API closed? https://github.com/zeit/next.js/issues/7329
Btw. tomorrow it will exactly one year I opened this issue ๐
One relatively wild idea is to have pages in something like src/pages
and then symlink them to the proper location. For example:
myapp.example.com
, I'd symlink src/pages
to pages
example.com/myapp
, I'd symlink src/pages
to pages/myapp
In combination with custom <Link>
component and assetPrefix
, it could work but I'm not brave enough to try it ๐.
Any update with that?
Any progress on basePath
support? :)
@nicholasbraun
Now on every Link click or Router push event the page reloads frowning_face
I had this issue but fixed using the 'as' parameter on the link, so the link points to the internal file, but the 'as' is relative to path eg:
<Link href={"/${item.link}"} as={"./${item.link}"}>
You saved my day! :)))
i am doing the same with Router.push(`/route`, `${process.env.BASE_PATH}route`);
@nicholasbraun
Now on every Link click or Router push event the page reloads โน๏ธ
I had this issue but fixed using the 'as' parameter on the link, so the link points to the internal file, but the 'as' is relative to path eg:
<Link href={"/${item.link}"} as={"./${item.link}"}>
This solution does not work with next 9 file based routing. /route/[id]
, ${process.env.BASE_PATH}/route${id}
throws this error
This comment explains the problem very well.
Although i've seen a few people discuss how the solutions here break pre-fetching. For us theres another more important issue.
With next9, using an assetPrefix in your href
makes next
always perform a server route. I have created a reproduction repo in this issue which demos it happening.
This essentially breaks our Apollo Client cache, as it is re-created on every route.
I think the implementation is comparing the underlying page href
without an assetPrefix, to the next routes href
(which includes an assetPrefix) - resulting in a deep route.
e.g If you are on href /prefix/page
(underlying page is just /page
) and your next href
route is /prefix/page/[id]
(because without the prefix it will 404) this is a compeltely different route and a shallow route isnt possible.
Looking into workarounds at the minute with express routes
When use component with the href
props that is basePath, the prefetch is not working.
PLZ support basePath and prefetch, it will be awesome
I could really use this. I'm running multiple apps from a single server source and have each separated into its own web/appX/{next project files}
. It'd be great to have more control over the basePath. I've figured out a workaround for now, but its not very pretty.
Feature request
Is your feature request related to a problem? Please describe.
Multi zones is a great feature which allows to run multiple next.js apps on the same domain, but it doesn't allow to define a basepath which will be accepted by all parts of next.js. Since we are not able to namespace apps right now it is not possible to have the same names for pages in various apps.
Describe the solution you'd like
I want to be able to configure a
basepath
in the next.config.js file. Thanks to this configuration all parts of next.js (Router, Link, Static assets etc.) will be aware of the basepath and will automatically generate and match to the correct paths.Describe alternatives you've considered
One alternative is to nest all desired pages into a folder which matches the basepath. This solves just one small issue with routing and is quite ugly because most of the my basepaths are not one level paths. The second alterantive is to configure a proxy in a way where the basepath is automatically removed before the request arrives into a next.js app and also implement a custom Link component which automatically adds basepath to all links. I just don't want to maintain custom fork of next.js. It doesn't make sense in my opinion.
Additional context
The
assetPrefix
solution allows us to define a different prefix for each app. But as fair as I know it works only with different hosts.with-zones example
If I add a basepath to it everything fails
In my opinion we should split it into 2 variables:
Related issues