|
1 | 1 | import { nanoid } from "nanoid";
|
2 | 2 | import { Router } from 'itty-router'
|
| 3 | +import { getAssetFromKV } from '@cloudflare/kv-asset-handler'; |
| 4 | +import manifestJSON from '__STATIC_CONTENT_MANIFEST'; |
| 5 | +const assetManifest = JSON.parse(manifestJSON); |
3 | 6 |
|
4 | 7 | const router = Router()
|
5 | 8 |
|
6 |
| -router.get('/', () => new Response(JSON.stringify({ id: nanoid(5) }), { |
7 |
| - headers: { |
8 |
| - 'content-type': 'application/json;charset=UTF-8', |
| 9 | +router.get('/', async (request, env, ctx) => { |
| 10 | + try { |
| 11 | + // Add logic to decide whether to serve an asset or run your original Worker code |
| 12 | + return await getAssetFromKV( |
| 13 | + { |
| 14 | + request, |
| 15 | + waitUntil: ctx.waitUntil.bind(ctx), |
| 16 | + }, |
| 17 | + { |
| 18 | + ASSET_NAMESPACE: env.__STATIC_CONTENT, |
| 19 | + ASSET_MANIFEST: assetManifest, |
| 20 | + } |
| 21 | + ); |
| 22 | + } catch (e) { |
| 23 | + let pathname = new URL(request.url).pathname; |
| 24 | + return new Response(`"${pathname}" not found`, { |
| 25 | + status: 404, |
| 26 | + statusText: 'not found', |
| 27 | + }); |
9 | 28 | }
|
10 |
| -})) |
| 29 | +}) |
11 | 30 |
|
12 | 31 | router.get('/ws', (request) => {
|
13 | 32 | const upgradeHeader = request.headers.get("Upgrade");
|
@@ -35,6 +54,12 @@ router.all('*', () => new Response('Not Found.', { status: 404 }))
|
35 | 54 | export default {
|
36 | 55 | async fetch(request, env, ctx) {
|
37 | 56 | return router.handle(request)
|
38 |
| - } |
| 57 | + }, |
| 58 | + |
| 59 | + async scheduled(event, env, ctx) { |
| 60 | + ctx.waitUntil(() => { |
| 61 | + console.log(111) |
| 62 | + }); |
| 63 | + }, |
39 | 64 | }
|
40 | 65 |
|
0 commit comments