Skip to content

Commit 2a337c4

Browse files
committed
Add demo and fix types
1 parent 825492b commit 2a337c4

31 files changed

+2746
-250
lines changed

demos/cache-handlers/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# build output
2+
dist/
3+
# generated types
4+
.astro/
5+
6+
# dependencies
7+
node_modules/
8+
9+
# logs
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
pnpm-debug.log*
14+
15+
16+
# environment variables
17+
.env
18+
.env.production
19+
20+
# macOS-specific files
21+
.DS_Store
22+
23+
# jetbrains setting folder
24+
.idea/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"recommendations": ["astro-build.astro-vscode"],
3+
"unwantedRecommendations": []
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"command": "./node_modules/.bin/astro dev",
6+
"name": "Development server",
7+
"request": "launch",
8+
"type": "node-terminal"
9+
}
10+
]
11+
}

demos/cache-handlers/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Astro Starter Kit: Minimal
2+
3+
```sh
4+
pnpm create astro@latest -- --template minimal
5+
```
6+
7+
> 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun!
8+
9+
## 🚀 Project Structure
10+
11+
Inside of your Astro project, you'll see the following folders and files:
12+
13+
```text
14+
/
15+
├── public/
16+
├── src/
17+
│ └── pages/
18+
│ └── index.astro
19+
└── package.json
20+
```
21+
22+
Astro looks for `.astro` or `.md` files in the `src/pages/` directory. Each page is exposed as a route based on its file name.
23+
24+
There's nothing special about `src/components/`, but that's where we like to put any Astro/React/Vue/Svelte/Preact components.
25+
26+
Any static assets, like images, can be placed in the `public/` directory.
27+
28+
## 🧞 Commands
29+
30+
All commands are run from the root of the project, from a terminal:
31+
32+
| Command | Action |
33+
| :------------------------ | :----------------------------------------------- |
34+
| `pnpm install` | Installs dependencies |
35+
| `pnpm dev` | Starts local dev server at `localhost:4321` |
36+
| `pnpm build` | Build your production site to `./dist/` |
37+
| `pnpm preview` | Preview your build locally, before deploying |
38+
| `pnpm astro ...` | Run CLI commands like `astro add`, `astro check` |
39+
| `pnpm astro -- --help` | Get help using the Astro CLI |
40+
41+
## 👀 Want to learn more?
42+
43+
Feel free to check [our documentation](https://docs.astro.build) or jump into our [Discord server](https://astro.build/chat).
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
import { defineConfig } from "astro/config";
3+
import cloudflare from "@astrojs/cloudflare";
4+
5+
// https://astro.build/config
6+
export default defineConfig({
7+
output: "server",
8+
adapter: cloudflare({
9+
platformProxy: {
10+
enabled: true,
11+
persist: true,
12+
},
13+
}),
14+
vite: {
15+
build: {
16+
minify: false, // Better error messages during development
17+
},
18+
},
19+
});

demos/cache-handlers/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@demo/cache-handlers",
3+
"private": true,
4+
"type": "module",
5+
"version": "0.0.1",
6+
"scripts": {
7+
"dev": "astro dev",
8+
"build": "astro build",
9+
"preview": "wrangler dev",
10+
"preview:local": "wrangler dev --local",
11+
"deploy": "astro build && wrangler deploy",
12+
"wrangler": "wrangler",
13+
"types": "wrangler types",
14+
"astro": "astro"
15+
},
16+
"dependencies": {
17+
"astro": "^5.12.9",
18+
"@astrojs/cloudflare": "^12.6.0",
19+
"cache-handlers": "workspace:*",
20+
"@cloudflare/workers-types": "^4.20250204.0"
21+
},
22+
"devDependencies": {
23+
"wrangler": "^4.32.0"
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Loading
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { invalidateByTag, invalidateByPath, invalidateAll, getCacheStats } from "cache-handlers";
2+
3+
export async function POST({ request }: { request: Request }) {
4+
try {
5+
const formData = await request.formData();
6+
const type = formData.get("type") as string;
7+
const value = formData.get("value") as string;
8+
9+
let result: { success: boolean; count?: number; error?: string };
10+
11+
switch (type) {
12+
case "tag":
13+
if (!value) {
14+
return Response.json({ success: false, error: "Tag value is required" }, { status: 400 });
15+
}
16+
const tagCount = await invalidateByTag(value);
17+
result = { success: true, count: tagCount };
18+
break;
19+
20+
case "path":
21+
if (!value) {
22+
return Response.json({ success: false, error: "Path value is required" }, { status: 400 });
23+
}
24+
const pathCount = await invalidateByPath(value);
25+
result = { success: true, count: pathCount };
26+
break;
27+
28+
case "all":
29+
const allCount = await invalidateAll();
30+
result = { success: true, count: allCount };
31+
break;
32+
33+
default:
34+
return Response.json({ success: false, error: "Invalid invalidation type" }, { status: 400 });
35+
}
36+
37+
return Response.json(result);
38+
} catch (error) {
39+
console.error("Invalidation error:", error);
40+
return Response.json(
41+
{ success: false, error: "Internal server error" },
42+
{ status: 500 }
43+
);
44+
}
45+
}
46+
47+
export async function GET() {
48+
try {
49+
const stats = await getCacheStats();
50+
return Response.json({
51+
success: true,
52+
stats,
53+
});
54+
} catch (error) {
55+
console.error("Cache stats error:", error);
56+
return Response.json(
57+
{ success: false, error: "Failed to get cache stats" },
58+
{ status: 500 }
59+
);
60+
}
61+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Blocking SWR demo forces revalidation before serving a stale response via worker route logic
2+
export async function GET() {
3+
const issuedAt = new Date();
4+
5+
return Response.json({
6+
feature: "blocking",
7+
issuedAt: issuedAt.toISOString(),
8+
now: new Date().toISOString(),
9+
}, {
10+
headers: {
11+
"Cache-Control": "public, max-age=5, stale-while-revalidate=30",
12+
}
13+
});
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ETag demo: We set long-lived response with ETag auto-generated by cache handler.
2+
export async function GET() {
3+
const issuedAt = new Date();
4+
5+
return Response.json({
6+
feature: "etag",
7+
issuedAt: issuedAt.toISOString(),
8+
now: new Date().toISOString(),
9+
}, {
10+
headers: {
11+
"Cache-Control": "public, max-age=60",
12+
}
13+
});
14+
}

0 commit comments

Comments
 (0)