Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OPTIONS requests in staticHandler.queryRoute #9914

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-panthers-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Support `OPTIONS` requests in `staticHandler.queryRoute`
12 changes: 10 additions & 2 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -13276,15 +13284,15 @@ 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);
} catch (data) {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down