-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-react-server-client-hmr
- Loading branch information
Showing
33 changed files
with
2,044 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# react-ssr-workerd | ||
|
||
```sh | ||
pnpm dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { test, expect } from "@playwright/test"; | ||
|
||
test("basic", async ({ page }) => { | ||
await page.goto("/"); | ||
await expect(page.locator("#root")).toContainText("hydrated: true"); | ||
await expect(page.locator("#root")).toContainText("Count: 0"); | ||
await page.getByRole("button", { name: "+" }).click(); | ||
await expect(page.locator("#root")).toContainText("Count: 1"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>react-ssr-workerd</title> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, height=device-height, initial-scale=1.0" | ||
/> | ||
</head> | ||
<body> | ||
<script src="/src/entry-client" type="module"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@hiogawa/vite-environment-examples-react-ssr-workerd", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"test-e2e": "playwright test" | ||
}, | ||
"dependencies": { | ||
"react": "18.3.0-canary-6c3b8dbfe-20240226", | ||
"react-dom": "18.3.0-canary-6c3b8dbfe-20240226" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20240405.0", | ||
"@hiogawa/vite-plugin-workerd": "workspace:*", | ||
"@types/react": "18.2.72", | ||
"@types/react-dom": "18.2.22" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { defineConfig, devices } from "@playwright/test"; | ||
|
||
const port = Number(process.env["E2E_PORT"] || 6174); | ||
const command = process.env["E2E_PREVIEW"] | ||
? `pnpm preview --port ${port} --strict-port` | ||
: process.env["E2E_WORKERD"] | ||
? `pnpm dev-workerd --port ${port} --strict-port` | ||
: `pnpm dev --port ${port} --strict-port`; | ||
|
||
export default defineConfig({ | ||
testDir: "e2e", | ||
use: { | ||
trace: "on-first-retry", | ||
}, | ||
projects: [ | ||
{ | ||
name: "chromium", | ||
use: devices["Desktop Chrome"], | ||
}, | ||
], | ||
webServer: { | ||
command, | ||
port, | ||
}, | ||
forbidOnly: !!process.env["CI"], | ||
retries: process.env["CI"] ? 2 : 0, | ||
reporter: "list", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { createMiddleware } from "@hattip/adapter-node/native-fetch"; | ||
import { handler } from "../entry-server"; | ||
|
||
export default createMiddleware((ctx) => handler(ctx.request)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { handler } from "../entry-server"; | ||
|
||
export default { | ||
fetch(request: Request, env: unknown) { | ||
Object.assign(globalThis, { env }); | ||
return handler(request); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { tinyassert } from "@hiogawa/utils"; | ||
import ReactDomClient from "react-dom/client"; | ||
import Page from "./routes/page"; | ||
import React from "react"; | ||
|
||
async function main() { | ||
const el = document.getElementById("root"); | ||
tinyassert(el); | ||
React.startTransition(() => { | ||
ReactDomClient.hydrateRoot(el, <Page />); | ||
}); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import ReactDomServer from "react-dom/server.edge"; | ||
import Page from "./routes/page"; | ||
|
||
export async function handler(request: Request) { | ||
const url = new URL(request.url); | ||
if (url.pathname === "/api") { | ||
return apiHandler(request); | ||
} | ||
if (url.pathname === "/nodejs-compat") { | ||
const util = await import("node:util"); | ||
return new Response(util.format("hello %s", "world")); | ||
} | ||
|
||
const ssrHtml = ReactDomServer.renderToString(<Page />); | ||
let html = (await import("virtual:index-html")).default; | ||
html = html.replace(/<body>/, `<body><div id="root">${ssrHtml}</div>`); | ||
return new Response(html, { headers: { "content-type": "text/html" } }); | ||
} | ||
|
||
async function apiHandler(request: Request) { | ||
let count = Number(await env.kv.get("count")); | ||
if (request.method === "POST") { | ||
const { delta } = await request.json(); | ||
count += delta; | ||
await env.kv.put("count", String(count)); | ||
} | ||
return new Response(JSON.stringify({ count })); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from "react"; | ||
|
||
export default function Page() { | ||
const [count, setCount] = React.useState<number>(); | ||
|
||
const [hydrated, setHydrated] = React.useState(false); | ||
React.useEffect(() => { | ||
setHydrated(true); | ||
getCount().then(setCount); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<div>hydrated: {String(hydrated)}</div> | ||
<div>Count: {count ?? "..."}</div> | ||
<button onClick={async () => changeCount(-1).then(setCount)}>-1</button> | ||
<button onClick={async () => changeCount(+1).then(setCount)}>+1</button> | ||
</div> | ||
); | ||
} | ||
|
||
async function getCount() { | ||
const res = await fetch("/api"); | ||
const { count } = await res.json(); | ||
return count as number; | ||
} | ||
|
||
async function changeCount(delta: number) { | ||
const res = await fetch("/api", { | ||
method: "POST", | ||
body: JSON.stringify({ delta }), | ||
}); | ||
const { count } = await res.json(); | ||
return count as number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module "react-dom/server.edge" { | ||
export * from "react-dom/server"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module "virtual:index-html" { | ||
const src: string; | ||
export default src; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare const env: { | ||
kv: import("@cloudflare/workers-types").KVNamespace; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "@tsconfig/strictest/tsconfig.json", | ||
"include": ["src", "vite.config.ts", "e2e", "playwright.config.ts"], | ||
"compilerOptions": { | ||
"exactOptionalPropertyTypes": false, | ||
"verbatimModuleSyntax": true, | ||
"noEmit": true, | ||
"moduleResolution": "Bundler", | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"lib": ["ESNext", "DOM"], | ||
"types": ["vite/client"], | ||
"jsx": "react-jsx" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { defineConfig } from "vite"; | ||
import react from "@vitejs/plugin-react"; | ||
import { vitePluginWorkerd } from "@hiogawa/vite-plugin-workerd"; | ||
import { vitePluginVirtualIndexHtml } from "../react-ssr/vite.config"; | ||
import { Log } from "miniflare"; | ||
|
||
export default defineConfig((_env) => ({ | ||
clearScreen: false, | ||
appType: "custom", | ||
plugins: [ | ||
react(), | ||
vitePluginWorkerd({ | ||
entry: "/src/adapters/workerd.ts", | ||
miniflare: { | ||
log: new Log(), | ||
}, | ||
wrangler: { | ||
configPath: "./wrangler.toml", | ||
}, | ||
}), | ||
vitePluginVirtualIndexHtml(), | ||
], | ||
environments: { | ||
workerd: { | ||
// [feedback] how to prevent deps optimization to inject this? still `ssr.target: "webworker"` needed? | ||
// import { createRequire } from 'module';const require = createRequire(import.meta.url); | ||
nodeCompatible: false, | ||
webCompatible: true, | ||
resolve: { | ||
noExternal: true, | ||
}, | ||
dev: { | ||
optimizeDeps: { | ||
include: [ | ||
"react", | ||
"react/jsx-runtime", | ||
"react/jsx-dev-runtime", | ||
"react-dom/server.edge", | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
ssr: { | ||
target: "webworker", | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
compatibility_date = "2024-01-01" | ||
compatibility_flags = ["nodejs_compat"] | ||
kv_namespaces = [ | ||
{ binding = "kv", id = "test-namespace" } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { handler } from "../entry-server"; | ||
|
||
export default { | ||
fetch: handler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.