Skip to content

Commit 11e434c

Browse files
add support for run_worker_first
1 parent 6c76711 commit 11e434c

File tree

22 files changed

+389
-4
lines changed

22 files changed

+389
-4
lines changed

.changeset/all-doors-pick.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@cloudflare/vite-plugin": patch
33
---
44

5-
Add support for run_worker_first flag in Miniflare options
5+
Do not ignore `run_worker_first` in `vite preview` mode.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, test } from "vitest";
2+
import { isBuild, page, viteTestUrl } from "../../__test-utils__";
3+
4+
describe.skipIf(!isBuild)("run_worker_first support", () => {
5+
test("returns the correct home page", async () => {
6+
const content = await page.textContent("h1");
7+
expect(content).toBe("Vite + React");
8+
});
9+
10+
test("returns the response from the API", async () => {
11+
const button = page.getByRole("button", { name: "get-name" });
12+
const contentBefore = await button.innerText();
13+
expect(contentBefore).toBe("Name from API is: unknown");
14+
const responsePromise = page.waitForResponse((response) =>
15+
response.url().endsWith("/api/")
16+
);
17+
await button.click();
18+
await responsePromise;
19+
const contentAfter = await button.innerText();
20+
expect(contentAfter).toBe("Name from API is: Cloudflare");
21+
});
22+
23+
test("returns UNAUTH for the admin page", async () => {
24+
const response = await fetch(viteTestUrl + "/admin");
25+
expect(response.status).toBe(401);
26+
});
27+
28+
test("returns UNAUTH for an admin image", async () => {
29+
const response = await fetch(viteTestUrl + "/admin/secret.svg");
30+
expect(response.status).toBe(401);
31+
});
32+
33+
test("returns response for authorized admin page", async () => {
34+
const response = await fetch(viteTestUrl + "/admin?auth=xxx");
35+
expect(response.status).toBe(200);
36+
});
37+
38+
test("returns response for authorized admin image", async () => {
39+
const response = await fetch(viteTestUrl + "/admin/secret.svg?auth=xxx");
40+
expect(response.status).toBe(200);
41+
});
42+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + TS</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
interface Env {
2+
ASSETS: Fetcher;
3+
}
4+
5+
export default {
6+
async fetch(request, env) {
7+
const url = new URL(request.url);
8+
9+
// Protect assets in the `/admin` directory from "unauthorized" access!
10+
if (url.pathname.startsWith("/admin")) {
11+
const auth = url.searchParams.get("auth");
12+
if (!auth) {
13+
return new Response("Unauthorized access", { status: 401 });
14+
}
15+
}
16+
17+
if (url.pathname.startsWith("/api/")) {
18+
return Response.json({
19+
name: "Cloudflare",
20+
});
21+
}
22+
23+
return env.ASSETS.fetch(request);
24+
},
25+
} satisfies ExportedHandler<Env>;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@playground/run-worker-first",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "vite build --app",
7+
"check:types": "tsc --build",
8+
"dev": "vite dev",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"react": "^19.0.0",
13+
"react-dom": "^19.0.0"
14+
},
15+
"devDependencies": {
16+
"@cloudflare/vite-plugin": "workspace:*",
17+
"@cloudflare/workers-tsconfig": "workspace:*",
18+
"@cloudflare/workers-types": "^4.20250310.0",
19+
"@types/react": "^19.0.0",
20+
"@types/react-dom": "^19.0.0",
21+
"@vitejs/plugin-react": "^4.3.4",
22+
"typescript": "catalog:default",
23+
"vite": "catalog:vite-plugin",
24+
"wrangler": "workspace:*"
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
transition: filter 300ms;
13+
}
14+
.logo:hover {
15+
filter: drop-shadow(0 0 2em #646cffaa);
16+
}
17+
.logo.react:hover {
18+
filter: drop-shadow(0 0 2em #61dafbaa);
19+
}
20+
21+
@keyframes logo-spin {
22+
from {
23+
transform: rotate(0deg);
24+
}
25+
to {
26+
transform: rotate(360deg);
27+
}
28+
}
29+
30+
@media (prefers-reduced-motion: no-preference) {
31+
a:nth-of-type(2) .logo {
32+
animation: logo-spin infinite 20s linear;
33+
}
34+
}
35+
36+
.card {
37+
padding: 2em;
38+
}
39+
40+
.read-the-docs {
41+
color: #888;
42+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import viteLogo from "/vite.svg";
2+
import { useState } from "react";
3+
import reactLogo from "./assets/react.svg";
4+
import "./App.css";
5+
6+
function App() {
7+
const [count, setCount] = useState(0);
8+
const [name, setName] = useState("unknown");
9+
10+
return (
11+
<>
12+
<div>
13+
<a href="https://vite.dev" target="_blank">
14+
<img src={viteLogo} className="logo" alt="Vite logo" />
15+
</a>
16+
<a href="https://react.dev" target="_blank">
17+
<img src={reactLogo} className="logo react" alt="React logo" />
18+
</a>
19+
</div>
20+
<h1>Vite + React</h1>
21+
<div className="card">
22+
<button
23+
onClick={() => setCount((count) => count + 1)}
24+
aria-label="increment"
25+
>
26+
count is {count}
27+
</button>
28+
<p>
29+
Edit <code>src/App.tsx</code> and save to test HMR
30+
</p>
31+
</div>
32+
<div className="card">
33+
<button
34+
onClick={() => {
35+
fetch("/api/")
36+
.then((res) => res.json() as Promise<{ name: string }>)
37+
.then((data) => setName(data.name));
38+
}}
39+
aria-label="get-name"
40+
>
41+
Name from API is: {name}
42+
</button>
43+
<p>
44+
Edit <code>api/index.ts</code> to change the name
45+
</p>
46+
</div>
47+
<p className="read-the-docs">
48+
Click on the Vite and React logos to learn more
49+
</p>
50+
</>
51+
);
52+
}
53+
54+
export default App;
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)