From d4aeee551ebf554f37803b57618926c828a70b39 Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Tue, 10 Nov 2026 06:39:29 -0600 Subject: [PATCH] fix(site): send every unhandled method a 405 listing what the address takes (#63104) --- services/site/src/static/render.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/services/site/src/static/render.tsx b/services/site/src/static/render.tsx index fa615287289..c5eaa3b26ac 100644 --- a/services/site/src/static/render.tsx +++ b/services/site/src/static/render.tsx @@ -42,6 +42,14 @@ const answered = (path: string, method: string) => !route.page && (route[`$${method}`] || (method === "HEAD" && route.$GET)) && matches(route.path, path), ) +/** The methods an address takes: an API route's handlers (HEAD with GET), else a page's GET and HEAD. */ +const allowed = (path: string) => { + const methods = pages + .filter((route) => !route.page && matches(route.path, path)) + .flatMap((route) => Object.keys(route).flatMap((key) => (/^\$[A-Z]+$/.test(key) ? [key.slice(1)] : []))) + return [...new Set(methods.includes("GET") || !methods.length ? [...methods, "GET", "HEAD"] : methods)].join(", ") +} + /** * Production only: the static routes (routes.ts) as plain HTML with no hydration, and the not-found * page (`[...404].tsx`, with its 404 status) for every address no route answers. The page renders @@ -56,12 +64,12 @@ export const staticPages: Middleware = async (_event, next) => { if (import.meta.env.DEV || !event) return next() const url = new URL(event.request.url) const path = strip(url.pathname) - // Pages only answer GET and HEAD. Another method on an address no API route or server function serves gets a - // 405, not the app's document, whose scripts production no longer ships. + // Pages only answer GET and HEAD. A method nothing handles gets a 405 listing the methods the address takes, + // not the app's document, whose scripts production no longer ships. if (event.request.method !== "GET" && event.request.method !== "HEAD") - return path.startsWith("/_server") || pages.some((route) => !route.page && matches(route.path, path)) + return answered(path, event.request.method) ? next() - : new Response(null, { status: 405, headers: { Allow: "GET, HEAD" } }) + : new Response(null, { status: 405, headers: { Allow: allowed(path) } }) const pattern = staticPath(path) ?? (answered(path, event.request.method) ? undefined : "/*404") const load = pattern && pages.find((route) => route.page && route.path.replace(/(.)\/$/, "$1") === pattern)?.$component?.import