Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/miniflare-workerd-assets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"miniflare": minor
---

Serve local static assets with workerd's native assets service

Local Workers with `assets` are now served by workerd's `assets` service, which routes each request to the User Worker or to a file as Cloudflare does, and sends the User Worker every other event (RPC, scheduled, tail). It reads files from the assets directory as requests arrive, so Miniflare no longer walks the directory to build a manifest at startup, and files added or changed while it runs are served at once. `_headers`, `_redirects`, `.assetsignore`, `html_handling`, `not_found_handling`, `base_path` and `run_worker_first` behave as before.

Miniflare no longer checks the asset count and size limits locally; `wrangler deploy` still does.
36 changes: 0 additions & 36 deletions packages/miniflare/scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,6 @@ const localExplorerWorkerPath = path.join(
"local-explorer",
"explorer.worker.ts"
);
const assetWorkerPaths = new Set([
path.join(workersRoot, "assets", "assets.worker.ts"),
path.join(workersRoot, "assets", "router.worker.ts"),
]);
const workersSharedSentryPath = path.join(
pkgRoot,
"..",
"workers-shared",
"utils",
"sentry.ts"
);
const noopSentryPath = path.join(workersRoot, "assets", "sentry-noop.ts");

/**
* Test fixtures that need to be transpiled by esbuild as part of the build.
* These are loaded dynamically by tests and must be pre-compiled.
Expand Down Expand Up @@ -141,28 +128,6 @@ const rewriteNodeToInternalPlugin = {
},
};

/**
* Replaces the production Sentry setup used by the asset and router Workers.
* Miniflare never binds the required Sentry credentials, so the production
* implementation always returns before initialising Toucan.
*
* @type {esbuild.Plugin}
*/
const noopSentryPlugin = {
name: "noop-sentry",
setup(build) {
build.onResolve({ filter: /sentry(?:\.ts)?$/ }, (args) => {
const importPath = path.resolve(args.resolveDir, args.path);
if (
importPath === workersSharedSentryPath ||
`${importPath}.ts` === workersSharedSentryPath
) {
return { path: noopSentryPath };
}
});
},
};

/**
* esbuild plugin that redirects Zod imports to the shared workerd extension.
*
Expand Down Expand Up @@ -259,7 +224,6 @@ const embedWorkersPlugin = {
args.path === miniflareZodExtensionPath
? [rewriteNodeToInternalPlugin]
: []),
...(assetWorkerPaths.has(args.path) ? [noopSentryPlugin] : []),
// The Zod extension must bundle the real package, while all other
// workers import that single shared copy.
...(args.path === miniflareZodExtensionPath
Expand Down
10 changes: 5 additions & 5 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import {
import { getLocalArtifactsNamespaceNames } from "./plugins/artifacts";
import { ArtifactsController } from "./plugins/artifacts/controller";
import { getArtifactsEventSubscriptions } from "./plugins/artifacts/events";
import { RPC_PROXY_SERVICE_NAME } from "./plugins/assets/constants";
import { ROUTER_SERVICE_NAME } from "./plugins/assets/constants";
import { BROWSER_VERSION } from "./plugins/browser-rendering/browser-version";
import { closeBrowserProcess } from "./plugins/browser-rendering/process";
import {
Expand Down Expand Up @@ -2241,7 +2241,7 @@ export class Miniflare {
);
if (maybeAssetTargetService && !binding.service?.entrypoint) {
assert(binding.service?.name);
binding.service.name = `${RPC_PROXY_SERVICE_NAME}:${targetWorkerName}`;
binding.service.name = `${ROUTER_SERVICE_NAME}:${targetWorkerName}`;
}
}
}
Expand Down Expand Up @@ -2339,7 +2339,7 @@ export class Miniflare {
const service =
workerOpts.config.assets && entrypoint === "default"
? {
name: `${RPC_PROXY_SERVICE_NAME}:${workerOpts.config.name}`,
name: `${ROUTER_SERVICE_NAME}:${workerOpts.config.name}`,
}
: {
name: getUserServiceName(serviceName),
Expand Down Expand Up @@ -2528,7 +2528,7 @@ export class Miniflare {
fallbackWorkerName:
this.#workerOpts[0].config.assets &&
!this.#workerOpts[0].config.name.startsWith("vitest-plugin-runner-")
? `${RPC_PROXY_SERVICE_NAME}:${this.#workerOpts[0].config.name}`
? `${ROUTER_SERVICE_NAME}:${this.#workerOpts[0].config.name}`
: getUserServiceName(this.#workerOpts[0].config.name),
tmpPath: this.#tmpPath,
log: this.#log,
Expand Down Expand Up @@ -3022,7 +3022,7 @@ export class Miniflare {
workerOpts.dev.unsafeOverrideFetchWorker
);
} else if (workerOpts.config.assets) {
defaultEntrypointService = `${RPC_PROXY_SERVICE_NAME}:${workerName}`;
defaultEntrypointService = `${ROUTER_SERVICE_NAME}:${workerName}`;
} else {
defaultEntrypointService = getUserServiceName(workerName);
}
Expand Down
3 changes: 0 additions & 3 deletions packages/miniflare/src/plugins/assets/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export const ASSETS_PLUGIN_NAME = "assets";
export const ASSETS_SERVICE_NAME = `${ASSETS_PLUGIN_NAME}:assets-service`;
export const ROUTER_SERVICE_NAME = `${ASSETS_PLUGIN_NAME}:router`;
export const RPC_PROXY_SERVICE_NAME = `${ASSETS_PLUGIN_NAME}:rpc-proxy`;
export const ASSETS_KV_SERVICE_NAME = `${ASSETS_PLUGIN_NAME}:kv`;
Loading
Loading