Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ export const config = {

export async function middleware(request: NextRequest) {
const host = (await headers()).get("host");

const pathname = request.nextUrl.pathname;

const searchParams = request.nextUrl.searchParams;
const paramsString =
searchParams.size > 0 ? `?${request.nextUrl.searchParams}` : "";

if (
process.env.NODE_ENV === "production" &&
(await headers()).get("x-forwarded-proto") !== "https" &&
!host?.includes("localhost")
) {
return NextResponse.redirect(
`https://${host}${request.nextUrl.pathname}`,
`https://${host}${pathname}${paramsString}`,
301,
);
}

const pathname = request.nextUrl.pathname;

if (pathname.startsWith("/permalink")) {
const permalink = pathname.split("/")?.[2];

Expand All @@ -34,7 +39,7 @@ export async function middleware(request: NextRequest) {
kind,
)}/${permalinkableSlug}`;
return NextResponse.rewrite(
new URL(`/dynamic${entityPath}`, request.url),
new URL(`/dynamic${entityPath}${paramsString}`, request.url),
);
}
}
Expand All @@ -43,5 +48,7 @@ export async function middleware(request: NextRequest) {
// Because we need runtime env vars, we need to avoid generating any pages at
// buildtime. This (or any) top-level dyanmic segment ensures we opt all
// routes out of Next's buildtime generation.
return NextResponse.rewrite(new URL(`/dynamic${pathname}`, request.url));
return NextResponse.rewrite(
new URL(`/dynamic${pathname}${paramsString}`, request.url),
);
}
Loading