Replies: 6 comments 6 replies
-
This is correct and expected. If you need a more dynamic way, you can have a look at Middleware: https://nextjs.org/docs/middleware |
Beta Was this translation helpful? Give feedback.
-
As far as I know, this only happens for static pages, i.e. if you change to use |
Beta Was this translation helpful? Give feedback.
-
Thanks, solved this with middleware and it fitted perfectly to what I was looking for. |
Beta Was this translation helpful? Give feedback.
-
@qgerome Sure, you can also check more information in the documentation here: https://nextjs.org/docs/advanced-features/middleware
This has been redacted since I have other business logic here so it may contain errors. |
Beta Was this translation helpful? Give feedback.
-
The problem is that Middelwares do not provide route matching, with path params detection, path params replacement, multi lang/domains support, etc. And we don't want to code ourself a path/route matching strategy, ranking the routes to know if the path is more likely to match this route or another one... then extract params in the current path to create the path in an other languages where words will be different, maybe params order too, etc. 🎉 Next config {
rewrites: async () => {
return {
beforeFiles: [
{
source: '/:destinationSlug/viajes/',
destination: '/:destinationSlug/travel/',
has: [{ type: 'host', value: 'www.local.example.es' }],
},
{
source: '/voyage/:destinationSlug/',
destination: '/:destinationSlug/travel/',
has: [{ type: 'host', value: 'www.local.example.fr' }],
},
// a lot more...
],
};
}
} ➡️ Note that we can generate this config from our route.json config file and not hard code each rewrite for each route. ❌ But because we know the host value via an env variable at runtime, we can not use Next config rewrites ... ⬇️ This is not possible and very sad 😞 (only possible if the env var is set at buildtime, but it makes no sense for this kind of var because we want the same build for any env as said before) // NEXT_PUBLIC_HOST_ENV may equal 'local.' or 'preprod.' or '', at runtime only
{
rewrites: async () => {
return {
beforeFiles: [
{
source: '/:slug/viajes/',
destination: ':/slug/voyage/',
has: [{ type: 'host', value: `www.${process.env.NEXT_PUBLIC_HOST_ENV}example.es` }],
},
{
source: '/travel/:slug/',
destination: ':slug/voyage/',
has: [{ type: 'host', value: `www.${process.env.NEXT_PUBLIC_HOST_ENV}example.com` }],
},
// a lot more...
],
};
}
} Remark
Questions
This is really annoying because Next.js's "buildtime config only" forces us to build a different assets build & Docker Image for each environment we have, this is insane... why would you do that? It is not logic at all to rely on env variables of a build/CI server, instead it should rely on runtime environment variables 😞 Thanks 🙏 |
Beta Was this translation helpful? Give feedback.
-
I also want to add to this saying that not being able to determine locale configuration dynamically in run-time based on e.g. environment variables makes the use of Azure Web App swapping (Staging => Production) impossible. Right now I deploy the app without the use of swapping, which means minutes of downtime for every deployment, and it also takes away the neat feature of rolling back bad deployments by swapping back again. My
I want my NextJS app to be built with the same configuration for the subdomains So what ultimately want to happen here is that I can swap between |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Run
next info
(available from version 12.0.8 and up)Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 20.4.0: Fri Mar 5 01:14:14 PST 2021; root:xnu-7195.101.1~3/RELEASE_X86_64
Binaries:
Node: 16.13.2
npm: 8.1.2
Yarn: 1.22.17
pnpm: 6.29.1
Relevant packages:
next: 12.0.10
react: 17.0.2
react-dom: 17.0.2
What version of Next.js are you using?
12.0.10
What version of Node.js are you using?
16.13.2
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
next start
Describe the Bug
rewrites()
innext.config.js
is only called at build time.IMHO it should be called at run time.
Use Case: If I don't know beforehand the url to where I want to rewrite I can't define it at runtime from an Environment variable.
Expected Behavior
rewrites()
should be called at runtime so that we can have a dynamic rewrite url using an env variable.To Reproduce
put a log in
rewrites()
innext.config.js
and verify that it is only called at build time and not at runtime.Beta Was this translation helpful? Give feedback.
All reactions