Skip to content

Commit

Permalink
Only use router worker when assets is enabled (cloudflare#115)
Browse files Browse the repository at this point in the history
* Added getEntryWorker function

* Remove unnecessary logic in middleware

* Removed unnecessary async

* Used consistent node:path import
  • Loading branch information
jamesopstad authored Dec 17, 2024
1 parent 1e7ee4a commit b1e5987
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import type { Miniflare } from 'miniflare';

export const ROUTER_WORKER_NAME = '__router-worker__';
export const ASSET_WORKER_NAME = '__asset-worker__';
export const ASSET_WORKERS_COMPATIBILITY_DATE = '2024-10-04';

export function getRouterWorker(miniflare: Miniflare) {
return miniflare.getWorker(ROUTER_WORKER_NAME);
}
22 changes: 22 additions & 0 deletions packages/vite-plugin-cloudflare/src/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'node:assert';
import { ROUTER_WORKER_NAME } from './constants';
import type { ResolvedPluginConfig } from './plugin-config';
import type { Miniflare } from 'miniflare';

export function getDevEntryWorker(
resolvedPluginConfig: ResolvedPluginConfig,
miniflare: Miniflare,
) {
const entryWorkerConfig =
resolvedPluginConfig.type === 'assets-only'
? resolvedPluginConfig.config
: resolvedPluginConfig.workers[
resolvedPluginConfig.entryWorkerEnvironmentName
];

assert(entryWorkerConfig, 'Unexpected error: No entry worker configuration');

return entryWorkerConfig.assets
? miniflare.getWorker(ROUTER_WORKER_NAME)
: miniflare.getWorker(entryWorkerConfig.name);
}
23 changes: 8 additions & 15 deletions packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import assert from 'node:assert';
import * as fs from 'node:fs';
import path from 'node:path';
import * as path from 'node:path';
import { createMiddleware } from '@hattip/adapter-node';
import { Miniflare } from 'miniflare';
import * as vite from 'vite';
import { getRouterWorker } from './assets';
import {
createCloudflareEnvironmentOptions,
initRunners,
} from './cloudflare-environment';
import { getDevEntryWorker } from './dev';
import {
getDevMiniflareOptions,
getPreviewMiniflareOptions,
Expand Down Expand Up @@ -209,21 +209,19 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin {
);

await initRunners(resolvedPluginConfig, viteDevServer, miniflare);
const routerWorker = await getRouterWorker(miniflare);
const entryWorker = await getDevEntryWorker(
resolvedPluginConfig,
miniflare,
);

const middleware = createMiddleware(async ({ request }) => {
return routerWorker.fetch(toMiniflareRequest(request), {
const middleware = createMiddleware(({ request }) => {
return entryWorker.fetch(toMiniflareRequest(request), {
redirect: 'manual',
}) as any;
});

return () => {
viteDevServer.middlewares.use((req, res, next) => {
if (!middleware) {
next();
return;
}

middleware(req, res, next);
});
};
Expand All @@ -241,11 +239,6 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin {

return () => {
vitePreviewServer.middlewares.use((req, res, next) => {
if (!middleware) {
next();
return;
}

middleware(req, res, next);
});
};
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ASSET_WORKER_NAME,
ASSET_WORKERS_COMPATIBILITY_DATE,
ROUTER_WORKER_NAME,
} from './assets';
} from './constants';
import type { CloudflareDevEnvironment } from './cloudflare-environment';
import type {
PersistState,
Expand Down

0 comments on commit b1e5987

Please sign in to comment.