Skip to content

Commit 51ff204

Browse files
Modify to use cloudflareDevProxy and load contents of wrangler.toml (#6)
* Modify to use cloudflareDevProxy and load contents of wrangler.toml * Fix pnpm-lock.yaml * fix tsconfig * Remove extra config stuff --------- Co-authored-by: Brooks Lybrand <[email protected]>
1 parent 450ab8b commit 51ff204

File tree

8 files changed

+24
-34
lines changed

8 files changed

+24
-34
lines changed

cloudflare/app/routes/home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function meta({}: Route.MetaArgs) {
99
}
1010

1111
export function loader({ context }: Route.LoaderArgs) {
12-
return { message: context.VALUE_FROM_CLOUDFLARE };
12+
return { message: context.cloudflare.env.VALUE_FROM_CLOUDFLARE };
1313
}
1414

1515
export default function Home({ loaderData }: Route.ComponentProps) {

cloudflare/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
},
1818
"devDependencies": {
1919
"@cloudflare/workers-types": "^4.20241112.0",
20-
"@hiogawa/vite-node-miniflare": "0.1.1",
2120
"@react-router/dev": "*",
2221
"@types/node": "^20",
2322
"@types/react": "^19.0.1",

cloudflare/tsconfig.cloudflare.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"app/**/*",
66
"app/**/.server/**/*",
77
"app/**/.client/**/*",
8-
"workers/**/*"
8+
"workers/**/*",
9+
"worker-configuration.d.ts"
910
],
1011
"compilerOptions": {
1112
"composite": true,

cloudflare/vite.config.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { vitePluginViteNodeMiniflare } from "@hiogawa/vite-node-miniflare";
21
import { reactRouter } from "@react-router/dev/vite";
2+
import { cloudflareDevProxy } from "@react-router/dev/vite/cloudflare";
33
import autoprefixer from "autoprefixer";
44
import tailwindcss from "tailwindcss";
55
import { defineConfig } from "vite";
@@ -18,29 +18,10 @@ export default defineConfig(({ isSsrBuild }) => ({
1818
plugins: [tailwindcss, autoprefixer],
1919
},
2020
},
21-
ssr: {
22-
target: "webworker",
23-
noExternal: true,
24-
resolve: {
25-
conditions: ["workerd", "browser"],
26-
},
27-
optimizeDeps: {
28-
include: [
29-
"react",
30-
"react/jsx-runtime",
31-
"react/jsx-dev-runtime",
32-
"react-dom",
33-
"react-dom/server",
34-
"react-router",
35-
],
36-
},
37-
},
3821
plugins: [
39-
vitePluginViteNodeMiniflare({
40-
entry: "./workers/app.ts",
41-
miniflareOptions: (options) => {
42-
options.compatibilityDate = "2024-11-18";
43-
options.compatibilityFlags = ["nodejs_compat"];
22+
cloudflareDevProxy({
23+
getLoadContext({ context }) {
24+
return { cloudflare: context.cloudflare };
4425
},
4526
}),
4627
reactRouter(),

cloudflare/worker-configuration.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Generated by Wrangler by running `wrangler types`
2+
3+
interface Env {
4+
VALUE_FROM_CLOUDFLARE: "Hello from Cloudflare";
5+
}

cloudflare/workers/app.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { createRequestHandler } from "react-router";
22

33
declare global {
4-
interface CloudflareEnvironment {}
4+
interface CloudflareEnvironment extends Env {}
55
}
66

77
declare module "react-router" {
88
export interface AppLoadContext {
9-
VALUE_FROM_CLOUDFLARE: string;
9+
cloudflare: {
10+
env: CloudflareEnvironment;
11+
ctx: ExecutionContext;
12+
};
1013
}
1114
}
1215

@@ -17,9 +20,10 @@ const requestHandler = createRequestHandler(
1720
);
1821

1922
export default {
20-
fetch(request, env) {
23+
fetch(request, env, ctx) {
24+
console.log("yo");
2125
return requestHandler(request, {
22-
VALUE_FROM_CLOUDFLARE: "Hello from Cloudflare",
26+
cloudflare: { env, ctx },
2327
});
2428
},
2529
} satisfies ExportedHandler<CloudflareEnvironment>;

cloudflare/wrangler.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
workers_dev = true
2-
name = "my-worker"
2+
name = "my-react-router-worker"
33
compatibility_date = "2024-11-18"
44
main = "./build/server/index.js"
55
assets = { directory = "./build/client/" }
6+
7+
[vars]
8+
VALUE_FROM_CLOUDFLARE = "Hello from Cloudflare"

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)