From ca93046aadd740be7ac73530e7e687ebf45bbfc9 Mon Sep 17 00:00:00 2001 From: Erik Tiekstra Date: Mon, 17 Mar 2025 07:17:08 +0000 Subject: [PATCH] feat/SW-550 sitemap route * feat(SW-550): Added rewrites to handle sitemap paths * feat(SW-550): Added sitemap-index generation * feat(SW-550): Added sitemap xml file generation * feat(SW-550): Added feature flag 'HIDE_FOR_NEXT_RELEASE' to sitemap routes Approved-by: Linus Flood --- .gitignore | 5 +- apps/scandic-web/app/api/sitemap/utils.ts | 8 +-- .../app/sitemap/[sitemapId]/route.ts | 50 +++++++++++++++++++ apps/scandic-web/app/sitemap/route.ts | 29 +++++++++++ apps/scandic-web/next.config.js | 8 +++ apps/scandic-web/types/sitemap.ts | 2 +- 6 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 apps/scandic-web/app/sitemap/[sitemapId]/route.ts create mode 100644 apps/scandic-web/app/sitemap/route.ts diff --git a/.gitignore b/.gitignore index 74f3e9c3c..0eae0acc0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,7 @@ node_modules .yarn/releases # Turbo -.turbo \ No newline at end of file +.turbo + +# Local Netlify folder +.netlify diff --git a/apps/scandic-web/app/api/sitemap/utils.ts b/apps/scandic-web/app/api/sitemap/utils.ts index 05844ff70..853058a3b 100644 --- a/apps/scandic-web/app/api/sitemap/utils.ts +++ b/apps/scandic-web/app/api/sitemap/utils.ts @@ -1,6 +1,8 @@ import { Lang } from "@/constants/languages" import { env } from "@/env/server" +import { removeTrailingSlash } from "@/utils/url" + import { mergeEntriesCounter, mergeEntriesSuccessCounter, @@ -114,13 +116,11 @@ function mapEntriesToSitemapEntry(entries: SyncItem[]) { if (mainEntry) { const { locale, url } = mainEntry.data const sitemapEntry: SitemapEntry = { - url: `${env.PUBLIC_URL}/${locale}${url}`, + url: removeTrailingSlash(`${env.PUBLIC_URL}/${locale}${url}`), lastModified, changeFrequency, priority, - } - if (alternates) { - sitemapEntry.alternates = alternates + alternates, } return sitemapEntry } diff --git a/apps/scandic-web/app/sitemap/[sitemapId]/route.ts b/apps/scandic-web/app/sitemap/[sitemapId]/route.ts new file mode 100644 index 000000000..1af4f382f --- /dev/null +++ b/apps/scandic-web/app/sitemap/[sitemapId]/route.ts @@ -0,0 +1,50 @@ +import { notFound } from "next/navigation" + +import { env } from "@/env/server" + +import { getSitemapDataById } from "@/utils/sitemap" + +import type { NextRequest } from "next/server" + +export const dynamic = "force-dynamic" + +export async function GET( + _request: NextRequest, + context: { params: { sitemapId: string } } +) { + if (env.HIDE_FOR_NEXT_RELEASE) { + notFound() + } + + const sitemapId = context.params.sitemapId + if (!sitemapId) { + notFound() + } + + const sitemapData = await getSitemapDataById(Number(sitemapId)) + if (!sitemapData.length) { + notFound() + } + + const entries = sitemapData.map((entry) => { + const alternates = Object.entries(entry.alternates).map( + ([lang, url]) => + `` + ) + + return ` + + ${entry.url} + ${entry.lastModified} + ${entry.changeFrequency} + ${entry.priority} + ${alternates.join("")} +` + }) + + const sitemapXML = `\n${entries.join("")}\n` + + return new Response(sitemapXML, { + headers: { "Content-Type": "text/xml" }, + }) +} diff --git a/apps/scandic-web/app/sitemap/route.ts b/apps/scandic-web/app/sitemap/route.ts new file mode 100644 index 000000000..84e9d484d --- /dev/null +++ b/apps/scandic-web/app/sitemap/route.ts @@ -0,0 +1,29 @@ +import { notFound } from "next/navigation" + +import { env } from "@/env/server" + +import { getLastUpdated, getSitemapIds } from "@/utils/sitemap" + +export const dynamic = "force-dynamic" + +export async function GET() { + if (env.HIDE_FOR_NEXT_RELEASE) { + notFound() + } + + const lastUpdated = await getLastUpdated() + const sitemaps = await getSitemapIds() + + const urls = sitemaps.map( + (id) => ` + ${env.PUBLIC_URL}/sitemap-${id}.xml + ${lastUpdated} +` + ) + + const sitemapIndexXML = `\n${urls.join("")}\n` + + return new Response(sitemapIndexXML, { + headers: { "Content-Type": "text/xml" }, + }) +} diff --git a/apps/scandic-web/next.config.js b/apps/scandic-web/next.config.js index cb0ef944a..91d6de58d 100644 --- a/apps/scandic-web/next.config.js +++ b/apps/scandic-web/next.config.js @@ -308,6 +308,14 @@ const nextConfig = { source: findMyBooking.sv, destination: "/sv/hotelreservation/get-booking", }, + { + source: `/sitemap-:id(\\d{1,}).xml`, + destination: `/sitemap/:id`, + }, + { + source: `/sitemap-index.xml`, + destination: `/sitemap`, + }, ], } }, diff --git a/apps/scandic-web/types/sitemap.ts b/apps/scandic-web/types/sitemap.ts index 94068135f..e3053456f 100644 --- a/apps/scandic-web/types/sitemap.ts +++ b/apps/scandic-web/types/sitemap.ts @@ -16,7 +16,7 @@ export interface SitemapEntry { lastModified: string changeFrequency: ChangeFrequency priority: number - alternates?: Partial> + alternates: Partial> } export type SitemapData = SitemapEntry[]