From 3f192488ebfd072472125ce3d58f20963faa4dab Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 17 Jan 2023 12:54:04 -0500 Subject: [PATCH] Support OPTIONS requests in staticHandler.queryRoute (#9914) --- .changeset/real-panthers-hear.md | 5 +++++ packages/router/__tests__/router-test.ts | 12 ++++++++++-- packages/router/router.ts | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .changeset/real-panthers-hear.md diff --git a/.changeset/real-panthers-hear.md b/.changeset/real-panthers-hear.md new file mode 100644 index 0000000000..1a992cccee --- /dev/null +++ b/.changeset/real-panthers-hear.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Support `OPTIONS` requests in `staticHandler.queryRoute` diff --git a/packages/router/__tests__/router-test.ts b/packages/router/__tests__/router-test.ts index 847312f7c3..043015aef1 100644 --- a/packages/router/__tests__/router-test.ts +++ b/packages/router/__tests__/router-test.ts @@ -12518,6 +12518,14 @@ describe("a router", () => { expect(data).toBe("PARENT LOADER"); }); + it("should support OPTIONS requests", async () => { + let { queryRoute } = createStaticHandler(SSR_ROUTES); + let data = await queryRoute( + createRequest("/parent", { method: "OPTIONS" }) + ); + expect(data).toBe("PARENT LOADER"); + }); + it("should support singular route load navigations (primitives)", async () => { let { queryRoute } = createStaticHandler(SSR_ROUTES); let data; @@ -13276,7 +13284,7 @@ describe("a router", () => { it("should handle unsupported methods with a 405 Response", async () => { try { - await queryRoute(createRequest("/", { method: "OPTIONS" }), { + await queryRoute(createRequest("/", { method: "TRACE" }), { routeId: "root", }); expect(false).toBe(true); @@ -13284,7 +13292,7 @@ describe("a router", () => { expect(isRouteErrorResponse(data)).toBe(true); expect(data.status).toBe(405); expect(data.error).toEqual( - new Error('Invalid request method "OPTIONS"') + new Error('Invalid request method "TRACE"') ); expect(data.internal).toBe(true); } diff --git a/packages/router/router.ts b/packages/router/router.ts index a30160096d..afe810b844 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -2421,7 +2421,7 @@ export function createStaticHandler( let matches = matchRoutes(dataRoutes, location, basename); // SSR supports HEAD requests while SPA doesn't - if (!isValidMethod(method) && method !== "head") { + if (!isValidMethod(method) && method !== "head" && method !== "options") { throw getInternalRouterError(405, { method }); } else if (!matches) { throw getInternalRouterError(404, { pathname: location.pathname });