Skip to content

Conversation

RielJ
Copy link

@RielJ RielJ commented Oct 1, 2025

Fixes #10404.

Moved the miniflare dispose directly inside server.restart() instead of in the buildEnd()

Previous logs shows that miniflare is updated instead of being created again this will make the miniflare undefined after the server is configured and the post middleware is executed.

5:37:24 AM [vite] vite.config.ts changed, restarting server...
@CLOUDFLARE:VITE-PLUGIN 26460: From server.restart(): Restarting server...
@CLOUDFLARE:VITE-PLUGIN 26460: Updating the existing Miniflare instance
@CLOUDFLARE:VITE-PLUGIN 26460: Miniflare is ready
@CLOUDFLARE:VITE-PLUGIN 26460: Initializing the Vite module runners
@CLOUDFLARE:VITE-PLUGIN 26460: Initializing worker: my-react-router-app
@CLOUDFLARE:VITE-PLUGIN 26460: buildEnd: restarted
@CLOUDFLARE:VITE-PLUGIN 26460: buildEnd: disposing
@CLOUDFLARE:VITE-PLUGIN 26460: buildEnd: disposing Miniflare instance
5:37:24 AM [vite] server restarted.
@CLOUDFLARE:VITE-PLUGIN 26460: From server.restart(): Restarted server...
@CLOUDFLARE:VITE-PLUGIN 26460: miniflare: undefined
@CLOUDFLARE:VITE-PLUGIN 26460: miniflare: undefined

Making this code asserts as miniflare is undefined

// post middleware
viteDevServer.middlewares.use(
	createRequestHandler(async (request, req) => {
		assert(miniflare, `Miniflare not defined`);

		if (req[kRequestType] === "asset") {
			const assetWorker =
				await miniflare.getWorker(ASSET_WORKER_NAME);

			return assetWorker.fetch(request, { redirect: "manual" });
		} else {
			const routerWorker =
				await miniflare.getWorker(ROUTER_WORKER_NAME);

			return routerWorker.fetch(request, {
				redirect: "manual",
			});
		}
	})
);

After the changes it shows that miniflare is disposed correctly and is being created again when the server is restarting

6:04:16 AM [vite] vite.config.ts changed, restarting server...
@CLOUDFLARE:VITE-PLUGIN 90076: From server.restart(): Restarting server...
@CLOUDFLARE:VITE-PLUGIN 90076: From server.restart(): disposing Miniflare instance

@CLOUDFLARE:VITE-PLUGIN 90076: Creating new Miniflare instance
@CLOUDFLARE:VITE-PLUGIN 90076: Initializing the Vite module runners
@CLOUDFLARE:VITE-PLUGIN 90076: Initializing worker: my-react-router-app
@CLOUDFLARE:VITE-PLUGIN 90076: buildEnd: restarted
@CLOUDFLARE:VITE-PLUGIN 90076: buildEnd: disposing
6:04:17 AM [vite] server restarted.
@CLOUDFLARE:VITE-PLUGIN 90076: From server.restart(): Restarted server...

  • Tests
    • Tests included
    • Tests not necessary because: all tests still viable
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: a lifecycle update
  • Wrangler V3 Backport
    • Wrangler PR:
    • Not necessary because: not a patch change

@RielJ RielJ requested review from a team as code owners October 1, 2025 22:01
Copy link

changeset-bot bot commented Oct 1, 2025

🦋 Changeset detected

Latest commit: 355c972

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Oct 2, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@10841

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@10841

miniflare

npm i https://pkg.pr.new/miniflare@10841

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@10841

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@10841

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@10841

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@10841

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@10841

wrangler

npm i https://pkg.pr.new/wrangler@10841

commit: e542042

@RielJ
Copy link
Author

RielJ commented Oct 4, 2025

@edmundhung Hi! can you take a look at this?

@RielJ RielJ changed the title fix(vite-plugin): move miniflare dispose directy on server.restart vite-plugin-cloudflare: move miniflare dispose directy on server.restart Oct 4, 2025
Copy link

@support-designfitout support-designfitout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link

@support-designfitout support-designfitout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🐛 BUG:Server restart fails with 'Miniflare not defined' with the react-router template

2 participants