mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(site): send every unhandled method a 405 listing what the address takes (#63104)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user