From b3f75da44749d6d69dd613668fd346b0e6802d9c Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Fri, 3 May 2024 16:41:29 +1000 Subject: [PATCH] Add Vite-based `cloudflare-workers` template (#9345) --- .eslintignore | 1 + docs/guides/templates.md | 1 + docs/guides/vite.md | 3 + templates/cloudflare-workers/.eslintrc.cjs | 83 ++++++++++++++++++ templates/cloudflare-workers/.gitignore | 5 ++ templates/cloudflare-workers/README.md | 38 ++++++++ .../cloudflare-workers/app/entry.client.tsx | 18 ++++ .../cloudflare-workers/app/entry.server.tsx | 43 +++++++++ templates/cloudflare-workers/app/root.tsx | 29 ++++++ .../cloudflare-workers/app/routes/_index.tsx | 35 ++++++++ templates/cloudflare-workers/env.d.ts | 4 + templates/cloudflare-workers/load-context.ts | 7 ++ templates/cloudflare-workers/package.json | 44 ++++++++++ .../cloudflare-workers/public/favicon.ico | Bin 0 -> 16958 bytes templates/cloudflare-workers/server.js | 57 ++++++++++++ templates/cloudflare-workers/tsconfig.json | 37 ++++++++ templates/cloudflare-workers/vite.config.ts | 21 +++++ .../worker-configuration.d.ts | 3 + templates/cloudflare-workers/wrangler.toml | 12 +++ 19 files changed, 441 insertions(+) create mode 100644 templates/cloudflare-workers/.eslintrc.cjs create mode 100644 templates/cloudflare-workers/.gitignore create mode 100644 templates/cloudflare-workers/README.md create mode 100644 templates/cloudflare-workers/app/entry.client.tsx create mode 100644 templates/cloudflare-workers/app/entry.server.tsx create mode 100644 templates/cloudflare-workers/app/root.tsx create mode 100644 templates/cloudflare-workers/app/routes/_index.tsx create mode 100644 templates/cloudflare-workers/env.d.ts create mode 100644 templates/cloudflare-workers/load-context.ts create mode 100644 templates/cloudflare-workers/package.json create mode 100644 templates/cloudflare-workers/public/favicon.ico create mode 100644 templates/cloudflare-workers/server.js create mode 100644 templates/cloudflare-workers/tsconfig.json create mode 100644 templates/cloudflare-workers/vite.config.ts create mode 100644 templates/cloudflare-workers/worker-configuration.d.ts create mode 100644 templates/cloudflare-workers/wrangler.toml diff --git a/.eslintignore b/.eslintignore index 90bb218295c..e26dc5e8bf1 100644 --- a/.eslintignore +++ b/.eslintignore @@ -15,3 +15,4 @@ templates/deno packages/remix-dev/config/defaults templates/remix-tutorial/app/data.ts +templates/cloudflare-workers/worker-configuration.d.ts diff --git a/docs/guides/templates.md b/docs/guides/templates.md index 96928197c3b..634dd5db7e5 100644 --- a/docs/guides/templates.md +++ b/docs/guides/templates.md @@ -29,6 +29,7 @@ If you want more control over your server or wish to deploy to a non-node runtim ```shellscript nonumber npx create-remix@latest --template remix-run/remix/templates/cloudflare +npx create-remix@latest --template remix-run/remix/templates/cloudflare-workers npx create-remix@latest --template remix-run/remix/templates/express npx create-remix@latest --template remix-run/remix/templates/remix npx create-remix@latest --template remix-run/remix/templates/remix-javascript diff --git a/docs/guides/vite.md b/docs/guides/vite.md index 841b7f68c29..7cf5b2128b3 100644 --- a/docs/guides/vite.md +++ b/docs/guides/vite.md @@ -27,6 +27,9 @@ npx create-remix@latest --template remix-run/remix/templates/express # Cloudflare: npx create-remix@latest --template remix-run/remix/templates/cloudflare + +# Cloudflare Workers: +npx create-remix@latest --template remix-run/remix/templates/cloudflare-workers ``` These templates include a `vite.config.ts` file which is where the Remix Vite plugin is configured. diff --git a/templates/cloudflare-workers/.eslintrc.cjs b/templates/cloudflare-workers/.eslintrc.cjs new file mode 100644 index 00000000000..c3128a897c8 --- /dev/null +++ b/templates/cloudflare-workers/.eslintrc.cjs @@ -0,0 +1,83 @@ +/** + * This is intended to be a basic starting point for linting in your app. + * It relies on recommended configs out of the box for simplicity, but you can + * and should modify this configuration to best suit your team's needs. + */ + +/** @type {import('eslint').Linter.Config} */ +module.exports = { + root: true, + parserOptions: { + ecmaVersion: "latest", + sourceType: "module", + ecmaFeatures: { + jsx: true, + }, + }, + env: { + browser: true, + commonjs: true, + es6: true, + }, + + // Base config + extends: ["eslint:recommended"], + + overrides: [ + // React + { + files: ["**/*.{js,jsx,ts,tsx}"], + plugins: ["react", "jsx-a11y"], + extends: [ + "plugin:react/recommended", + "plugin:react/jsx-runtime", + "plugin:react-hooks/recommended", + "plugin:jsx-a11y/recommended", + ], + settings: { + react: { + version: "detect", + }, + formComponents: ["Form"], + linkComponents: [ + { name: "Link", linkAttribute: "to" }, + { name: "NavLink", linkAttribute: "to" }, + ], + "import/resolver": { + typescript: {}, + }, + }, + }, + + // Typescript + { + files: ["**/*.{ts,tsx}"], + plugins: ["@typescript-eslint", "import"], + parser: "@typescript-eslint/parser", + settings: { + "import/internal-regex": "^~/", + "import/resolver": { + node: { + extensions: [".ts", ".tsx"], + }, + typescript: { + alwaysTryTypes: true, + }, + }, + }, + extends: [ + "plugin:@typescript-eslint/recommended", + "plugin:import/recommended", + "plugin:import/typescript", + ], + }, + + // Node + { + files: [".eslintrc.cjs"], + env: { + node: true, + }, + }, + ], +}; diff --git a/templates/cloudflare-workers/.gitignore b/templates/cloudflare-workers/.gitignore new file mode 100644 index 00000000000..41709094033 --- /dev/null +++ b/templates/cloudflare-workers/.gitignore @@ -0,0 +1,5 @@ +node_modules + +/.wrangler +/build +.env diff --git a/templates/cloudflare-workers/README.md b/templates/cloudflare-workers/README.md new file mode 100644 index 00000000000..bc4655f5e09 --- /dev/null +++ b/templates/cloudflare-workers/README.md @@ -0,0 +1,38 @@ +# Welcome to Remix + Vite! + +📖 See the [Remix docs](https://remix.run/docs) and the [Remix Vite docs](https://remix.run/docs/en/main/guides/vite) for details on supported features. + +## Typegen + +Generate types for your Cloudflare bindings in `wrangler.toml`: + +```sh +pnpm typegen +``` + +You will need to rerun typegen whenever you make changes to `wrangler.toml`. + +## Development + +Run the Vite dev server: + +```sh +npm run dev +``` + +To run Wrangler: + +```sh +npm run build +npm start +``` + +## Deployment + +If you don't already have an account, then [create a cloudflare account here](https://dash.cloudflare.com/sign-up) and after verifying your email address with Cloudflare, go to your dashboard and set up your free custom Cloudflare Workers subdomain. + +Once that's done, you should be able to deploy your app: + +```sh +pnpm run deploy +``` diff --git a/templates/cloudflare-workers/app/entry.client.tsx b/templates/cloudflare-workers/app/entry.client.tsx new file mode 100644 index 00000000000..94d5dc0de0f --- /dev/null +++ b/templates/cloudflare-workers/app/entry.client.tsx @@ -0,0 +1,18 @@ +/** + * By default, Remix will handle hydrating your app on the client for you. + * You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨ + * For more information, see https://remix.run/file-conventions/entry.client + */ + +import { RemixBrowser } from "@remix-run/react"; +import { startTransition, StrictMode } from "react"; +import { hydrateRoot } from "react-dom/client"; + +startTransition(() => { + hydrateRoot( + document, + + + + ); +}); diff --git a/templates/cloudflare-workers/app/entry.server.tsx b/templates/cloudflare-workers/app/entry.server.tsx new file mode 100644 index 00000000000..0d5c40a755e --- /dev/null +++ b/templates/cloudflare-workers/app/entry.server.tsx @@ -0,0 +1,43 @@ +/** + * By default, Remix will handle generating the HTTP Response for you. + * You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨ + * For more information, see https://remix.run/file-conventions/entry.server + */ + +import type { AppLoadContext, EntryContext } from "@remix-run/cloudflare"; +import { RemixServer } from "@remix-run/react"; +import { isbot } from "isbot"; +import { renderToReadableStream } from "react-dom/server"; + +export default async function handleRequest( + request: Request, + responseStatusCode: number, + responseHeaders: Headers, + remixContext: EntryContext, + // This is ignored so we can keep it in the template for visibility. Feel + // free to delete this parameter in your app if you're not using it! + // eslint-disable-next-line @typescript-eslint/no-unused-vars + loadContext: AppLoadContext +) { + const body = await renderToReadableStream( + , + { + signal: request.signal, + onError(error: unknown) { + // Log streaming rendering errors from inside the shell + console.error(error); + responseStatusCode = 500; + }, + } + ); + + if (isbot(request.headers.get("user-agent") || "")) { + await body.allReady; + } + + responseHeaders.set("Content-Type", "text/html"); + return new Response(body, { + headers: responseHeaders, + status: responseStatusCode, + }); +} diff --git a/templates/cloudflare-workers/app/root.tsx b/templates/cloudflare-workers/app/root.tsx new file mode 100644 index 00000000000..e82f26fd179 --- /dev/null +++ b/templates/cloudflare-workers/app/root.tsx @@ -0,0 +1,29 @@ +import { + Links, + Meta, + Outlet, + Scripts, + ScrollRestoration, +} from "@remix-run/react"; + +export function Layout({ children }: { children: React.ReactNode }) { + return ( + + + + + + + + + {children} + + + + + ); +} + +export default function App() { + return ; +} diff --git a/templates/cloudflare-workers/app/routes/_index.tsx b/templates/cloudflare-workers/app/routes/_index.tsx new file mode 100644 index 00000000000..a6521f5b800 --- /dev/null +++ b/templates/cloudflare-workers/app/routes/_index.tsx @@ -0,0 +1,35 @@ +import type { MetaFunction } from "@remix-run/cloudflare"; + +export const meta: MetaFunction = () => { + return [ + { title: "New Remix App" }, + { + name: "description", + content: "Welcome to Remix! Using Vite and Cloudflare Workers!", + }, + ]; +}; + +export default function Index() { + return ( +
+

Welcome to Remix (with Vite and Cloudflare Workers)

+ +
+ ); +} diff --git a/templates/cloudflare-workers/env.d.ts b/templates/cloudflare-workers/env.d.ts new file mode 100644 index 00000000000..78d2fb87787 --- /dev/null +++ b/templates/cloudflare-workers/env.d.ts @@ -0,0 +1,4 @@ +declare module "__STATIC_CONTENT_MANIFEST" { + const manifest: string; + export default manifest; +} diff --git a/templates/cloudflare-workers/load-context.ts b/templates/cloudflare-workers/load-context.ts new file mode 100644 index 00000000000..e9604b71ba0 --- /dev/null +++ b/templates/cloudflare-workers/load-context.ts @@ -0,0 +1,7 @@ +import { type PlatformProxy } from "wrangler"; + +declare module "@remix-run/cloudflare" { + interface AppLoadContext { + cloudflare: Omit, "dispose">; + } +} diff --git a/templates/cloudflare-workers/package.json b/templates/cloudflare-workers/package.json new file mode 100644 index 00000000000..2229a5e4674 --- /dev/null +++ b/templates/cloudflare-workers/package.json @@ -0,0 +1,44 @@ +{ + "private": true, + "sideEffects": false, + "type": "module", + "scripts": { + "build": "remix vite:build", + "deploy": "wrangler deploy", + "dev": "remix vite:dev", + "lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .", + "start": "wrangler dev ./server.js", + "typegen": "wrangler types", + "typecheck": "tsc" + }, + "dependencies": { + "@cloudflare/kv-asset-handler": "^0.1.3", + "@remix-run/cloudflare": "*", + "@remix-run/react": "*", + "@remix-run/server-runtime": "*", + "isbot": "^4.1.0", + "react": "^18.2.0", + "react-dom": "^18.2.0" + }, + "devDependencies": { + "@cloudflare/workers-types": "^4.20230518.0", + "@remix-run/dev": "*", + "@types/react": "^18.2.20", + "@types/react-dom": "^18.2.7", + "@typescript-eslint/eslint-plugin": "^6.7.4", + "@typescript-eslint/parser": "^6.7.4", + "eslint": "^8.38.0", + "eslint-import-resolver-typescript": "^3.6.1", + "eslint-plugin-import": "^2.28.1", + "eslint-plugin-jsx-a11y": "^6.7.1", + "eslint-plugin-react": "^7.33.2", + "eslint-plugin-react-hooks": "^4.6.0", + "typescript": "^5.1.6", + "vite": "^5.1.0", + "vite-tsconfig-paths": "^4.2.1", + "wrangler": "^3.24.0" + }, + "engines": { + "node": ">=18.0.0" + } +} diff --git a/templates/cloudflare-workers/public/favicon.ico b/templates/cloudflare-workers/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..8830cf6821b354114848e6354889b8ecf6d2bc61 GIT binary patch literal 16958 zcmeI3+jCXb9mnJN2h^uNlXH@jlam{_a8F3W{T}Wih>9YJpaf7TUbu)A5fv|h7OMfR zR;q$lr&D!wv|c)`wcw1?>4QT1(&|jdsrI2h`Rn)dTW5t$8pz=s3_5L?#oBxAowe8R z_WfPfN?F+@`q$D@rvC?(W!uWieppskmQ~YG*>*L?{img@tWpnYXZslxeh#TSUS3{q z1Ju6JcfQSbQuORq69@YK(X-3c9vC2c2a2z~zw=F=50@pm0PUiCAm!bAT?2jpM`(^b zC|2&Ngngt^<>oCv#?P(AZ`5_84x#QBPulix)TpkIAUp=(KgGo4CVS~Sxt zVoR4>r5g9%bDh7hi0|v$={zr>CHd`?-l4^Ld(Z9PNz9piFY+llUw_x4ou7Vf-q%$g z)&)J4>6Ft~RZ(uV>dJD|`nxI1^x{X@Z5S<=vf;V3w_(*O-7}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inXT~p z3;S|e$=&Tek9Wn!`cdY+d-w@o`37}x{(hl>ykB|%9yB$CGdIcl7Z?d&lJ%}QHck77 zJPR%C+s2w1_Dl_pxu6$Zi!`HmoD-%7OD@7%lKLL^Ixd9VlRSW*o&$^iQ2z+}hTgH) z#91TO#+jH<`w4L}XWOt(`gqM*uTUcky`O(mEyU|4dJoy6*UZJ7%*}ajuos%~>&P2j zk23f5<@GeV?(?`l=ih+D8t`d72xrUjv0wsg;%s1@*2p?TQ;n2$pV7h?_T%sL>iL@w zZ{lmc<|B7!e&o!zs6RW+u8+aDyUdG>ZS(v&rT$QVymB7sEC@VsK1dg^3F@K90-wYB zX!we79qx`(6LA>F$~{{xE8-3Wzyfe`+Lsce(?uj{k@lb97YTJt#>l*Z&LyKX@zjmu?UJC9w~;|NsB{%7G}y*uNDBxirfC EKbET!0{{R3 literal 0 HcmV?d00001 diff --git a/templates/cloudflare-workers/server.js b/templates/cloudflare-workers/server.js new file mode 100644 index 00000000000..dfcfb3f5184 --- /dev/null +++ b/templates/cloudflare-workers/server.js @@ -0,0 +1,57 @@ +import { getAssetFromKV } from "@cloudflare/kv-asset-handler"; +import { createRequestHandler } from "@remix-run/cloudflare"; +import * as remixBuild from "./build/server"; +// eslint-disable-next-line import/no-unresolved +import __STATIC_CONTENT_MANIFEST from "__STATIC_CONTENT_MANIFEST"; + +const MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST); +const handleRemixRequest = createRequestHandler(remixBuild); + +export default { + async fetch(request, env, ctx) { + try { + const url = new URL(request.url); + const ttl = url.pathname.startsWith("/assets/") + ? 60 * 60 * 24 * 365 // 1 year + : 60 * 5; // 5 minutes + return await getAssetFromKV( + { + request, + waitUntil: ctx.waitUntil.bind(ctx), + }, + { + ASSET_NAMESPACE: env.__STATIC_CONTENT, + ASSET_MANIFEST: MANIFEST, + cacheControl: { + browserTTL: ttl, + edgeTTL: ttl, + }, + } + ); + } catch (error) { + // No-op + } + + try { + const loadContext = { + cloudflare: { + // This object matches the return value from Wrangler's + // `getPlatformProxy` used during development via Remix's + // `cloudflareDevProxyVitePlugin`: + // https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy + cf: request.cf, + ctx: { + waitUntil: ctx.waitUntil, + passThroughOnException: ctx.passThroughOnException, + }, + caches, + env, + }, + }; + return await handleRemixRequest(request, loadContext); + } catch (error) { + console.log(error); + return new Response("An unexpected error occurred", { status: 500 }); + } + }, +}; diff --git a/templates/cloudflare-workers/tsconfig.json b/templates/cloudflare-workers/tsconfig.json new file mode 100644 index 00000000000..964fa4bea87 --- /dev/null +++ b/templates/cloudflare-workers/tsconfig.json @@ -0,0 +1,37 @@ +{ + "include": [ + "worker-configuration.d.ts", + "env.d.ts", + "**/*.ts", + "**/*.tsx", + "**/.server/**/*.ts", + "**/.server/**/*.tsx", + "**/.client/**/*.ts", + "**/.client/**/*.tsx" + ], + "compilerOptions": { + "lib": ["DOM", "DOM.Iterable", "ES2022"], + "types": [ + "@remix-run/cloudflare", + "@cloudflare/workers-types", + "vite/client" + ], + "isolatedModules": true, + "esModuleInterop": true, + "jsx": "react-jsx", + "moduleResolution": "Bundler", + "resolveJsonModule": true, + "target": "ES2022", + "strict": true, + "allowJs": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "baseUrl": ".", + "paths": { + "~/*": ["./app/*"] + }, + + // Remix takes care of building everything in `remix build`. + "noEmit": true + } +} diff --git a/templates/cloudflare-workers/vite.config.ts b/templates/cloudflare-workers/vite.config.ts new file mode 100644 index 00000000000..aed125b969c --- /dev/null +++ b/templates/cloudflare-workers/vite.config.ts @@ -0,0 +1,21 @@ +import { defineConfig } from "vite"; +import { + vitePlugin as remix, + cloudflareDevProxyVitePlugin, +} from "@remix-run/dev"; +import tsconfigPaths from "vite-tsconfig-paths"; + +export default defineConfig({ + plugins: [cloudflareDevProxyVitePlugin(), remix(), tsconfigPaths()], + ssr: { + resolve: { + conditions: ["workerd", "worker", "browser"], + }, + }, + resolve: { + mainFields: ["browser", "module", "main"], + }, + build: { + minify: true, + }, +}); diff --git a/templates/cloudflare-workers/worker-configuration.d.ts b/templates/cloudflare-workers/worker-configuration.d.ts new file mode 100644 index 00000000000..2a6ba0cb08c --- /dev/null +++ b/templates/cloudflare-workers/worker-configuration.d.ts @@ -0,0 +1,3 @@ +// Generated by Wrangler by running `wrangler types` + +interface Env {} diff --git a/templates/cloudflare-workers/wrangler.toml b/templates/cloudflare-workers/wrangler.toml new file mode 100644 index 00000000000..51b671d52f8 --- /dev/null +++ b/templates/cloudflare-workers/wrangler.toml @@ -0,0 +1,12 @@ +name = "remix-cloudflare-workers-template" + +main = "./server.js" +workers_dev = true +# https://developers.cloudflare.com/workers/platform/compatibility-dates +compatibility_date = "2023-04-20" + +[site] +bucket = "./build/client" + +[build] +command = "npm run build"