|
| 1 | +import type { Page } from "@playwright/test"; |
| 2 | +import { expect } from "@playwright/test"; |
| 3 | + |
| 4 | +import type { Files } from "./helpers/vite.js"; |
| 5 | +import { test, viteConfig } from "./helpers/vite.js"; |
| 6 | + |
| 7 | +const files: Files = async ({ port }) => ({ |
| 8 | + "vite.config.ts": ` |
| 9 | + import { |
| 10 | + vitePlugin as reactRouter, |
| 11 | + cloudflareDevProxyVitePlugin as reactRouterCloudflareDevProxy, |
| 12 | + } from "@react-router/dev"; |
| 13 | + import { getLoadContext } from "./load-context"; |
| 14 | +
|
| 15 | + export default { |
| 16 | + ${await viteConfig.server({ port })} |
| 17 | + plugins: [ |
| 18 | + reactRouterCloudflareDevProxy({ getLoadContext }), |
| 19 | + reactRouter(), |
| 20 | + ], |
| 21 | + } |
| 22 | + `, |
| 23 | + "load-context.ts": ` |
| 24 | + import { type AppLoadContext } from "@react-router/cloudflare"; |
| 25 | + import { type PlatformProxy } from "wrangler"; |
| 26 | +
|
| 27 | + type Env = { |
| 28 | + MY_KV: KVNamespace; |
| 29 | + } |
| 30 | + type Cloudflare = Omit<PlatformProxy<Env>, 'dispose'>; |
| 31 | +
|
| 32 | + declare module "@react-router/cloudflare" { |
| 33 | + interface AppLoadContext { |
| 34 | + cloudflare: Cloudflare; |
| 35 | + env2: Cloudflare["env"]; |
| 36 | + extra: string; |
| 37 | + } |
| 38 | + } |
| 39 | +
|
| 40 | + type GetLoadContext = (args: { |
| 41 | + request: Request; |
| 42 | + context: { cloudflare: Cloudflare }; |
| 43 | + }) => AppLoadContext; |
| 44 | +
|
| 45 | + export const getLoadContext: GetLoadContext = ({ context }) => { |
| 46 | + return { |
| 47 | + ...context, |
| 48 | + env2: context.cloudflare.env, |
| 49 | + extra: "stuff", |
| 50 | + }; |
| 51 | + }; |
| 52 | + `, |
| 53 | + "functions/[[page]].ts": ` |
| 54 | + import { createPagesFunctionHandler } from "@react-router/cloudflare"; |
| 55 | +
|
| 56 | + // @ts-ignore - the server build file is generated by \`react-router build\` |
| 57 | + import * as build from "../build/server"; |
| 58 | + import { getLoadContext } from "../load-context"; |
| 59 | +
|
| 60 | + export const onRequest = createPagesFunctionHandler({ |
| 61 | + build, |
| 62 | + getLoadContext, |
| 63 | + }); |
| 64 | + `, |
| 65 | + "wrangler.toml": ` |
| 66 | + kv_namespaces = [ |
| 67 | + { id = "abc123", binding="MY_KV" } |
| 68 | + ] |
| 69 | + `, |
| 70 | + "app/routes/_index.tsx": ` |
| 71 | + import { |
| 72 | + type LoaderFunctionArgs, |
| 73 | + type ActionFunctionArgs, |
| 74 | + json, |
| 75 | + Form, |
| 76 | + useLoaderData, |
| 77 | + } from "react-router"; |
| 78 | +
|
| 79 | + const key = "__my-key__"; |
| 80 | +
|
| 81 | + export async function loader({ context }: LoaderFunctionArgs) { |
| 82 | + const { MY_KV } = context.cloudflare.env; |
| 83 | + const value = await MY_KV.get(key); |
| 84 | + return json({ value, extra: context.extra }); |
| 85 | + } |
| 86 | +
|
| 87 | + export async function action({ request, context }: ActionFunctionArgs) { |
| 88 | + const { MY_KV } = context.env2; |
| 89 | +
|
| 90 | + if (request.method === "POST") { |
| 91 | + const formData = await request.formData(); |
| 92 | + const value = formData.get("value") as string; |
| 93 | + await MY_KV.put(key, value); |
| 94 | + return null; |
| 95 | + } |
| 96 | +
|
| 97 | + if (request.method === "DELETE") { |
| 98 | + await MY_KV.delete(key); |
| 99 | + return null; |
| 100 | + } |
| 101 | +
|
| 102 | + throw new Error(\`Method not supported: "\${request.method}"\`); |
| 103 | + } |
| 104 | +
|
| 105 | + export default function Index() { |
| 106 | + const { value, extra } = useLoaderData<typeof loader>(); |
| 107 | + return ( |
| 108 | + <div> |
| 109 | + <h1>Welcome to React Router + Cloudflare</h1> |
| 110 | + <p data-extra>Extra: {extra}</p> |
| 111 | + {value ? ( |
| 112 | + <> |
| 113 | + <p data-text>Value: {value}</p> |
| 114 | + <Form method="DELETE"> |
| 115 | + <button>Delete</button> |
| 116 | + </Form> |
| 117 | + </> |
| 118 | + ) : ( |
| 119 | + <> |
| 120 | + <p data-text>No value</p> |
| 121 | + <Form method="POST"> |
| 122 | + <label htmlFor="value">Set value:</label> |
| 123 | + <input type="text" name="value" id="value" required /> |
| 124 | + <br /> |
| 125 | + <button>Save</button> |
| 126 | + </Form> |
| 127 | + </> |
| 128 | + )} |
| 129 | + </div> |
| 130 | + ); |
| 131 | + } |
| 132 | + `, |
| 133 | +}); |
| 134 | + |
| 135 | +test("vite dev", async ({ page, dev }) => { |
| 136 | + let { port } = await dev(files, "vite-cloudflare-template"); |
| 137 | + await workflow({ page, port }); |
| 138 | +}); |
| 139 | + |
| 140 | +test("wrangler pages dev", async ({ page, wranglerPagesDev }) => { |
| 141 | + let { port } = await wranglerPagesDev(files); |
| 142 | + await workflow({ page, port }); |
| 143 | +}); |
| 144 | + |
| 145 | +async function workflow({ page, port }: { page: Page; port: number }) { |
| 146 | + await page.goto(`http://localhost:${port}/`, { |
| 147 | + waitUntil: "networkidle", |
| 148 | + }); |
| 149 | + await expect(page.locator("[data-extra]")).toHaveText("Extra: stuff"); |
| 150 | + await expect(page.locator("[data-text]")).toHaveText("No value"); |
| 151 | + |
| 152 | + await page.getByLabel("Set value:").fill("my-value"); |
| 153 | + await page.getByRole("button").click(); |
| 154 | + await expect(page.locator("[data-text]")).toHaveText("Value: my-value"); |
| 155 | + expect(page.errors).toEqual([]); |
| 156 | +} |
0 commit comments