diff --git a/content/workers/platform/sites/configuration.md b/content/workers/platform/sites/configuration.md index 1cccd187f339683f024603c88507c2bd9014f9ac..bda820afb8916ade3a5fa6afb6704d94b8238645 100644 --- a/content/workers/platform/sites/configuration.md +++ b/content/workers/platform/sites/configuration.md @@ -73,24 +73,20 @@ filename: wrangler.toml --- account_id = "95e..." name = "docs-site-blah" -type = "webpack" workers_dev = false [site] bucket = "./public" -entry-point = "workers-site" [env.production] name = "docs-site" -route = "https://example.com/docs*" -zone_id = "351c.." account_id = "b54f07a.." +route = { pattern = "https://example.com/docs*", zone_name = "example.com" } [env.staging] -zone_id = "ef47a..." account_id = "95e5d..." name = "docs-site-staging" -route = "https://staging.example.com/docs*" +route = { pattern = "https://staging.example.com/docs*", zone_name = "staging.example.com" } ``` ## Storage limits @@ -114,7 +110,6 @@ If you want to include only a certain set of files or directories in your `bucke ```toml [site] bucket = "./public" -entry-point = "workers-site" include = ["included_dir"] # must be an array. ``` @@ -127,7 +122,6 @@ If you want to exclude files or directories in your `bucket`, you can add an `ex ```toml [site] bucket = "./public" -entry-point = "workers-site" exclude = ["excluded_dir"] # must be an array. ``` diff --git a/content/workers/platform/sites/start-from-existing.md b/content/workers/platform/sites/start-from-existing.md index 6dd8e791f5793895832fd7fcdba0554c248f6f96..8a9508640b51eb553ec5c566ff6b269ee6a4ed48 100644 --- a/content/workers/platform/sites/start-from-existing.md +++ b/content/workers/platform/sites/start-from-existing.md @@ -58,23 +58,38 @@ To deploy a pre-existing static site project, start with a pre-generated site. W 4. Replace the contents of `src/index.ts` with the following code snippet: ```js - import { getAssetFromKV } from "@cloudflare/kv-asset-handler"; - - addEventListener("fetch", (event) => { - event.respondWith(handleEvent(event)); - }); - - async function handleEvent(event) { - try { - // Add logic to decide whether to serve an asset or run your original Worker code - return await getAssetFromKV(event); - } catch (e) { - let pathname = new URL(event.request.url).pathname; - return new Response(`"${pathname}" not found`, { - status: 404, - statusText: "not found", - }); - } + import { getAssetFromKV, NotFoundError } from "@cloudflare/kv-asset-handler"; + import manifestJSON from '__STATIC_CONTENT_MANIFEST'; + const assetManifest = JSON.parse(manifestJSON) + + export default { + async fetch(request, env, ctx) { + try { + return await getAssetFromKV( + { + request, + waitUntil(promise) { + return ctx.waitUntil(promise) + }, + }, + { + ASSET_NAMESPACE: env.__STATIC_CONTENT, + ASSET_MANIFEST: assetManifest, + }, + ) + } catch (e) { + if (e instanceof NotFoundError) { + const pathname = new URL(request.url).pathname; + return new Response(`"${pathname}" not found`, { + status: 404, + statusText: "not found", + }); + // ... + } else { + return new Response('An unexpected error occurred', { status: 500 }) + } + } + }, } ``` @@ -88,7 +103,7 @@ To deploy a pre-existing static site project, start with a pre-generated site. W 6. Publish your site to a [custom domain](/workers/get-started/guide/#optional-configure-for-deploying-to-a-registered-domain) that you own and have already attached as a Cloudflare zone. Add a `route` property to the `wrangler.toml` file. ```toml - route = "https://example.com/*" + route = { pattern = "https://example.com/*", zone_name = "example.com" } ``` {{<Aside type="note">}} diff --git a/content/workers/platform/sites/start-from-scratch.md b/content/workers/platform/sites/start-from-scratch.md index 49b60eff3800d3c04ad727e8004a7ad2ab806fef..16dcb88f66c0edc1b111332e61a9814dccf78b6d 100644 --- a/content/workers/platform/sites/start-from-scratch.md +++ b/content/workers/platform/sites/start-from-scratch.md @@ -61,7 +61,7 @@ The template project contains the following files and directories: - Publish your site to a [custom domain](/workers/get-started/guide/#optional-configure-for-deploying-to-a-registered-domain) that you own and have already attached as a Cloudflare zone: ```toml - route = "https://example.com/*" + route = { pattern = "https://example.com/*", zone_name = "example.com" } ``` {{<Aside type="note">}} diff --git a/content/workers/platform/sites/start-from-worker.md b/content/workers/platform/sites/start-from-worker.md index f131ff1549586ff9dc5539f9ba4bcc44b4a83ae4..150b7d6a6afbb2c3a8b86e16ee0af1c5d34a2749 100644 --- a/content/workers/platform/sites/start-from-worker.md +++ b/content/workers/platform/sites/start-from-worker.md @@ -33,23 +33,39 @@ If you have a pre-existing Worker project, you can use Workers Sites to serve st 4. Import the `getAssetFromKV()` function into your Worker script and use it to respond with static assets. ```js - import { getAssetFromKV } from "@cloudflare/kv-asset-handler"; - - addEventListener("fetch", (event) => { - event.respondWith(handleEvent(event)); - }); - - async function handleEvent(event) { - try { - // Add logic to decide whether to serve an asset or run your original Worker code - return await getAssetFromKV(event); - } catch (e) { - let pathname = new URL(event.request.url).pathname; - return new Response(`"${pathname}" not found`, { - status: 404, - statusText: "not found", - }); - } + import { getAssetFromKV, NotFoundError } from "@cloudflare/kv-asset-handler"; + import manifestJSON from '__STATIC_CONTENT_MANIFEST'; + const assetManifest = JSON.parse(manifestJSON) + + export default { + async fetch(request, env, ctx) { + try { + // Add logic to decide whether to serve an asset or run your original Worker code + return await getAssetFromKV( + { + request, + waitUntil(promise) { + return ctx.waitUntil(promise) + }, + }, + { + ASSET_NAMESPACE: env.__STATIC_CONTENT, + ASSET_MANIFEST: assetManifest, + }, + ) + } catch (e) { + if (e instanceof NotFoundError) { + const pathname = new URL(request.url).pathname; + return new Response(`"${pathname}" not found`, { + status: 404, + statusText: "not found", + }); + // ... + } else { + return new Response('An unexpected error occurred', { status: 500 }) + } + } + }, } ```