Skip to content

Commit

Permalink
feat: add faststore-redirects-time when the redirect not found
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromtec committed Oct 23, 2024
1 parent 80c972b commit 353fc3e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down

0 comments on commit 353fc3e

Please sign in to comment.