You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add more detail around how to use bindings in React-Router Workers projects (#22850)
* add more detail around how to use bindings in React-Router Workers projects
* Update src/content/docs/workers/frameworks/framework-guides/react-router.mdx
Co-authored-by: Greg Brimble <[email protected]>
* fix up missing import and formatting
---------
Co-authored-by: Greg Brimble <[email protected]>
**Start from CLI**: Scaffold a full-stack app with [React Router v7](https://reactrouter.com/) and the [Cloudflare Vite plugin](/workers/vite-plugin/) for lightning-fast development.
@@ -33,74 +34,164 @@ It combines with the [Cloudflare Vite plugin](/workers/vite-plugin/) to provide
33
34
## Creating a full-stack React Router app
34
35
35
36
<Steps>
36
-
1.**Create a new project with the create-cloudflare CLI (C3)**
37
37
38
-
<PackageManagers
38
+
1.**Create a new project with the create-cloudflare CLI (C3)**
`react-router.config.ts` is your [React Router config file](https://reactrouter.com/explanation/special-files#react-routerconfigts).
61
-
In this file:
62
-
-`ssr` is set to `true`, meaning that your application will use server-side rendering.
63
-
-`future.unstable_viteEnvironmentApi` is set to `true` to enable compatibility with the [Cloudflare Vite plugin](/workers/vite-plugin/).
64
-
65
-
:::note
66
-
SPA mode and prerendering are not currently supported when using the [Cloudflare Vite plugin](/workers/vite-plugin/).
67
-
If you wish to use React Router in an SPA then we recommend starting with the [React template](/workers/frameworks/framework-guides/react/) and using React Router [as a library](https://reactrouter.com/start/data/installation).
68
-
:::
69
-
70
-
`vite.config.ts` is your [Vite config file](https://vite.dev/config/).
71
-
The React Router and Cloudflare plugins are included in the `plugins` array.
72
-
The [Cloudflare Vite plugin](/workers/vite-plugin/) runs your server code in the Workers runtime, ensuring your local development environment is as close to production as possible.
73
-
74
-
`wrangler.jsonc` is your [Worker config file](/workers/wrangler/configuration/).
75
-
In this file:
76
-
- `main` points to `./workers/app.ts`.
77
-
This is the entry file for your Worker.
78
-
The default export includes a [`fetch` handler](/workers/runtime-apis/fetch/), which delegates the request to React Router.
79
-
- If you want to add [bindings](/workers/runtime-apis/bindings/) to resources on Cloudflare's developer platform, you configure them here.
80
-
</Details>
44
+
/>
45
+
46
+
<Detailsheader="How is this project set up?">
47
+
48
+
Below is a simplified file tree of the project.
49
+
50
+
<FileTree>
51
+
- my-react-router-app
52
+
- app
53
+
- routes
54
+
- ...
55
+
- entry.server.ts
56
+
- root.tsx
57
+
- routes.ts
58
+
- workers
59
+
- app.ts
60
+
- react-router.config.ts
61
+
- vite.config.ts
62
+
- wrangler.jsonc
63
+
</FileTree>
64
+
65
+
`react-router.config.ts` is your [React Router config file](https://reactrouter.com/explanation/special-files#react-routerconfigts).
66
+
In this file:
67
+
- `ssr` is set to `true`, meaning that your application will use server-side rendering.
68
+
- `future.unstable_viteEnvironmentApi` is set to `true` to enable compatibility with the [Cloudflare Vite plugin](/workers/vite-plugin/).
69
+
70
+
:::note
71
+
SPA mode and prerendering are not currently supported when using the [Cloudflare Vite plugin](/workers/vite-plugin/).
72
+
If you wish to use React Router in an SPA then we recommend starting with the [React template](/workers/frameworks/framework-guides/react/) and using React Router [as a library](https://reactrouter.com/start/data/installation).
73
+
:::
74
+
75
+
`vite.config.ts` is your [Vite config file](https://vite.dev/config/).
76
+
The React Router and Cloudflare plugins are included in the `plugins` array.
77
+
The [Cloudflare Vite plugin](/workers/vite-plugin/) runs your server code in the Workers runtime, ensuring your local development environment is as close to production as possible.
78
+
79
+
`wrangler.jsonc` is your [Worker config file](/workers/wrangler/configuration/).
80
+
In this file:
81
+
- `main` points to `./workers/app.ts`.
82
+
This is the entry file for your Worker.
83
+
The default export includes a [`fetch` handler](/workers/runtime-apis/fetch/), which delegates the request to React Router.
84
+
- If you want to add [bindings](/workers/runtime-apis/bindings/) to resources on Cloudflare's developer platform, you configure them here.
85
+
86
+
</Details>
81
87
82
88
2.**Develop locally**
83
89
84
90
After creating your project, run the following command in your project directory to start a local development server.
91
+
85
92
<PackageManagerstype="run"args="dev" />
86
-
<Detailsheader="What's happening in local development?">
87
-
This project uses React Router in combination with the [Cloudflare Vite plugin](/workers/vite-plugin/).
88
-
This means that your application runs in the Cloudflare Workers runtime, just like in production, and enables access to local emulations of bindings.
89
-
</Details>
93
+
94
+
<Detailsheader="What's happening in local development?">
95
+
This project uses React Router in combination with the [Cloudflare Vite plugin](/workers/vite-plugin/).
96
+
This means that your application runs in the Cloudflare Workers runtime, just like in production, and enables access to local emulations of bindings.
97
+
</Details>
90
98
91
99
3.**Deploy your project**
92
100
93
-
Your project can be deployed to a `*.workers.dev` subdomain or a [Custom Domain](/workers/configuration/routing/custom-domains/) from your own machine or from any CI/CD system, including Cloudflare's own [Workers Builds](/workers/ci-cd/builds/).
101
+
Your project can be deployed to a `*.workers.dev` subdomain or a [Custom Domain](/workers/configuration/routing/custom-domains/) from your own machine or from any CI/CD system, including Cloudflare's own [Workers Builds](/workers/ci-cd/builds/).
94
102
95
-
The following command will build and deploy your project. If you are using CI, ensure you update your ["deploy command"](/workers/ci-cd/builds/configuration/#build-settings) configuration appropriately.
103
+
The following command will build and deploy your project. If you are using CI, ensure you update your ["deploy command"](/workers/ci-cd/builds/configuration/#build-settings) configuration appropriately.
96
104
97
-
<PackageManagerstype="run"args={"deploy"} />
105
+
<PackageManagerstype="run"args={"deploy"} />
98
106
99
107
</Steps>
100
108
101
109
## Use bindings with React Router
102
110
103
111
With bindings, your application can be fully integrated with the Cloudflare Developer Platform, giving you access to compute, storage, AI and more.
104
-
As you have direct access to your Worker entry file (`workers/app.ts`), you can also add additional exports such as [Durable Objects](/durable-objects/) and [Workflows](/workflows/).
112
+
113
+
Once you have configured the bindings in the Wrangler configuration file, they are then available within `context.cloudflare` in your loader or action functions:
As you have direct access to your Worker entry file (`workers/app.ts`), you can also add additional exports such as [Durable Objects](/durable-objects/) and [Workflows](/workflows/)
126
+
127
+
<Detailsheader="Example: Using Workflows">
128
+
129
+
Here is an example of how to set up a simple Workflow in your Worker entry file.
0 commit comments