From 20dcebb9a8d3acdb69572d2d5a4aabdc535a2031 Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Wed, 4 Sep 2024 15:43:18 -0300 Subject: [PATCH 1/2] Allow users to specify search params on route path Signed-off-by: Marcos Candeia --- website/handlers/router.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/website/handlers/router.ts b/website/handlers/router.ts index 02746ec04..b5104396e 100644 --- a/website/handlers/router.ts +++ b/website/handlers/router.ts @@ -99,8 +99,16 @@ export const router = ( } for (const { pathTemplate: routePath, handler } of routes) { + let url; + if (URL.canParse(routePath)) { + url = new URL(routePath); + } else { + url = new URL(routePath, "http://localhost:8000"); + } + const pattern = urlPatternCache[routePath] ??= new URLPattern({ - pathname: routePath, + pathname: url.pathname, + search: url.search, }); const res = pattern.exec(req.url); const groups = res?.pathname.groups ?? {}; From 19a0e61a74d122879f5bd03965e1370cbf65b724 Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Wed, 4 Sep 2024 21:34:08 -0300 Subject: [PATCH 2/2] Avoid building url for all routing request Signed-off-by: Marcos Candeia --- website/handlers/router.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/website/handlers/router.ts b/website/handlers/router.ts index b5104396e..f1521bd82 100644 --- a/website/handlers/router.ts +++ b/website/handlers/router.ts @@ -99,17 +99,18 @@ export const router = ( } for (const { pathTemplate: routePath, handler } of routes) { - let url; - if (URL.canParse(routePath)) { - url = new URL(routePath); - } else { - url = new URL(routePath, "http://localhost:8000"); - } - - const pattern = urlPatternCache[routePath] ??= new URLPattern({ - pathname: url.pathname, - search: url.search, - }); + const pattern = urlPatternCache[routePath] ??= (() => { + let url; + if (URL.canParse(routePath)) { + url = new URL(routePath); + } else { + url = new URL(routePath, "http://localhost:8000"); + } + return new URLPattern({ + pathname: url.pathname, + search: url.search, + }); + })(); const res = pattern.exec(req.url); const groups = res?.pathname.groups ?? {};