Skip to content

Commit

Permalink
added caching to redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvarty committed Jul 11, 2024
1 parent 8396e42 commit 328e46a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 0 additions & 2 deletions lib/persistant-cache/getCachedObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import fs from 'fs'
import { DateTime } from 'luxon'
import path from 'path'
import { getStore } from "@netlify/blobs";


Expand Down
19 changes: 16 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,26 @@ export async function middleware(request: NextRequest) {
const redirection = await checkRedirect({ path: request.nextUrl.pathname })

if (redirection) {
console.log("redirecting", redirection)
//redirect to the destination url
//cache the redirect for 10 minutes
if (redirection.destinationUrl.startsWith("/")) {
//handle relative paths
const url = request.nextUrl.clone()
url.pathname = redirection.destinationUrl
return NextResponse.redirect(url, redirection.statusCode)
return NextResponse.redirect(url, {
status: redirection.statusCode,
headers: {
"Cache-Control": "public,maxage=600, stale-while-revalidate"
}
})
} else {
return NextResponse.redirect(redirection.destinationUrl, redirection.statusCode)
//handle absolute paths
return NextResponse.redirect(redirection.destinationUrl, {
status: redirection.statusCode,
headers: {
"Cache-Control": "public,maxage=3600, stale-while-revalidate"
}
})
}

} else {
Expand Down

0 comments on commit 328e46a

Please sign in to comment.