fix: dateFormat normalization middleware

This commit is contained in:
Christel Westerberg
2024-11-28 15:44:17 +01:00
parent 528107e0ef
commit 6bc7e27908
3 changed files with 25 additions and 11 deletions

View File

@@ -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 = [ const middlewares = [
currentWebLogin, currentWebLogin,
currentWebLoginEmail, currentWebLoginEmail,
@@ -51,9 +52,9 @@ export const middleware: NextMiddleware = async (request, event) => {
handleAuth, handleAuth,
myPages, myPages,
webView, webView,
dateFormat,
bookingFlow, bookingFlow,
cmsContent, cmsContent,
dateFormat,
] ]
try { try {

View File

@@ -7,6 +7,7 @@ import { MiddlewareMatcher } from "@/types/middleware"
YYYY-MM-D and YYYY-MM-DD since the current web uses YYYY-MM-D YYYY-MM-D and YYYY-MM-DD since the current web uses YYYY-MM-D
in the URL as parameters (toDate and fromDate) 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 { function normalizeDate(date: string): string {
const datePattern = /^\d{4}-\d{1,2}-\d{1,2}$/ const datePattern = /^\d{4}-\d{1,2}-\d{1,2}$/
@@ -18,20 +19,32 @@ function normalizeDate(date: string): string {
} }
export const middleware: NextMiddleware = (request) => { export const middleware: NextMiddleware = (request) => {
const url = request.nextUrl.clone() const url = request.nextUrl
const { searchParams } = url 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")! 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")! 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) => { export const matcher: MiddlewareMatcher = (request) => {

View File

@@ -136,7 +136,7 @@ const nextConfig = {
{ {
key: "fromDate", key: "fromDate",
type: "query", 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, permanent: false,
@@ -168,7 +168,7 @@ const nextConfig = {
{ {
key: "toDate", key: "toDate",
type: "query", 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, permanent: false,