|
| 1 | +--- |
| 2 | +pcx_content_type: how-to |
| 3 | +title: TanStack |
| 4 | +head: [] |
| 5 | +description: Create an TanStack application and deploy it to Cloudflare Workers with Workers Assets. |
| 6 | +--- |
| 7 | + |
| 8 | +import { WranglerConfig } from "~/components"; |
| 9 | + |
| 10 | +In this guide, you will create a new [TanStack](https://tanstack.com/) application and deploy to Cloudflare Workers (with the new [Workers Assets](/workers/static-assets/)). |
| 11 | + |
| 12 | +## Set up a new project |
| 13 | + |
| 14 | +TanStack provides a start-basic project. We'll use this starter project to create a new TanStack application. |
| 15 | + |
| 16 | +```sh |
| 17 | +npx gitpick TanStack/router/tree/main/examples/react/start-basic start-basic |
| 18 | +cd start-basic |
| 19 | +npm install |
| 20 | +``` |
| 21 | + |
| 22 | +## Develop locally |
| 23 | + |
| 24 | +```sh |
| 25 | +npm run dev |
| 26 | +``` |
| 27 | + |
| 28 | +## Preparing for Deployment to Cloudflare Workers |
| 29 | + |
| 30 | +### Install `unenv` package |
| 31 | + |
| 32 | +[`unenv`](https://github.com/unjs/unenv) is a package that normalizes runtime environments across Node.js, browsers, and edge runtimes like Cloudflare Workers. It’s essential for TanStack Router because certain Node.js APIs are unavailable in the Workers environment. `unenv` offers compatible replacements for those APIs. |
| 33 | + |
| 34 | +```sh |
| 35 | +npm install unenv |
| 36 | +``` |
| 37 | + |
| 38 | +### Modify the `app.config.ts` file |
| 39 | + |
| 40 | +To configure your application for Cloudflare Workers deployment, add the following lines to your `app.config.ts` file: |
| 41 | + |
| 42 | +```ts |
| 43 | + server: { |
| 44 | + preset: "cloudflare-module", |
| 45 | + unenv: cloudflare, |
| 46 | + }, |
| 47 | +``` |
| 48 | + |
| 49 | +This will set the correct build format and runtime environment for Cloudflare. |
| 50 | + |
| 51 | +### Add a Wrnagler file |
| 52 | + |
| 53 | +Create a `wrangler.jsonc` or `wrangler.toml` file in the root of your project. This file is used to configure the Cloudflare Workers deployment. |
| 54 | + |
| 55 | +<WranglerConfig> |
| 56 | +```json |
| 57 | +{ |
| 58 | + "$schema": "node_modules/wrangler/config-schema.json", |
| 59 | + "name": "start-basic", |
| 60 | + "main": "./.output/server/index.mjs", |
| 61 | + "compatibility_date": "2025-04-14", |
| 62 | + "observability": { |
| 63 | + "enabled": true, |
| 64 | + }, |
| 65 | + "assets": { |
| 66 | + "directory": "./.output/public/", |
| 67 | + }, |
| 68 | + "compatibility_flags": ["nodejs_compat"], |
| 69 | +} |
| 70 | +``` |
| 71 | +</WranglerConfig> |
| 72 | + |
| 73 | +### Build the application |
| 74 | + |
| 75 | +You need to build your application before deploying it to Cloudflare Workers. |
| 76 | + |
| 77 | +```sh |
| 78 | +npm run build |
| 79 | +``` |
| 80 | + |
| 81 | +### Deploy the application |
| 82 | + |
| 83 | +The below command will deploy your application to Cloudflare Workers and provide you with a deployment URL. |
| 84 | + |
| 85 | +```sh |
| 86 | +npx wrangler deploy |
| 87 | +``` |
| 88 | + |
| 89 | +### Update utils file with deployment URL |
| 90 | + |
| 91 | +This step is required for the `/users` page to function properly in the `start-basic` example. Update the `/src/utils/users.tsx` file with the Cloudflare Workers deployment URL. |
| 92 | + |
| 93 | +```ts |
| 94 | +export const DEPLOY_URL = 'YOUR_DEPLOYMENT_URL' |
| 95 | +``` |
| 96 | + |
| 97 | +By following the steps above, you will have deployed the `start-basic` TanStack example to Cloudflare Workers. |
| 98 | + |
| 99 | +## GitHub Repository Integration |
| 100 | + |
| 101 | +This step is optional. If you want your application deploy with every new push to your GitHub repository, you can follow the steps below. |
| 102 | + |
| 103 | +1. Go to your Workers project in the Cloudflare dashboard. |
| 104 | +2. Click on the "Settings" tab. |
| 105 | +3. Go to the Build section |
| 106 | +4. In the Git repository section, connect your GitHub repository. |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
0 commit comments