From 48b79b618f15aa706966b94ecabf2c44aff44688 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Thu, 27 Jul 2023 16:48:17 +0530 Subject: [PATCH] fix: middleware logic Routing Forms (#10412) * Match /apps/routing_forms * Next.js config.matcher doesnt support spread operator --- apps/web/middleware.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index d877adde6d..602bc1b1e4 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -79,22 +79,22 @@ const routingForms = { handle: (url: URL) => { // Next.config.js Redirects don't handle Client Side navigations and we need that. // So, we add the rewrite here instead. - if (url.pathname.startsWith("/routing-forms/")) { - url.pathname = url.pathname.replace(/^\/routing-forms\//, "/apps/routing-forms/"); + if (url.pathname.startsWith("/routing-forms")) { + url.pathname = url.pathname.replace(/^\/routing-forms($|\/)/, "/apps/routing-forms/"); return NextResponse.rewrite(url); } // Don't 404 old routing_forms links if (url.pathname.startsWith("/apps/routing_forms")) { - url.pathname = url.pathname.replace(/^\/apps\/routing_forms\//, "/apps/routing-forms/"); + url.pathname = url.pathname.replace(/^\/apps\/routing_forms($|\/)/, "/apps/routing-forms/"); return NextResponse.rewrite(url); } }, - // Matcher paths that are required by the handle function to work - paths: ["/apps/routing_forms/:path*", "/routing-forms/:path*"], }; export const config = { + // Next.js Doesn't support spread operator in config matcher, so, we must list all paths explicitly here. + // https://github.com/vercel/next.js/discussions/42458 matcher: [ "/((?!_next|.*avatar.png$|favicon.ico$).*)", "/api/collect-events/:path*", @@ -102,7 +102,12 @@ export const config = { "/:path*/embed", "/api/trpc/:path*", "/auth/login", - ...routingForms.paths, + + /** + * Paths required by routingForms.handle + */ + "/apps/routing_forms/:path*", + "/routing-forms/:path*", ], };