From 353fc3e2076c8248925c9ac0af3a883e34bc21d6 Mon Sep 17 00:00:00 2001 From: Pedro Soares Date: Wed, 23 Oct 2024 17:16:58 -0300 Subject: [PATCH] feat: add faststore-redirects-time when the redirect not found --- packages/core/src/middleware.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/core/src/middleware.ts b/packages/core/src/middleware.ts index 2e15aff439..5a72187552 100644 --- a/packages/core/src/middleware.ts +++ b/packages/core/src/middleware.ts @@ -6,13 +6,13 @@ import storeConfig from 'discovery.config.default' const redirectsClient = new RedirectsClient() //cache-control: max-age=300, stale-while-revalidate=31536000 -// faststore-redirects-time: 10 +//faststore-redirects-time: 10 export async function middleware(request: NextRequest) { const startTime = Date.now() const { pathname } = request.nextUrl const redirect = await redirectsClient.get(pathname) - console.log('redirect response: ', redirect) + if (redirect) { const pathnameToRedirect = redirect.to const redirectUrl = storeConfig.storeUrl + pathnameToRedirect @@ -33,7 +33,13 @@ export async function middleware(request: NextRequest) { return response } - return NextResponse.next() + const endTime = Date.now() + const executionTime = endTime - startTime + + const response = NextResponse.next() + + response.headers.set('faststore-redirects-time', executionTime.toString()) + return response } export const config = {