Skip to content

Instantly share code, notes, and snippets.

@leog
Created October 22, 2024 23:52
Show Gist options
  • Select an option

  • Save leog/45a26f9109c3e9c91f87787955eb42a1 to your computer and use it in GitHub Desktop.

Select an option

Save leog/45a26f9109c3e9c91f87787955eb42a1 to your computer and use it in GitHub Desktop.
Support multiple products from a single next.js app based on app router and relying on different hosts
const appConfig = {
prodct1: {
environments: {
production: ["acme.com"],
development: [
"localhost",
"development.acme.com",
"pr-(?<prNumber>.*).acme.com",
],
}
}
}
const environment =
process.env.ENVIRONMENT !== undefined
? process.env.ENVIRONMENT
: process.env.NODE_ENV;
const beforeFiles = Object.keys(appConfig).flatMap((appKey) => {
const app = appConfig[appKey]; // appConfig[product1]
return Object.keys(app.environments)
.filter((env) => env === environment)
.flatMap((env) => {
const domains = app.environments[env];
return domains?.map((domain) => ({
source: "/:path((?!api|auth|_next/static|_next/image|favicon.ico).*)",
has: [
{
type: "host",
value: domain,
},
],
destination: `/products/${appKey.toLowerCase()}/:path(.*)`, // /products/product1/*
}));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment