From 8ac6aa40c76f50c5a117c2d71c3ade610b7ede60 Mon Sep 17 00:00:00 2001 From: Aiji Uejima Date: Sun, 24 Apr 2022 21:00:00 +0900 Subject: [PATCH] fix: bug of relative urls --- src/handle-fallback.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/handle-fallback.ts b/src/handle-fallback.ts index e45ecb1..354e59c 100644 --- a/src/handle-fallback.ts +++ b/src/handle-fallback.ts @@ -9,9 +9,15 @@ export const handleFallback = ( ): ReturnType => { if (typeof fallback === 'function') return fallback(request, event) if (request.preflight) return new NextResponse(null) - if (fallback.type === 'rewrite') - return NextResponse.rewrite(fallback.destination) + if (fallback.type === 'rewrite') { + const url = request.nextUrl.clone() + url.pathname = fallback.destination + return NextResponse.rewrite(url) + } - if (request.nextUrl.pathname !== fallback.destination) - return NextResponse.redirect(fallback.destination, fallback.statusCode) + if (request.nextUrl.pathname !== fallback.destination) { + const url = request.nextUrl.clone() + url.pathname = fallback.destination + return NextResponse.redirect(url, fallback.statusCode) + } }