Skip to content

Latest commit

History

History
78 lines (60 loc) 路 2.23 KB

sites.md

File metadata and controls

78 lines (60 loc) 路 2.23 KB
order
4

馃寪 Workers Sites

This page refers to Workers Sites, not Cloudflare Pages. Cloudflare Pages are not directly supported by Miniflare. You must use Wrangler 2.

Enabling Sites

Workers Sites can be enabled by specifying a path to serve files from. You can optionally specify glob patterns to include/exclude. If you specify both include and exclude options, only include will be used and exclude will be ignored:

import ConfigTabs from "../components/mdx/config-tabs";

$ miniflare --site ./public # or -s
$ miniflare --site ./public --site-include upload_dir
$ miniflare --site ./public --site-exclude ignore_dir
---
filename: wrangler.toml
---
[site]
bucket = "./public"
# Below options are optional
include = ["upload_dir"]
exclude = ["ignore_dir"]
const mf = new Miniflare({
  sitePath: "./public",
  // Below options are optional
  siteInclude: ["upload_dir"],
  siteExclude: ["exclude_dir"],
});

The following worker will now serve static files from the ./public directory. Note that you'll need a build step to bundle @cloudflare/kv-asset-handler. See 馃洜 Builds for more details:

import { getAssetFromKV } from "@cloudflare/kv-asset-handler";

addEventListener("fetch", (event) => {
  event.respondWith(getAssetFromKV(event));
});

Internal Details

When you enable Workers Sites, a read-only KV namespace, bound to __STATIC_CONTENT, is created using the file system (without key sanitisation) as its storage. Each entry in the bound __STATIC_CONTENT_MANIFEST object contains a magic prefix that disables edge caching. This means the most up-to-date file are always loaded from disk. Miniflare also binds this object to the __STATIC_CONTENT_MANIFEST text module.