Skip to content

Commit e374e1f

Browse files
author
Tanushree Sharma
committed
remix-workers
1 parent 07f253b commit e374e1f

File tree

1 file changed

+209
-4
lines changed
  • src/content/docs/workers/frameworks/framework-guides

1 file changed

+209
-4
lines changed

src/content/docs/workers/frameworks/framework-guides/remix.mdx

Lines changed: 209 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import {
1313
PackageManagers,
1414
} from "~/components";
1515

16-
In this guide, you will create a new [Remix](https://remix.run/) application and deploy to Cloudflare Workers (with the new [<InlineBadge preset="beta" /> Workers Assets](/workers/static-assets/)).
16+
In this guide, you can [create a new](/workers/frameworks/framework-guides/remix/#create-a-new-application) or [deploy an existing](/workers/frameworks/framework-guides/remix/#deploy-an-existing-remix-application) [Remix](https://remix.run/) application to Cloudflare Workers with the new [<InlineBadge preset="beta" /> Workers Assets](/workers/static-assets/) feature.
1717

18-
## 1. Set up a new project
18+
## Create a new application
19+
20+
### 1. Set up a new project
1921

2022
Use the [`create-cloudflare`](https://www.npmjs.com/package/create-cloudflare) CLI (C3) to set up a new project. C3 will create a new project directory, initiate Remix's official setup tool, and provide the option to deploy instantly.
2123

@@ -42,13 +44,216 @@ After setting up your project, change your directory by running the following co
4244
cd my-remix-app
4345
```
4446

45-
## 2. Develop locally
47+
### 2. Develop locally
48+
49+
After you have created your project, run the following command in the project directory to start a local server. This will allow you to preview your project locally during development.
50+
51+
<PackageManagers type="run" args={"dev"} />
52+
53+
### 3. Deploy your Project
54+
55+
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/).
56+
57+
The following command will build and deploy your project. If you're using CI, ensure you update your ["deploy command"](/workers/ci-cd/builds/configuration/#build-settings) configuration appropriately.
58+
59+
<PackageManagers type="run" args={"deploy"} />
60+
61+
---
62+
63+
## Deploy an existing Remix application
64+
65+
Deploy an existing Remix application (for example, if you created an application with the [create-remix CLI](https://remix.run/docs/en/main/other-api/create-remix)) to Cloudflare Workers.
66+
67+
### 1. Manage dependencies
68+
69+
a. Install [Cloudflare's Wrangler CLI tool](/workers/wrangler/) and [`@cloudflare/workers-types`](/@cloudflare/workers-types).
70+
71+
```sh
72+
npm install --save-dev wrangler @cloudflare/workers-types
73+
```
74+
75+
b. Install the Cloudflare Remix adapter.
76+
77+
```sh
78+
npm install --save @remix-run/cloudflare
79+
```
80+
81+
c. Uninstall the Node.js Remix package.
82+
83+
```sh
84+
npm uninstall @remix-run/node
85+
```
86+
87+
### 2. Generate entry files
88+
89+
a. Remove the existing entry files that are tailored for Node.js.
90+
91+
```sh
92+
rm -rf ./app/entry.*.tsx
93+
```
94+
95+
b. Regenerate entry files compatible with Cloudflare.
96+
97+
```sh
98+
npx remix reveal
99+
```
100+
101+
### 3. Create `server.ts`
102+
103+
Create a `server.ts` file in the root directory. This file configures Remix for the Workers runtime and allows you to interact with Cloudflare-specific features like [`ctx`](/workers/runtime-apis/context/) and [Workers bindings](/workers/runtime-apis/bindings/).
104+
105+
```js title="server.ts"
106+
import { createRequestHandler, logDevReady } from "@remix-run/cloudflare";
107+
import * as build from "@remix-run/dev/server-build";
108+
109+
const handleRemixRequest = createRequestHandler(build, process.env.NODE_ENV);
110+
111+
if (process.env.NODE_ENV === "development") {
112+
logDevReady(build);
113+
}
114+
export default {
115+
async fetch(request, env, ctx) {
116+
117+
try {
118+
const loadContext = {
119+
cloudflare: {
120+
// This object matches the return value from Wrangler's
121+
// `getPlatformProxy` used during development via Remix's
122+
// `cloudflareDevProxyVitePlugin`:
123+
// https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy
124+
cf: request.cf,
125+
ctx: {
126+
waitUntil: ctx.waitUntil.bind(ctx),
127+
passThroughOnException: ctx.passThroughOnException.bind(ctx),
128+
},
129+
caches,
130+
env,
131+
},
132+
};
133+
return await handleRemixRequest(request, loadContext);
134+
} catch (error) {
135+
console.log(error);
136+
return new Response("An unexpected error occurred", { status: 500 });
137+
}
138+
},
139+
} satisfies ExportedHandler<Env>;
140+
```
141+
142+
### 4. Create `wrangler.toml`
143+
144+
Create a `wrangler.toml` file with your project details.
145+
146+
```toml
147+
name = "my-worker"
148+
compatibility_date = "2024-10-03"
149+
main = "./build/index.js"
150+
151+
[assets]
152+
directory = "./public"
153+
```
154+
155+
### 5. Generate wrangler types
156+
157+
Generates TypeScript definitions for Cloudflare Workers.
158+
159+
```sh
160+
npx wrangler types
161+
```
162+
163+
### 6. Update `tsconfig.json`
164+
165+
Update `tsconfig.json` to recognize Cloudflare types.
166+
167+
```json
168+
{
169+
"compilerOptions": {
170+
"types": ["@remix-run/cloudflare", "@cloudflare/workers-types"]
171+
// ...other compiler options
172+
}
173+
// ...other configurations
174+
}
175+
```
176+
177+
### 7. Update import statements
178+
179+
Update the `./app/root.tsx` and `./app/routes/_index.tsx` files, by changing imports from `@remix-run/node` to `@remix-run/cloudflare`:
180+
181+
```js
182+
import { ... } from "@remix-run/cloudflare";
183+
```
184+
185+
### 8. Create `remix.config.js`
186+
187+
Create a `remix.config.js` file in the root directory. This file to defines various settings for how your application is built and behaves during development and deployment.
188+
189+
:::note
190+
191+
This uses the "Classic" Remix compiler, since Vite does not work with all Workers resources yet (for example, [Durable Objects](/durable-objects/), [Hyperdrive](/hyperdrive/), etc). We are currently working on supporting the Vite Environment for workerd. Once complete, this guide will be updated. Using Vite will not require any changes in your actual code, simply in configuration (for example `remix.config.js` will get replaced by `vite.config.js`).
192+
193+
:::
194+
195+
```js title="server.ts"
196+
/** @type {import('@remix-run/dev').AppConfig} */
197+
export default {
198+
ignoredRouteFiles: ["**/*.css"],
199+
server: "./server.ts",
200+
serverConditions: ["workerd", "worker", "browser"],
201+
serverMainFields: ["workerd", "browser", "module", "main"],
202+
// eslint-disable-next-line no-undef
203+
serverDependenciesToBundle: [
204+
// bundle everything except the external cloudflare:workers package
205+
/^(?!.*\bcloudflare:workers\b).*$/,
206+
],
207+
serverMainFields: ["browser", "module", "main"],
208+
serverMinify: true,
209+
serverModuleFormat: "esm",
210+
serverPlatform: "neutral",
211+
// appDirectory: "app",
212+
assetsBuildDirectory: "public/build",
213+
publicPath: "/build/",
214+
serverBuildPath: "build/index.js",
215+
};
216+
```
217+
218+
### 9. Update styling
219+
220+
a. Replace the Tailwind CSS import: Replace `import "./tailwind.css";` with `import styles from "./styles.css";`.
221+
222+
```js
223+
import styles from "./styles.css";
224+
```
225+
226+
b. Add the `styles.css` stylesheet
227+
228+
```js
229+
export const links: LinksFunction = () => [
230+
{ rel: "stylesheet", href: styles },
231+
{ rel: "preconnect", href: "https://fonts.googleapis.com" },
232+
// ... other stylesheets or pre-connect resources
233+
234+
]
235+
```
236+
237+
### 10. Update `package.json`
238+
239+
Update `package.json` scripts to add wrangler commands and remove Vite.
240+
241+
```json
242+
"scripts": {
243+
"build": "remix build",
244+
"dev": "remix dev --manual -c \"wrangler dev\"",
245+
"deploy": "npm run build && wrangler deploy",
246+
// ...other scripts
247+
},
248+
```
249+
250+
### 9. Develop locally
46251

47252
After you have created your project, run the following command in the project directory to start a local server. This will allow you to preview your project locally during development.
48253

49254
<PackageManagers type="run" args={"dev"} />
50255

51-
## 3. Deploy your Project
256+
### 10. Deploy your Project
52257

53258
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/).
54259

0 commit comments

Comments
 (0)