Merged in feat/SW-1296-hotel-subpages (pull request #1233)
feat(SW-1296): added Subpage for hotel pages and its routing * feat(SW-1296): added Subpage for hotel pages and its routing Approved-by: Fredrik Thorsson
This commit is contained in:
@@ -8,6 +8,7 @@ import { removeTrailingSlash } from "@/utils/url"
|
||||
import { fetchAndCacheEntry, getDefaultRequestHeaders } from "./utils"
|
||||
|
||||
import type { MiddlewareMatcher } from "@/types/middleware"
|
||||
import { PageContentTypeEnum } from "@/types/requests/contentType"
|
||||
|
||||
export const middleware: NextMiddleware = async (request) => {
|
||||
const { nextUrl } = request
|
||||
@@ -19,13 +20,44 @@ export const middleware: NextMiddleware = async (request) => {
|
||||
const isPreview = request.nextUrl.pathname.includes("/preview")
|
||||
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
|
||||
|
||||
const { contentType, uid } = await fetchAndCacheEntry(
|
||||
let { contentType, uid } = await fetchAndCacheEntry(
|
||||
isPreview
|
||||
? contentTypePathName.replace("/preview", "")
|
||||
: contentTypePathName,
|
||||
lang
|
||||
)
|
||||
|
||||
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 incomingPathName = isPreview
|
||||
? contentTypePathName.replace("/preview", "")
|
||||
: contentTypePathName
|
||||
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 parentPageResult = await fetchAndCacheEntry(
|
||||
incomingPathNameParts.join("/"),
|
||||
lang
|
||||
)
|
||||
|
||||
if (parentPageResult.uid) {
|
||||
switch (parentPageResult.contentType) {
|
||||
case PageContentTypeEnum.hotelPage:
|
||||
// E.g. Dedicated pages for restaurant, parking etc.
|
||||
contentType = parentPageResult.contentType
|
||||
uid = parentPageResult.uid
|
||||
searchParams.set("subpage", subpage)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!contentType || !uid) {
|
||||
throw notFound(
|
||||
`Unable to resolve CMS entry for locale "${lang}": ${contentTypePathName}`
|
||||
|
||||
Reference in New Issue
Block a user