Created
October 22, 2024 23:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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