From 6bc7e27908a8c8575b7b26e7d7b603a348233bd6 Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Thu, 28 Nov 2024 15:44:17 +0100 Subject: [PATCH] fix: dateFormat normalization middleware --- middleware.ts | 3 ++- middlewares/dateFormat.ts | 29 +++++++++++++++++++++-------- next.config.js | 4 ++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/middleware.ts b/middleware.ts index 27b4eca97..58f151dab 100644 --- a/middleware.ts +++ b/middleware.ts @@ -43,6 +43,7 @@ export const middleware: NextMiddleware = async (request, event) => { ) } + // Note that the order of middlewares is important since that is the order they are matched by. const middlewares = [ currentWebLogin, currentWebLoginEmail, @@ -51,9 +52,9 @@ export const middleware: NextMiddleware = async (request, event) => { handleAuth, myPages, webView, + dateFormat, bookingFlow, cmsContent, - dateFormat, ] try { diff --git a/middlewares/dateFormat.ts b/middlewares/dateFormat.ts index b1ee329f1..d2f3c584f 100644 --- a/middlewares/dateFormat.ts +++ b/middlewares/dateFormat.ts @@ -7,6 +7,7 @@ import { MiddlewareMatcher } from "@/types/middleware" YYYY-MM-D and YYYY-MM-DD since the current web uses YYYY-MM-D in the URL as parameters (toDate and fromDate) */ +const legacyDatePattern = /^([12]\d{3}-(0[1-9]|1[0-2])-([1-9]))$/ function normalizeDate(date: string): string { const datePattern = /^\d{4}-\d{1,2}-\d{1,2}$/ @@ -18,20 +19,32 @@ function normalizeDate(date: string): string { } export const middleware: NextMiddleware = (request) => { - const url = request.nextUrl.clone() + const url = request.nextUrl const { searchParams } = url - if (searchParams.has("fromDate")) { + if ( + !!( + searchParams.has("fromDate") && + searchParams.get("fromDate")!.match(legacyDatePattern) + ) || + !!( + searchParams.has("toDate") && + searchParams.get("toDate")!.match(legacyDatePattern) + ) + ) { const fromDate = searchParams.get("fromDate")! - searchParams.set("fromDate", normalizeDate(fromDate)) - } + url.searchParams.set("fromDate", normalizeDate(fromDate)) - if (searchParams.has("toDate")) { const toDate = searchParams.get("toDate")! - searchParams.set("toDate", normalizeDate(toDate)) + url.searchParams.set("toDate", normalizeDate(toDate)) + return NextResponse.redirect(url) + } else { + const headers = new Headers(request.headers) + headers.set("x-continue", "1") + return NextResponse.next({ + headers, + }) } - - return NextResponse.rewrite(url) } export const matcher: MiddlewareMatcher = (request) => { diff --git a/next.config.js b/next.config.js index 72121b713..c73b08464 100644 --- a/next.config.js +++ b/next.config.js @@ -136,7 +136,7 @@ const nextConfig = { { key: "fromDate", type: "query", - value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))$", + value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0?[1-9]|[12]\\d|3[01]))$", }, ], permanent: false, @@ -168,7 +168,7 @@ const nextConfig = { { key: "toDate", type: "query", - value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))$", + value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0?[1-9]|[12]\\d|3[01]))$", }, ], permanent: false,