feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import { type NextMiddleware, NextResponse } from "next/server"
|
|
|
|
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
|
import { findLang } from "@scandic-hotels/common/utils/languages"
|
|
|
|
import { notFound } from "@/server/errors/next"
|
|
import { getPublicNextURL } from "@/server/utils"
|
|
|
|
import { getDefaultRequestHeaders } from "./utils"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import type { MiddlewareMatcher } from "@/types/middleware"
|
|
|
|
async function fetchAndCacheRedirect(lang: Lang, pathname: string) {
|
|
const cacheKey = `${lang}:redirect:${pathname}`
|
|
const cache = await getCacheClient()
|
|
|
|
return await cache.cacheOrGet(
|
|
cacheKey,
|
|
async () => {
|
|
const matchedRedirect = await fetch(
|
|
"https://redirect-scandic-hotels.netlify.app",
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({ lang, pathname }),
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
signal: AbortSignal.timeout(15_000),
|
|
}
|
|
)
|
|
|
|
if (matchedRedirect.ok) {
|
|
const result = await matchedRedirect.text()
|
|
|
|
if (result) {
|
|
return result
|
|
}
|
|
}
|
|
return null
|
|
},
|
|
// longer once tested
|
|
"1d"
|
|
)
|
|
}
|
|
|
|
export const middleware: NextMiddleware = async (request) => {
|
|
const lang = findLang(request.nextUrl.pathname)!
|
|
const headers = getDefaultRequestHeaders(request)
|
|
try {
|
|
const matchedRedirect = await fetchAndCacheRedirect(
|
|
lang,
|
|
request.nextUrl.pathname
|
|
)
|
|
|
|
if (matchedRedirect) {
|
|
const newUrl = new URL(matchedRedirect, getPublicNextURL(request))
|
|
headers.set("Cache-control", "public, max-age=14400") // 4 hours
|
|
return NextResponse.redirect(newUrl, {
|
|
headers,
|
|
status: 308,
|
|
})
|
|
}
|
|
headers.set("x-continue", "1")
|
|
return NextResponse.next({ headers })
|
|
} catch (e) {
|
|
console.error("Redirect error: ", e)
|
|
throw notFound()
|
|
}
|
|
}
|
|
|
|
export const matcher: MiddlewareMatcher = (_) => {
|
|
return true
|
|
}
|