Skip to content

Commit 658e81d

Browse files
Enable pass-through requests with unstable_url (#234)
Switch routing redirects and docs loaders to React Router's normalized unstable_url so behavior stays consistent when raw request URLs include data-request internals.
1 parent f77d3cb commit 658e81d

7 files changed

Lines changed: 17 additions & 14 deletions

File tree

app/modules/http-utils/ensure-secure.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { redirect, type MiddlewareFunction } from "react-router";
22

3-
export const ensureSecure: MiddlewareFunction = async ({ request }) => {
3+
export const ensureSecure: MiddlewareFunction = async ({
4+
request,
5+
unstable_url,
6+
}) => {
47
let proto = request.headers.get("x-forwarded-proto");
58
// this indirectly allows `http://localhost` because there is no
69
// "x-forwarded-proto" in the local server headers
710
if (proto === "http") {
8-
let secureUrl = new URL(request.url);
11+
let secureUrl = new URL(unstable_url);
912
secureUrl.protocol = "https:";
1013
throw redirect(secureUrl.toString());
1114
}

app/modules/http-utils/remove-slashes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { redirect, type MiddlewareFunction } from "react-router";
22

33
export const removeTrailingSlashes: MiddlewareFunction = async ({
4-
request,
4+
unstable_url,
55
}) => {
6-
let url = new URL(request.url);
6+
let url = new URL(unstable_url);
77
if (url.pathname.endsWith("/") && url.pathname !== "/") {
88
url.pathname = url.pathname.slice(0, -1);
99
throw redirect(url.toString());

app/modules/redirects/.server/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import { getRedirects } from "./get-redirects";
1313
* /docs/* /api/*
1414
* ```
1515
*
16-
* @param request Web Fetch Request to possibly redirect
16+
* @param unstable_url Normalized location URL (see `future.unstable_passThroughRequests`)
1717
*/
18-
export const handleRedirects: MiddlewareFunction = async ({ request }) => {
18+
export const handleRedirects: MiddlewareFunction = async ({ unstable_url }) => {
1919
let redirects = await getRedirects();
20-
let url = new URL(request.url);
21-
let response = await checkUrl(url.pathname, redirects);
20+
let response = await checkUrl(unstable_url.pathname, redirects);
2221
if (response) throw response;
2322
};

app/pages/doc.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import type { Route } from "./+types/doc";
1414

1515
export { ErrorBoundary } from "~/components/doc-error-boundary";
1616

17-
export async function loader({ request, params }: Route.LoaderArgs) {
18-
let url = new URL(request.url);
17+
export async function loader({ unstable_url, params }: Route.LoaderArgs) {
18+
let url = new URL(unstable_url);
1919
let splat = params["*"] ?? "";
2020

2121
const { ref, slug, githubPath, githubEditPath } = parseDocUrl(url, splat);

app/pages/docs-layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { useCodeBlockCopyButton } from "~/ui/utils";
1515

1616
import docsCss from "~/styles/docs.css?url";
1717

18-
export async function loader({ request, params }: Route.LoaderArgs) {
19-
let url = new URL(request.url);
18+
export async function loader({ unstable_url, params }: Route.LoaderArgs) {
19+
let url = new URL(unstable_url);
2020
if (!url.pathname.endsWith("/")) {
2121
url.pathname += "/";
2222
}

app/pages/redirect-major-version.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { getRepoTags } from "~/modules/gh-docs/.server";
33
import * as semver from "semver";
44
import type { Route } from "./+types/redirect-major-version";
55

6-
export async function loader({ request }: Route.LoaderArgs) {
7-
let url = new URL(request.url);
6+
export async function loader({ unstable_url }: Route.LoaderArgs) {
7+
let url = new URL(unstable_url);
88
let [, s, ...rest] = url.pathname.split("/");
99

1010
let versions = await getRepoTags();

react-router.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Config } from "@react-router/dev/config";
33
export default {
44
future: {
55
unstable_optimizeDeps: true,
6+
unstable_passThroughRequests: true,
67
v8_splitRouteModules: "enforce",
78
v8_middleware: true,
89
v8_viteEnvironmentApi: true,

0 commit comments

Comments
 (0)