Skip to content
Open
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/great-clocks-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a race condition where the Vite dep optimizer could lose React dependencies in dev mode when using Astro Actions
36 changes: 26 additions & 10 deletions packages/astro/src/vite-plugin-integrations-container/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PluginContext } from 'rollup';
import type { Plugin as VitePlugin } from 'vite';
import type { Plugin as VitePlugin, ViteDevServer } from 'vite';
import { normalizePath } from 'vite';
import type { AstroLogger } from '../core/logger/core.js';
import { runHookServerSetup } from '../integrations/hooks.js';
Expand All @@ -14,29 +14,45 @@ export default function astroIntegrationsContainerPlugin({
settings: AstroSettings;
logger: AstroLogger;
}): VitePlugin {
let server: ViteDevServer | undefined;
return {
name: 'astro:integration-container',
async configureServer(server) {
if (server.config.isProduction) return;
await runHookServerSetup({ config: settings.config, server, logger });
async configureServer(_server) {
server = _server;
if (_server.config.isProduction) return;
await runHookServerSetup({ config: settings.config, server: _server, logger });
},
async buildStart() {
if (settings.injectedRoutes.length === settings.resolvedInjectedRoutes.length) return;
// Ensure the injectedRoutes are all resolved to their final paths through Rollup
settings.resolvedInjectedRoutes = await Promise.all(
settings.injectedRoutes.map((route) => resolveEntryPoint.call(this, route)),
settings.injectedRoutes.map((route) =>
resolveEntryPoint(route, server, this),
),
);
},
};
}

async function resolveEntryPoint(
this: PluginContext,
route: InternalInjectedRoute,
server: ViteDevServer | undefined,
pluginContext: PluginContext,
): Promise<ResolvedInjectedRoute> {
const resolvedId = await this.resolve(route.entrypoint.toString())
.then((res) => res?.id)
.catch(() => undefined);
const entrypoint = route.entrypoint.toString();
// In dev, resolve through the SSR environment's plugin container to avoid
// triggering the client dep optimizer's registerMissingImport, which can
// race against optimizer init and corrupt the metadata cache.
// In build, this.resolve() is safe since there's no dep optimizer race.
let resolvedId: string | undefined;
if (server) {
const resolved = await server.environments.ssr.pluginContainer.resolveId(entrypoint);
resolvedId = resolved?.id;
} else {
resolvedId = await pluginContext
.resolve(entrypoint)
.then((res) => res?.id)
.catch(() => undefined);
}
if (!resolvedId) return route;

const resolvedEntryPoint = new URL(`file://${normalizePath(resolvedId)}`);
Expand Down
Loading