Skip to content

Commit

Permalink
Demo Site: move Admin redirect and DAM rewrite into middleware (#2727)
Browse files Browse the repository at this point in the history
Accessing env vars in next.config.js doesn't work correctly when built image is deployed in different environments.

We don't deploy demo, so this is not an actual issue - still the demo implementation should be as similar as possible to real implementations
  • Loading branch information
nsams authored Nov 13, 2024
1 parent 1d9c70b commit 1e3e7a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
22 changes: 0 additions & 22 deletions demo/site/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ const cometConfig = require("./src/comet-config.json");
* @type {import('next').NextConfig}
**/
const nextConfig = {
async rewrites() {
return [
{
source: "/dam/:path*",
destination: process.env.API_URL + "/dam/:path*",
},
];
},
async redirects() {
const adminUrl = process.env.ADMIN_URL;

if (!adminUrl) {
throw Error("ADMIN_URL is not defined");
}
return [
{
source: "/admin",
destination: adminUrl,
permanent: false,
},
];
},
images: {
deviceSizes: cometConfig.dam.allowedImageSizes,
},
Expand Down
8 changes: 8 additions & 0 deletions demo/site/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export async function middleware(request: NextRequest) {
return NextResponse.rewrite(new URL(predefinedPageRewrite, request.url));
}

if (pathname.startsWith("/dam/")) {
return NextResponse.rewrite(new URL(`${process.env.API_URL_INTERNAL}${request.nextUrl.pathname}`));
}

if (request.nextUrl.pathname === "/admin" && process.env.ADMIN_URL) {
return NextResponse.redirect(new URL(process.env.ADMIN_URL));
}

return NextResponse.next();
}

Expand Down

0 comments on commit 1e3e7a2

Please sign in to comment.