Files
web/apps/scandic-web/middlewares/cmsContent.ts
Hrishikesh Vaipurkar f23652b929 Merged in feat/BOOK-434-users-should-redirect-to- (pull request #3154)
* feat(BOOK-434): Moved redirect to middleware layer
* feat(BOOK-434): Updated to handle no filters available scenario

Approved-by: Erik Tiekstra
2025-11-14 09:51:44 +00:00

127 lines
4.1 KiB
TypeScript

import { type NextMiddleware, NextResponse } from "next/server"
import { findLang } from "@scandic-hotels/common/utils/languages"
import { removeTrailingSlash } from "@scandic-hotels/common/utils/url"
import { getUidAndContentTypeByPath } from "@scandic-hotels/trpc/cms/getUidAndContentTypeByPath"
import { PageContentTypeEnum } from "@scandic-hotels/trpc/enums/contentType"
import { internalServerError } from "@/server/errors/next"
import { getDefaultRequestHeaders } from "./utils"
import type { MiddlewareMatcher } from "@/types/middleware"
export const middleware: NextMiddleware = async (request) => {
const { nextUrl } = request
const lang = findLang(nextUrl.pathname)!
const pathWithoutTrailingSlash = removeTrailingSlash(nextUrl.pathname)
const isPreview = request.nextUrl.pathname.includes("/preview")
const incomingPathName = isPreview
? pathWithoutTrailingSlash.replace("/preview", "")
: pathWithoutTrailingSlash
const uidAndContent = await getUidAndContentTypeByPath(incomingPathName)
if (uidAndContent.error) {
throw internalServerError(uidAndContent.error)
}
let { contentType, uid } = uidAndContent
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
if (!contentType || !uid) {
// Routes to subpages we need to check if the parent of the incomingPathName exists.
// Then we considered the incomingPathName to be a subpage. These subpages do NOT_LIVE in the CMS.
const incomingPathNameParts = incomingPathName.split("/")
// If the incomingPathName has 2 or more parts, it could possibly be a subpage.
if (incomingPathNameParts.length >= 2) {
const subpage = incomingPathNameParts.pop()
if (subpage) {
const {
contentType: parentContentType,
seoFilters,
uid: parentUid,
} = await getUidAndContentTypeByPath(incomingPathNameParts.join("/"))
if (parentUid) {
switch (parentContentType) {
case PageContentTypeEnum.hotelPage:
// E.g. Dedicated pages for restaurant, parking etc.
searchParams.set("subpage", subpage)
contentType = parentContentType
uid = parentUid
break
case PageContentTypeEnum.destinationCityPage:
case PageContentTypeEnum.destinationCountryPage:
// Validate Seo Filters from CMS and redirect if not found
if (!seoFilters?.includes(subpage)) {
return NextResponse.redirect(
new URL(incomingPathNameParts.join("/"), nextUrl),
{
status: 301,
}
)
}
// E.g. Active Seo filters inside destination pages to filter hotels.
searchParams.set("filterFromUrl", subpage)
contentType = parentContentType
uid = parentUid
break
default:
break
}
}
}
}
}
if (!contentType || !uid) {
const headers = getDefaultRequestHeaders(request)
headers.set("x-continue", "1")
return NextResponse.next({ headers })
}
const headers = getDefaultRequestHeaders(request)
headers.set("x-uid", uid)
headers.set("x-contenttype", contentType)
if (isPreview) {
searchParams.set("isPreview", "true")
return NextResponse.rewrite(
new URL(
`/${lang}/${contentType}/${uid}?${searchParams.toString()}`,
nextUrl
),
{
request: {
headers,
},
}
)
}
return NextResponse.rewrite(
new URL(
`/${lang}/${contentType}/${uid}?${searchParams.toString()}`,
nextUrl
),
{
request: {
headers,
},
}
)
}
export const matcher: MiddlewareMatcher = (request) => {
// Do not process paths with file extension.
// Only looking for dot might be too brute force/give false positives.
// It works right now but adjust accordingly when new use cases/data emerges.
const lastPathnameSegment = request.nextUrl.pathname.split("/").pop()
return lastPathnameSegment?.indexOf(".") === -1
}