Merged in chore/redirect-counter (pull request #3302)
Counter name is now searchable and add counter for redirects * refactor: createCounter() only takes one argument, the name of the counter. Makes it easier to search for * feat: add counter when we do a redirect from redirect-service Approved-by: Linus Flood
This commit is contained in:
@@ -2,6 +2,7 @@ import { type NextMiddleware, NextResponse } from "next/server"
|
||||
|
||||
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
||||
import { logger } from "@scandic-hotels/common/logger"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
import { findLang } from "@scandic-hotels/common/utils/languages"
|
||||
|
||||
import { notFound } from "@/server/errors/next"
|
||||
@@ -45,7 +46,7 @@ async function fetchAndCacheRedirect(lang: Lang, pathname: string) {
|
||||
"1d"
|
||||
)
|
||||
}
|
||||
|
||||
const redirectCounter = createCounter("middleware.redirect")
|
||||
export const middleware: NextMiddleware = async (request) => {
|
||||
const lang = findLang(request.nextUrl.pathname)!
|
||||
const headers = getDefaultRequestHeaders(request)
|
||||
@@ -56,6 +57,14 @@ export const middleware: NextMiddleware = async (request) => {
|
||||
)
|
||||
|
||||
if (matchedRedirect) {
|
||||
redirectCounter
|
||||
.init({
|
||||
lang,
|
||||
pathname: request.nextUrl.pathname,
|
||||
newPathname: matchedRedirect,
|
||||
})
|
||||
.success()
|
||||
|
||||
const newUrl = new URL(matchedRedirect, getPublicNextURL(request))
|
||||
headers.set("Cache-control", "public, max-age=14400") // 4 hours
|
||||
return NextResponse.redirect(newUrl, {
|
||||
|
||||
Reference in New Issue
Block a user