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
Update TanStack Workers guide to use the Cloudflare Vite plugin (#25529)
Note: this is a temporary update to the guide (which is currently incorrect), we're working on enabling C3 to generate TanStack applications to streamline the process for developers, once that's ready we'll update again this guide accordingly
---------
Co-authored-by: James Opstad <[email protected]>
In this guide, you will create a new [TanStack Start](https://tanstack.com/start) application and deploy it to Cloudflare Workers (with the new [Workers Assets](/workers/static-assets/)).
14
20
15
-
TanStack Start is a full-stack React framework powered by TanStack Router. It provides a full-document SSR, streaming, server functions, bundling, and more using Vite and modern web standards.
21
+
## 1. Set up a new project
16
22
17
-
## Create a new TanStack Start
23
+
Start by cloning the Cloudflare example from the official TanStack repository.
18
24
19
-
TanStack Start Beta has significantly improved Cloudflare compatibility compared to the Alpha version, making deployment and development much more straightforward.
This command will clone the TanStack Start basic project to your local machine, change directory to the project, and install the dependencies. TanStack [provides other examples](https://tanstack.com/start/latest/docs/framework/react/quick-start#examples) that you can use by replacing `start-basic` with the example you want to use.
33
-
</Details>
37
+
And install the project's dependencies with:
34
38
35
-
2.**Develop locally**
39
+
<PackageManagerstype="install" />
36
40
37
-
After creating your project, run the following command in your project directory to start a local development server. By default this starts a local development server on `http://localhost:3000/`
41
+
## 2. Develop locally
38
42
39
-
<PackageManagerstype="run"args="dev" />
43
+
After you have created your project, run the following command in the project directory to start a local development server. This will allow you to preview your project locally during development.
40
44
41
-
</Steps>
45
+
<PackageManagerstype="run"args="dev" />
42
46
43
-
## Preparing for Deployment to Cloudflare Workers
47
+
## 3. Deploy your Project
44
48
45
-
Whether you created a new TanStack Start project or are using an existing project, you'll need to make some changes to prepare for deployment to Cloudflare Workers.
49
+
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/ci-cd/builds/).
46
50
47
-
<Steps>
51
+
To deploy your application you will first need to build it:
48
52
49
-
1.**Configure Vite for Cloudflare compatibility**
53
+
<PackageManagerstype="run"args="build" />
50
54
51
-
Update your `vite.config.ts` file to use the `cloudflare-module` target for a compatible build:
target: "cloudflare-module", // Key configuration for Cloudflare compatibility
68
-
}),
69
-
],
70
-
});
71
-
```
59
+
If you're using CI, ensure you update your ["deploy command"](/workers/ci-cd/builds/configuration/#build-settings) configuration appropriately.
72
60
73
-
This single configuration change is all that's needed to make your TanStack Start application compatible with Cloudflare Workers.
61
+
:::note
74
62
75
-
2.**Add a Wrangler file**
63
+
After having built the application you can run the `preview` script to preview the built output locally before deploying it.
64
+
This can help you making sure that your application will work as intended once it's been deployed to the Cloudflare network:
76
65
77
-
Create a `wrangler.jsonc` or `wrangler.toml` file in the root of your project, `wrangler.jsonc` is the recommended approach. This file is used to configure the Cloudflare Workers deployment.
Note that the `directory` key is set to `.output/public`, which is the folder that will be filled with the build output. Additionally, the `main` key is set to `.output/server/index.mjs`, indicating to Cloudflare Workers where to locate the entry point for your application. The `kv_namespaces` section shows an example of how to configure a KV namespace binding.
106
-
107
-
3.**Add deployment scripts to package.json**
108
-
109
-
Add the following scripts to your `package.json` file to streamline deployment and type generation:
The `deploy` script combines building and deploying in one command, while `cf-typegen` generates TypeScript types for your Cloudflare bindings.
122
-
123
-
4.**Build the application**
124
-
125
-
You must build your application before deploying it to Cloudflare Workers.
126
-
127
-
<PackageManagerstype="run"args={"build"} />
128
-
129
-
5.**Deploy the application**
130
-
131
-
You can now use the deploy script to build and deploy your application in one command:
132
-
133
-
<PackageManagerstype="run"args={"deploy"} />
134
-
135
-
Alternatively, you can still deploy directly with Wrangler:
136
-
137
-
```sh
138
-
npx wrangler deploy
139
-
```
140
-
141
-
</Steps>
142
-
143
-
## Using Cloudflare Bindings
144
-
145
-
<Steps>
146
-
147
-
1.**Generate TypeScript types for your bindings**
148
-
149
-
Before using Cloudflare bindings in your code, generate the TypeScript types to ensure proper type safety:
150
-
151
-
<PackageManagerstype="run"args={"cf-typegen"} />
152
-
153
-
This command reads your `wrangler.jsonc` configuration and generates an `Env` interface with all your configured bindings.
154
-
155
-
2.**Create a helper function to get access to Cloudflare bindings**
156
-
157
-
Create a helper function named `bindings.ts` in the `src/utils` folder (create the folder if it doesn't exist), and paste in the below code. The example assumes you have a KV namespace with a binding name of `CACHE` already created in your account and added to the wrangler file.
158
-
159
-
160
-
```ts title="src/utils/bindings.ts"
161
-
162
-
let cachedEnv:Env|null=null;
163
-
164
-
// This gets called once at startup when running locally
* Will only work when being accessed on the server. Obviously, CF bindings are not available in the browser.
177
-
* @returns
178
-
*/
179
-
exportfunction getBindings():Env {
180
-
if (import.meta.env.DEV) {
181
-
if (!cachedEnv) {
182
-
thrownewError(
183
-
"Dev bindings not initialized yet. Call initDevEnv() first."
184
-
);
185
-
}
186
-
returncachedEnv;
187
-
}
72
+
## Bindings
188
73
189
-
returnprocess.envasunknownasEnv;
190
-
}
191
-
```
74
+
Your TanStack Start application can be fully integrated with the Cloudflare Developer Platform, in both local development and in production, by using bindings.
192
75
193
-
<Detailsheader="How is this code working?">
194
-
The helper function uses [getPlatformProxy](/workers/wrangler/api/#getplatformproxy) method from wrangler to provide access to your Cloudflare bindings during local development. The bindings are cached at startup for better performance. In production, bindings are accessed via `process.env`. Make sure you've run `npm run cf-typegen` to generate the `Env` types that this code references.
195
-
</Details>
76
+
You can use bindings simply by [importing the `env` object](https://developers.cloudflare.com/workers/runtime-apis/bindings/#importing-env-as-a-global) and accessing it in your server
77
+
side code.
196
78
197
-
3.**Example using a Cloudflare Binding in Server Functions**
79
+
For example in the following way:
198
80
199
-
Now that you have a helper function to get access to your Cloudflare bindings, you can use them in your server functions.
let growingAge =Number((awaitenv.CACHE.get("age")) ||0);
212
-
growingAge++;
213
-
awaitenv.CACHE.put("age", growingAge.toString());
214
-
return { name, randomNumber: growingAge };
215
-
});
216
-
```
95
+
function RouteComponent() {
96
+
// ...
97
+
}
98
+
```
217
99
218
-
A special thanks to GitHub user [backpine](https://github.com/backpine) for the code that supports Cloudflare Bindings in TanStack Start, which is demonstrated in their [TanStack Start Beta on Cloudflare example](https://github.com/backpine/tanstack-start-beta-on-cloudflare).
100
+
See `src/routes/index.tsx` for an example.
219
101
220
-
</Steps>
102
+
:::note
221
103
222
-
## Environment Handling
104
+
Running the `cf-typegen` script:
223
105
224
-
The TanStack Start Beta version provides seamless environment handling:
106
+
<PackageManagerstype="run"args="cf-typegen" />
225
107
226
-
-**Development**: Bindings are accessed via [`getPlatformProxy()`](/workers/wrangler/api/#getplatformproxy) from Wrangler and cached at startup
227
-
-**Production**: Bindings are accessed via [`process.env`](/workers/runtime-apis/nodejs/process/#processenv)
108
+
Will populate the `env` object with the various bindings based on your configuration.
228
109
229
-
This approach ensures your bindings are properly typed throughout your project and provides a smooth development experience.
110
+
:::
230
111
231
-
By following the steps above, you will have deployed your TanStack Start application to Cloudflare Workers.
0 commit comments