Skip to content

[Feature Request] option to skip request in dev middleware #12747

@OliverSpeir

Description

@OliverSpeir

Describe the solution

Frameworks with hybrid rendering (mix of prerendered and SSR routes) need a way to have the Cloudflare Vite plugin not intercept all requests during dev.

In Astro to support prerender environments other than workerd withastro/astro#15684, we need a way for the dev server to match the build behavior.

https://github.com/cloudflare/workers-sdk/blob/596b8a0cb/packages/vite-plugin-cloudflare/src/plugins/dev.ts#L177-L193

https://github.com/cloudflare/workers-sdk/blob/596b8a0cb/packages/vite-plugin-cloudflare/src/plugins/dev.ts#L267-L289

Proposal

Add an optional callback to PluginConfig that controls whether a request should be dispatched to miniflare or fall through to Vite:

shouldHandle?: (request: Request) => boolean | Promise<boolean>;

When shouldHandle returns false, the request skips both the preMiddleware worker dispatch and the catch-all post-middleware, falling through to Vite as if the plugin wasn't there.

One way to implement this would be marking skipped requests with a new kRequestType value so the post-middleware knows to call next():

  if (shouldHandle && !(await shouldHandle(request))) {
    req[kRequestType] = "skip";
    return next();
  }

Usage

  cloudflare({
    shouldHandle: (request) => {
      return !prerenderRoutes.has(new URL(request.url).pathname);
    },
  });

Metadata

Metadata

Labels

vite-pluginRelating to the `@cloudflare/vite-plugin` package

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions