Skip to content

Lazy load Cloudflare platform proxy #13016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/slow-coats-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

Lazy load Cloudflare platform proxy on first dev server request when using the `cloudflareDevProxy` Vite plugin to avoid creating unnecessary workerd processes
16 changes: 10 additions & 6 deletions packages/react-router-dev/vite/cloudflare-dev-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
}
},
configureServer: async (viteDevServer) => {
let { getPlatformProxy } = await importWrangler();
// Do not include `dispose` in Cloudflare context
let { dispose, ...cloudflare } = await getPlatformProxy<Env, Cf>(
restOptions
);
let context = { cloudflare };
let context: Awaited<ReturnType<typeof getContext>>;
let getContext = async () => {
let { getPlatformProxy } = await importWrangler();
// Do not include `dispose` in Cloudflare context
let { dispose, ...cloudflare } = await getPlatformProxy<Env, Cf>(
restOptions
);
return { cloudflare };
};
return () => {
if (!viteDevServer.config.server.middlewareMode) {
viteDevServer.middlewares.use(async (nodeReq, nodeRes, next) => {
Expand All @@ -124,6 +127,7 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(

let handler = createRequestHandler(build, "development");
let req = fromNodeRequest(nodeReq, nodeRes);
context ??= await getContext();
let loadContext = getLoadContext
? await getLoadContext({ request: req, context })
: context;
Expand Down