Add a dataPathPrefix config option #25681
Replies: 20 comments 24 replies
-
@timneutkens thanks for converting this to a discussion and apologies if I got the process wrong here. It would be helpful if someone could give me some feedback so I know if the description above makes sense, or if I need to try and clarify it further? Also what is the best way of getting the ball rolling on this? Should I go ahead and have a stab at creating a pull request? Or wait for some feedback on the idea first? |
Beta Was this translation helpful? Give feedback.
-
Just wanted to voice my support here as well. Our organization has the exact same routing strategy that @petewarman described and we're currently running into the same issue where our requests to We've currently disabled client-side navigation due to this issue, losing performance optimizations that we'd get from prefetching. We've considered solving this problem by using The additional configuration variable |
Beta Was this translation helpful? Give feedback.
-
Piling on as well. This is definitely a feature that would help my team. From my digging there are a number of open discussions on this issue (including a solution I asked for). I don't personally have an opinion about which would be best as they would all work for my use case. @timneutkens I'm happy to contribute a PR or contribute in any other way to help out here. Which solution would be best? |
Beta Was this translation helpful? Give feedback.
-
Those options work for me too - I just need a mechanism to identify which requests should be routed to my application |
Beta Was this translation helpful? Give feedback.
-
Upvoted! My use-case is also similar where we have an external public URL,
The routing was possible in Fastly with a custom VCL: # redirect v2/custom/foobar => http://app.internal/product/custom/foobar
if (req.url.path ~ "^/v2/custom(/.*)?$") {
call product_redirect;
return(lookup);
}
# Redirect Next.JS assets
if (req.url ~ "^/_next(/?|/(.*))$") {
call product_redirect;
return(lookup);
} If you observe the second condition, we redirect So in this case, had to map I have been able to solve this (re: workaround) with a custom server. I inject the server.get('/_next/data/:buildId/v2/custom/:path*', (req, res) => {
const parsedUrl = parse(req.url, true);
// => incoming url: /_next/data/:buildId/v2/custom/:path
parsedUrl.pathname = parsedUrl.pathname.replace(
'v2',
req.headers['x-product']
);
// => outgoing url: /_next/data/:buildId/:product/custom/:path
handle(req, res, parsedUrl);
});
// Let Next.js handle rest of the routes
server.all('*', handle); |
Beta Was this translation helpful? Give feedback.
-
this is an issue for us as well at the moment. Our services are grouped under paths per service |
Beta Was this translation helpful? Give feedback.
-
For anyone else struggling with this problem - I came across this article which describes using a custom It's is a bit of a hacky work around though - nextjs needs a better solution to this problem. |
Beta Was this translation helpful? Give feedback.
-
Also running into issues with the exact same potential solution. |
Beta Was this translation helpful? Give feedback.
-
We're also affected by this and it is hindering our NextJS Adoption. |
Beta Was this translation helpful? Give feedback.
-
expected to add this config |
Beta Was this translation helpful? Give feedback.
-
Next.js uses the client side const useInterceptNextDataHref = ({
router,
namespace,
}: {
router: Router;
namespace: string;
}) => {
useEffect(() => {
if (router.pageLoader?.getDataHref) {
const originalGetDataHref = router.pageLoader.getDataHref;
router.pageLoader.getDataHref = function (...args: any[]) {
const r = originalGetDataHref.call(router.pageLoader, ...args);
return r && r.startsWith('/_next/data')
? `/${namespace}${r}`
: r;
};
}
}, [router, namespace]);
}; We can then use it inside of our _app for example: import type { AppProps } from 'next/app'
function MyApp({ Component, pageProps, router }: AppProps) {
useInterceptNextDataHref({
router,
namespace: 'my-app-namespace',
});
return <Component {...pageProps} />
}
export default MyApp Then simply add a rewrite rule in next.config: module.exports = {
async rewrites() {
return [
{
source: `/my-app-namespace/_next/data/:path*`,
destination: '/_next/data/:path*'
},
]
},
} |
Beta Was this translation helpful? Give feedback.
-
We're running into this issue as well. I don't know if adding a We almost need a change to
where |
Beta Was this translation helpful? Give feedback.
-
Bump, spending much time in getStaticProps, turns out the basePath doesn't work well with assetPrefix. |
Beta Was this translation helpful? Give feedback.
-
Just adding to the many voices here that would like something like a We're following this guide (provided by Vercel): https://vercel.com/guides/how-can-i-serve-multiple-projects-under-a-single-domain, but it's somewhat unusable because it doesn't work without the More ContextTL;DR: The SetupRewrites on a separate domain
assetPrefix So we've gone ahead and done that and it's working fine to fetch files under We'll try the workaround for now that @alizeait has shared. |
Beta Was this translation helpful? Give feedback.
-
Adding my support to this too 👍 |
Beta Was this translation helpful? Give feedback.
-
Same here as well, glad to have the workaround but was surprised to see it wasn't yet an option in the config. |
Beta Was this translation helpful? Give feedback.
-
Does anyone find a way to do this? |
Beta Was this translation helpful? Give feedback.
-
This is potentially solved by using It is fully solved when using the App Router (which did not exist when this was originally created), as |
Beta Was this translation helpful? Give feedback.
-
Here is my workaround to fix that, I hope it can help anyone facing the same issue. Simply you can replace the path in the bundled javascript using webpack string-replace-loader loader as following:
|
Beta Was this translation helpful? Give feedback.
-
Bump, the problem is still relevant.
As a result, Next.js will generate a /_next/data path like this:
This allows nginx to detect that the request belongs to this specific app and route it to the correct folder.
Make sure to place all static files in public/myapp/, and then you can access them via |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the feature you'd like to request
I am currently not able to upgrade beyond 9.5.0 because
assetPrefix
no longer applies to_next/data
requests (#15634).A bit of context - my organisation splits traffic between multiple applications for various parts of our site. Incoming requests are routed to relevant applications by matching certain url patterns. The application I work on handles three different base paths - for the sake of simplicity let's call them
/a
,/b
and/c
, and we are currently using theassetPrefix
config to add a path prefix to effectively add a namespace all our static assets and data requests (let's call that/app-prefix
). So the routing logic that sits in front of my application can simply pass all requests that begin with/a
,/b
,/c
or/app-prefix
through to my application.As of 9.5.1 the assetPrefix no longer gets applied to the data requests so those requests are not sent to our application and now return 404s instead.
We aren't able to use the newer basePath config option as that would also transform the
/a
,/b
,/c
paths to be/app-prefix/a
,/app-prefix/b
,/app-prefix/c
. Which is not what we want.Describe the solution you'd like
I'd like to propose a new config option (something like
dataPathPrefix
) to allow namespacing of_next/data
requests fromgetServerSideProps
calls.Describe alternatives you've considered
The alternative would be to revert the changes introduced to
assetPrefix
in 9.5.1 but presumably that would break things for other people.Beta Was this translation helpful? Give feedback.
All reactions