Merged in feat/SW-2111 (pull request #1761)
feat(SW-2111): add initial scandic-redirect * feat(SW-2111): add initial scandic-redirect * feat(SW-2112): add scandic-redirect call to middleware * chore: add redirect jsons per lang * fix: handle incorrect contentTypes * fix: handle lang * refactor: add json streaming * refactor: wrap redirect call in cacheOrGet * refactor: review Approved-by: Michael Zetterberg
This commit is contained in:
@@ -30,10 +30,18 @@ function getBreadcrumbsVariantsByContentType(
|
||||
}
|
||||
}
|
||||
|
||||
// We need to return null for both ignored content types and non-existent content types
|
||||
// In order to no having to maintain an explicit array of all content types besides the PageContentTypeEnum we convert it here.
|
||||
const allowedContentTypes = Object.keys(PageContentTypeEnum)
|
||||
.map((key) => {
|
||||
return PageContentTypeEnum[key as keyof typeof PageContentTypeEnum]
|
||||
})
|
||||
.filter((c) => !IGNORED_CONTENT_TYPES.includes(c))
|
||||
|
||||
export default function PageBreadcrumbs({
|
||||
params,
|
||||
}: PageArgs<LangParams & ContentTypeParams>) {
|
||||
if (IGNORED_CONTENT_TYPES.includes(params.contentType)) {
|
||||
if (!allowedContentTypes.includes(params.contentType)) {
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { notFound } from "next/navigation"
|
||||
|
||||
import styles from "./layout.module.css"
|
||||
|
||||
import type {
|
||||
@@ -6,17 +8,24 @@ import type {
|
||||
LayoutArgs,
|
||||
UIDParams,
|
||||
} from "@/types/params"
|
||||
import { PageContentTypeEnum } from "@/types/requests/contentType"
|
||||
|
||||
export default function ContentTypeLayout({
|
||||
breadcrumbs,
|
||||
preview,
|
||||
children,
|
||||
params,
|
||||
}: React.PropsWithChildren<
|
||||
LayoutArgs<LangParams & ContentTypeParams & UIDParams> & {
|
||||
breadcrumbs: React.ReactNode
|
||||
preview: React.ReactNode
|
||||
}
|
||||
>) {
|
||||
// Would like a better way to check if the contentType is valid.
|
||||
// Perhaps a case for using an `{} as const` object for PageContentTypes instead?
|
||||
if (!Object.values(PageContentTypeEnum).includes(params.contentType)) {
|
||||
notFound()
|
||||
}
|
||||
return (
|
||||
<div className={styles.container}>
|
||||
<section className={styles.layout}>
|
||||
|
||||
@@ -12,6 +12,7 @@ import * as currentWebLogout from "@/middlewares/currentWebLogout"
|
||||
import * as dateFormat from "@/middlewares/dateFormat"
|
||||
import * as handleAuth from "@/middlewares/handleAuth"
|
||||
import * as myPages from "@/middlewares/myPages"
|
||||
import * as redirect from "@/middlewares/redirect"
|
||||
import * as sasXScandic from "@/middlewares/sasXScandic"
|
||||
import { getDefaultRequestHeaders } from "@/middlewares/utils"
|
||||
import * as webView from "@/middlewares/webView"
|
||||
@@ -58,6 +59,7 @@ export const middleware: NextMiddleware = async (request, event) => {
|
||||
bookingFlow,
|
||||
sasXScandic,
|
||||
cmsContent,
|
||||
redirect,
|
||||
]
|
||||
|
||||
try {
|
||||
|
||||
@@ -63,9 +63,10 @@ export const middleware: NextMiddleware = async (request) => {
|
||||
}
|
||||
|
||||
if (!contentType || !uid) {
|
||||
throw notFound(
|
||||
`Unable to resolve CMS entry for page "${pathWithoutTrailingSlash}"`
|
||||
)
|
||||
const headers = getDefaultRequestHeaders(request)
|
||||
headers.set("x-continue", "1")
|
||||
|
||||
return NextResponse.next({ headers })
|
||||
}
|
||||
const headers = getDefaultRequestHeaders(request)
|
||||
headers.set("x-uid", uid)
|
||||
|
||||
69
apps/scandic-web/middlewares/redirect.ts
Normal file
69
apps/scandic-web/middlewares/redirect.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { type NextMiddleware, NextResponse } from "next/server"
|
||||
|
||||
import { notFound } from "@/server/errors/next"
|
||||
|
||||
import { getCacheClient } from "@/services/dataCache"
|
||||
import { findLang } from "@/utils/languages"
|
||||
|
||||
import { getDefaultRequestHeaders } from "./utils"
|
||||
|
||||
import type { MiddlewareMatcher } from "@/types/middleware"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (matchedRedirect.ok) {
|
||||
const result = await matchedRedirect.text()
|
||||
|
||||
if (result) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return null
|
||||
},
|
||||
// longer once tested
|
||||
"1m"
|
||||
)
|
||||
}
|
||||
|
||||
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, request.nextUrl)
|
||||
headers.set("Cache-control", "public, max-age=60")
|
||||
return NextResponse.redirect(newUrl, {
|
||||
headers,
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Redirect error: ", e)
|
||||
throw notFound()
|
||||
}
|
||||
}
|
||||
|
||||
export const matcher: MiddlewareMatcher = (_) => {
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user