Skip to content

Commit 564b074

Browse files
committed
Root middlewares
1 parent 8094099 commit 564b074

File tree

8 files changed

+2689
-265
lines changed

8 files changed

+2689
-265
lines changed

app/http.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import { ensureSecure } from "~/modules/http-utils/ensure-secure";
2-
import { handleRedirects } from "~/modules/redirects/.server";
3-
import { removeTrailingSlashes } from "~/modules/http-utils/remove-slashes";
4-
51
export const CACHE_CONTROL = {
62
// what we use for docs so they are up-to-date within minutes of what's on
73
// github
@@ -10,9 +6,3 @@ export const CACHE_CONTROL = {
106
// don't cache at all
117
none: "no-store, no-cache, must-revalidate, max-age=0",
128
};
13-
14-
export async function middlewares(request: Request): Promise<void> {
15-
await ensureSecure(request);
16-
await removeTrailingSlashes(request);
17-
await handleRedirects(request);
18-
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { redirect } from "react-router";
2+
import type { Route } from "../../+types/root";
23

3-
export async function ensureSecure(request: Request) {
4+
export const ensureSecure: Route.unstable_MiddlewareFunction = async (
5+
{ request },
6+
next
7+
) => {
48
let proto = request.headers.get("x-forwarded-proto");
59
// this indirectly allows `http://localhost` because there is no
610
// "x-forwarded-proto" in the local server headers
@@ -9,4 +13,7 @@ export async function ensureSecure(request: Request) {
913
secureUrl.protocol = "https:";
1014
throw redirect(secureUrl.toString());
1115
}
12-
}
16+
// FIXME: Update RR to auto-bubble the internal response if `next` isn't
17+
// called so we can omit the return value
18+
return next();
19+
};
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { redirect } from "react-router";
2+
import type { Route } from "../../+types/root";
23

3-
export async function removeTrailingSlashes(request: Request) {
4+
export const removeTrailingSlashes: Route.unstable_MiddlewareFunction = async (
5+
{ request },
6+
next
7+
) => {
48
let url = new URL(request.url);
59
if (url.pathname.endsWith("/") && url.pathname !== "/") {
610
url.pathname = url.pathname.slice(0, -1);
711
throw redirect(url.toString());
812
}
9-
}
13+
// FIXME: Update RR to auto-bubble the internal response if `next` isn't
14+
// called so we can omit the return value
15+
return next();
16+
};

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Route } from "../../../+types/root";
12
import { checkUrl } from "./check-url";
23
import { getRedirects } from "./get-redirects";
34

@@ -14,9 +15,15 @@ import { getRedirects } from "./get-redirects";
1415
*
1516
* @param request Web Fetch Request to possibly redirect
1617
*/
17-
export async function handleRedirects(request: Request): Promise<void> {
18+
export const handleRedirects: Route.unstable_MiddlewareFunction = async (
19+
{ request },
20+
next
21+
) => {
1822
let redirects = await getRedirects();
1923
let url = new URL(request.url);
2024
let response = await checkUrl(url.pathname, redirects);
2125
if (response) throw response;
22-
}
26+
// FIXME: Update RR to auto-bubble the internal response if `next` isn't
27+
// called so we can omit the return value
28+
return next();
29+
};

app/root.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Scripts,
88
ScrollRestoration,
99
} from "react-router";
10-
import { CACHE_CONTROL, middlewares } from "./http";
10+
import { CACHE_CONTROL } from "./http";
1111

1212
import { parseColorScheme } from "./modules/color-scheme/server";
1313
import {
@@ -23,9 +23,17 @@ import "@docsearch/css/dist/style.css";
2323
import "~/styles/docsearch.css";
2424
import type { Route } from "./+types/root";
2525

26-
export async function loader({ request }: LoaderFunctionArgs) {
27-
await middlewares(request);
26+
import { ensureSecure } from "~/modules/http-utils/ensure-secure";
27+
import { handleRedirects } from "~/modules/redirects/.server";
28+
import { removeTrailingSlashes } from "~/modules/http-utils/remove-slashes";
29+
30+
export const unstable_middleware: Route.unstable_MiddlewareFunction[] = [
31+
ensureSecure,
32+
removeTrailingSlashes,
33+
handleRedirects,
34+
];
2835

36+
export async function loader({ request }: LoaderFunctionArgs) {
2937
let colorScheme = await parseColorScheme(request);
3038
let isProductionHost = isHost("reactrouter.com", request);
3139

0 commit comments

Comments
 (0)