Skip to content

Commit 60ca9bc

Browse files
committed
Update playground to TS and apply getLoadContext
1 parent bcbd8f7 commit 60ca9bc

File tree

13 files changed

+295
-106
lines changed

13 files changed

+295
-106
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { unstable_createContext } from "react-router";
22

3+
export const expressContext = unstable_createContext<string>("default");
34
export const rootContext = unstable_createContext<string>();
45
export const aContext = unstable_createContext<string>();
56
export const bContext = unstable_createContext<string>("empty");

playground/middleware/app/routes/client.a.b.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const unstable_clientMiddleware: Route.unstable_ClientMiddlewareFunction[
1212
},
1313
];
1414

15-
export async function clientLoader({ context }: Route.LoaderArgs) {
15+
export async function clientLoader({ context }: Route.ClientLoaderArgs) {
1616
await new Promise((r) => setTimeout(r, 200));
1717
return JSON.stringify({
1818
root: context.get(rootContext),

playground/middleware/app/routes/client.a.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Outlet, unstable_createContext } from "react-router";
1+
import { Outlet } from "react-router";
22
import type { Route } from "./+types/client.a";
33
import { aContext, rootContext } from "~/contexts";
44

@@ -12,7 +12,7 @@ export const unstable_clientMiddleware: Route.unstable_ClientMiddlewareFunction[
1212
},
1313
];
1414

15-
export function clientLoader({ context }: Route.LoaderArgs) {
15+
export function clientLoader({ context }: Route.ClientLoaderArgs) {
1616
return JSON.stringify({
1717
root: context.get(rootContext),
1818
a: context.get(aContext),

playground/middleware/app/routes/server.a.b.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Outlet, unstable_createContext } from "react-router";
22
import type { Route } from "./+types/server.a.b";
3-
import { aContext, bContext, rootContext } from "~/contexts";
3+
import { aContext, bContext, expressContext, rootContext } from "~/contexts";
44

55
export const unstable_middleware: Route.unstable_MiddlewareFunction[] = [
66
async ({ context }, next) => {
@@ -15,6 +15,7 @@ export const unstable_middleware: Route.unstable_MiddlewareFunction[] = [
1515
export async function loader({ context }: Route.LoaderArgs) {
1616
await new Promise((r) => setTimeout(r, 200));
1717
return JSON.stringify({
18+
express: context.get(expressContext),
1819
root: context.get(rootContext),
1920
a: context.get(aContext),
2021
b: context.get(bContext),

playground/middleware/app/routes/server.a.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Outlet, unstable_createContext } from "react-router";
22
import type { Route } from "./+types/server.a";
3-
import { aContext, rootContext } from "~/contexts";
3+
import { aContext, expressContext, rootContext } from "~/contexts";
44

55
export const unstable_middleware: Route.unstable_MiddlewareFunction[] = [
66
async ({ context }, next) => {
@@ -14,6 +14,7 @@ export const unstable_middleware: Route.unstable_MiddlewareFunction[] = [
1414

1515
export function loader({ context }: Route.LoaderArgs) {
1616
return JSON.stringify({
17+
express: context.get(expressContext),
1718
root: context.get(rootContext),
1819
a: context.get(aContext),
1920
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import express from "express";
2+
3+
const PORT = Number.parseInt(process.env.PORT || "3000");
4+
5+
const app = express();
6+
7+
console.log("Starting development server");
8+
const viteDevServer = await import("vite").then((vite) =>
9+
vite.createServer({
10+
server: { middlewareMode: true },
11+
})
12+
);
13+
app.use(viteDevServer.middlewares);
14+
app.use(async (req, res, next) => {
15+
try {
16+
const source = await viteDevServer.ssrLoadModule("./server.ts");
17+
return await source.app(req, res, next);
18+
} catch (error) {
19+
if (typeof error === "object" && error instanceof Error) {
20+
viteDevServer.ssrFixStacktrace(error);
21+
}
22+
next(error);
23+
}
24+
});
25+
26+
app.listen(PORT, () => {
27+
console.log(`Server is running on http://localhost:${PORT}`);
28+
});

playground/middleware/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,33 @@
66
"type": "module",
77
"scripts": {
88
"build": "react-router build",
9-
"dev": "react-router dev",
10-
"start": "react-router-serve ./build/server/index.js",
9+
"dev": "cross-env NODE_ENV=development tsx dev-server.ts",
10+
"start": "cross-env NODE_ENV=production node build/server/index.js",
1111
"typecheck": "react-router typegen && tsc"
1212
},
1313
"dependencies": {
14+
"@react-router/express": "workspace:*",
1415
"@react-router/node": "workspace:*",
15-
"@react-router/serve": "workspace:*",
16+
"compression": "^1.7.4",
17+
"cross-env": "^7.0.3",
18+
"express": "^4.19.2",
1619
"isbot": "^5.1.11",
20+
"morgan": "^1.10.0",
1721
"react": "^18.2.0",
1822
"react-dom": "^18.2.0",
1923
"react-router": "workspace:*"
2024
},
2125
"devDependencies": {
2226
"@react-router/dev": "workspace:*",
2327
"@react-router/fs-routes": "workspace:*",
28+
"@types/compression": "^1.7.5",
29+
"@types/express": "^4.17.21",
30+
"@types/express-serve-static-core": "^5.0.6",
31+
"@types/morgan": "^1.9.9",
32+
"@types/node": "^20.0.0",
2433
"@types/react": "^18.2.20",
2534
"@types/react-dom": "^18.2.7",
35+
"tsx": "^4.19.3",
2636
"typescript": "^5.1.6",
2737
"vite": "^6.0.0",
2838
"vite-tsconfig-paths": "^4.2.1"

playground/middleware/server.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createRequestHandler } from "@react-router/express";
2+
import compression from "compression";
3+
import express from "express";
4+
import morgan from "morgan";
5+
import "react-router";
6+
import { expressContext } from "~/contexts";
7+
8+
export const app = express();
9+
app.use(compression());
10+
app.disable("x-powered-by");
11+
12+
if (process.env.NODE_ENV === "production") {
13+
app.use(
14+
"/assets",
15+
express.static("build/client/assets", { immutable: true, maxAge: "1y" })
16+
);
17+
app.use(express.static("build/client", { maxAge: "1h" }));
18+
}
19+
20+
app.use(morgan("tiny"));
21+
app.use(
22+
createRequestHandler({
23+
// @ts-expect-error
24+
build: () => import("virtual:react-router/server-build"),
25+
getLoadContext() {
26+
return new Map([[expressContext, "Hello from Express"]]);
27+
},
28+
})
29+
);
30+
31+
if (process.env.NODE_ENV === "production") {
32+
console.log("Starting production server");
33+
const PORT = Number.parseInt(process.env.PORT || "3000");
34+
app.listen(PORT, () => {
35+
console.log(`Server is running on http://localhost:${PORT}`);
36+
});
37+
}
Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
11
{
2-
"include": [
3-
"**/*.ts",
4-
"**/*.tsx",
5-
"**/.server/**/*.ts",
6-
"**/.server/**/*.tsx",
7-
"**/.client/**/*.ts",
8-
"**/.client/**/*.tsx",
9-
"./.react-router/types/**/*"
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.node.json" },
5+
{ "path": "./tsconfig.vite.json" }
106
],
117
"compilerOptions": {
12-
"lib": ["DOM", "DOM.Iterable", "ES2022"],
13-
"types": ["@react-router/node", "vite/client"],
8+
"checkJs": true,
149
"verbatimModuleSyntax": true,
15-
"esModuleInterop": true,
16-
"jsx": "react-jsx",
17-
"module": "ESNext",
18-
"moduleResolution": "Bundler",
19-
"resolveJsonModule": true,
20-
"target": "ES2022",
21-
"strict": true,
22-
"allowJs": true,
2310
"skipLibCheck": true,
24-
"baseUrl": ".",
25-
"paths": {
26-
"~/*": ["./app/*"]
27-
},
28-
"noEmit": true,
29-
"rootDirs": [".", "./.react-router/types"]
11+
"strict": true,
12+
"noEmit": true
3013
}
3114
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": [
4+
"**/*.ts",
5+
"**/*.tsx",
6+
"dev-server.ts",
7+
"tailwind.config.ts",
8+
"vite.config.ts"
9+
],
10+
"compilerOptions": {
11+
"composite": true,
12+
"strict": true,
13+
"types": ["node"],
14+
"lib": ["ES2022"],
15+
"target": "ES2022",
16+
"module": "ES2022",
17+
"moduleResolution": "bundler"
18+
}
19+
}

0 commit comments

Comments
 (0)