Skip to content

Commit 4e8ad9a

Browse files
authored
Speed up dev server startup by deferring server app and content config compilation (#17955)
* perf: defer dev server app and content config compilation during dev startup The dev server app module graph was previously compiled and evaluated on the critical path inside the astro:server configureServer hook, blocking dev server creation on ~400ms of on-demand Vite compilation. The content config load had the same shape during types generation. Both are now kicked off early during server creation and awaited lazily by the request handlers / types generator instead. The dev server app compile is serialized after the content config load so the two don't contend for the main thread. Compile failures are still logged at startup and surface on first request. Measured on a Starlight project (150 pages, warm): dev server ready time drops from ~910ms to ~550ms, and config-change restarts from ~700ms to ~250ms. * perf(dev): defer dev server app compile to the first request * fix(dev): correct types of lazily-created dev server app handler * fix(dev): finish background setup before shutdown * fix(dev): start background setup concurrently * test: allow content watcher time in frontmatter test * perf(dev): serialize background server setup
1 parent 0037c1a commit 4e8ad9a

7 files changed

Lines changed: 141 additions & 20 deletions

File tree

‎.changeset/quick-pandas-dev.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Improves dev server startup time. The content config and dev server app module graphs now begin compiling during server creation without blocking the server from listening. Request handling waits for the shared setup result when needed, cutting `astro dev` ready time by roughly a third on projects with a content config.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type fsMod from 'node:fs';
2+
import type { RunnableDevEnvironment } from 'vite';
3+
import type { AstroLogger } from '../core/logger/core.js';
4+
import type { AstroSettings } from '../types/astro.js';
5+
import { getContentPaths, reloadContentConfigObserver } from './utils.js';
6+
7+
/**
8+
* Starts the content config load as early as possible during dev startup so its
9+
* on-demand Vite compilation overlaps other startup work instead of blocking it.
10+
*
11+
* The types generator (`createContentTypesGenerator#init`) and the dev server
12+
* app compile both dedupe on the same in-flight promise, so the config is
13+
* compiled and evaluated exactly once per dev server instance.
14+
*/
15+
let inFlight: Promise<void> | undefined;
16+
17+
export function getContentConfigLoadPromise(): Promise<void> | undefined {
18+
return inFlight;
19+
}
20+
21+
export function kickOffContentConfigLoad({
22+
settings,
23+
fs,
24+
logger,
25+
environment,
26+
}: {
27+
settings: AstroSettings;
28+
fs: typeof fsMod;
29+
logger: AstroLogger;
30+
environment: RunnableDevEnvironment;
31+
}): Promise<void> {
32+
if (inFlight) {
33+
return inFlight;
34+
}
35+
const contentPaths = getContentPaths(
36+
settings.config,
37+
fs,
38+
settings.config.legacy?.collectionsBackwardsCompat,
39+
);
40+
if (!contentPaths.config.exists) {
41+
return Promise.resolve();
42+
}
43+
inFlight = reloadContentConfigObserver({
44+
fs,
45+
settings,
46+
environment,
47+
logger,
48+
}).finally(() => {
49+
inFlight = undefined;
50+
});
51+
return inFlight;
52+
}

‎packages/astro/src/content/types-generator.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
getEntryType,
4040
reloadContentConfigObserver,
4141
} from './utils.js';
42+
import { getContentConfigLoadPromise } from './config-prewarm.js';
4243
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../core/constants.js';
4344

4445
type ChokidarEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
@@ -137,14 +138,23 @@ export async function createContentTypesGenerator({
137138
return { shouldGenerateTypes: false };
138139
}
139140
if (fileType === 'config') {
140-
await reloadContentConfigObserver({
141-
fs,
142-
settings,
143-
environment: viteServer.environments[
144-
ASTRO_VITE_ENVIRONMENT_NAMES.astro
145-
] as RunnableDevEnvironment,
146-
logger,
147-
});
141+
// Dev server creation may already be loading this config. Wait for that
142+
// exact load when present; otherwise this path owns the reload. Do not
143+
// infer ownership from the global observer status because `astro sync`
144+
// can retain a settled status from an earlier invocation in the process.
145+
const prewarm = getContentConfigLoadPromise();
146+
if (prewarm) {
147+
await prewarm;
148+
} else {
149+
await reloadContentConfigObserver({
150+
fs,
151+
settings,
152+
environment: viteServer.environments[
153+
ASTRO_VITE_ENVIRONMENT_NAMES.astro
154+
] as RunnableDevEnvironment,
155+
logger,
156+
});
157+
}
148158
return { shouldGenerateTypes: true };
149159
}
150160

‎packages/astro/src/core/constants.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ export const fetchStateSymbol = Symbol.for('astro.fetchState');
5656
*/
5757
export const devPrerenderMiddlewareSymbol = Symbol.for('astro.devPrerenderMiddleware');
5858

59+
/**
60+
* A promise for background dev server app setup that must settle before Vite's
61+
* runnable environments are closed.
62+
*/
63+
export const devServerAppReadySymbol = Symbol.for('astro.devServerAppReady');
64+
5965
/**
6066
* The symbol used as a field on the request object to store a cleanup callback associated with aborting the request when the underlying socket closes.
6167
*/

‎packages/astro/src/core/dev/container.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../../integrations/hooks.js';
1111
import type { AstroSettings } from '../../types/astro.js';
1212
import type { AstroInlineConfig } from '../../types/public/config.js';
13+
import { devServerAppReadySymbol } from '../constants.js';
1314
import { createVite } from '../create-vite.js';
1415
import type { AstroLogger } from '../logger/core.js';
1516
import { createRoutesList } from '../routing/create-manifest.js';
@@ -152,6 +153,10 @@ export async function createContainer({
152153
}
153154

154155
async function closeContainer({ viteServer, settings, logger }: Container) {
156+
// Background app setup uses the runnable environment's module runner. Let it
157+
// settle before Vite disconnects that runner so shutdown cannot strand an
158+
// in-flight module import.
159+
await (viteServer as any)[devServerAppReadySymbol];
155160
await viteServer.close();
156161
await runHookServerDone({
157162
config: settings.config,

‎packages/astro/src/vite-plugin-astro-server/plugin.ts‎

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import { AsyncLocalStorage } from 'node:async_hooks';
2+
import nodeFs from 'node:fs';
23
import { IncomingMessage } from 'node:http';
34
import type * as vite from 'vite';
45
import { isRunnableDevEnvironment, type RunnableDevEnvironment } from 'vite';
56
import type { RouteInfo, SSRManifest } from '../core/app/types.js';
6-
import { ASTRO_VITE_ENVIRONMENT_NAMES, devPrerenderMiddlewareSymbol } from '../core/constants.js';
7+
import {
8+
ASTRO_VITE_ENVIRONMENT_NAMES,
9+
devPrerenderMiddlewareSymbol,
10+
devServerAppReadySymbol,
11+
} from '../core/constants.js';
712
import { getViteErrorPayload } from '../core/errors/dev/index.js';
813
import { AstroError, AstroErrorData } from '../core/errors/index.js';
914
import type { AstroLogger } from '../core/logger/core.js';
1015
import { createViteLoader } from '../core/module-loader/index.js';
1116
import { matchAllRoutes } from '../core/routing/match.js';
1217
import { SERIALIZED_MANIFEST_ID } from '../manifest/serialized.js';
1318
import type { AstroSettings } from '../types/astro.js';
19+
import { kickOffContentConfigLoad } from '../content/config-prewarm.js';
1420
import { ASTRO_DEV_SERVER_APP_ID } from '../vite-plugin-app/index.js';
1521
import { baseMiddleware } from './base.js';
1622
import { createController } from './controller.js';
@@ -67,15 +73,48 @@ export default function createVitePluginAstroServer({
6773
return { controller, handler, loader, manifest, environment };
6874
}
6975

70-
const ssrHandler = runnableSsrEnvironment
71-
? await createHandler(runnableSsrEnvironment)
76+
// Kick off the content config load and dev server app compile as early
77+
// as possible. Neither is needed until the first request, so install the
78+
// middleware synchronously and let request handlers await the in-flight
79+
// results without blocking server creation. Serialize the app compile
80+
// after the config load so they do not contend for the main thread.
81+
const astroEnvironment = viteServer.environments[ASTRO_VITE_ENVIRONMENT_NAMES.astro];
82+
const runnableAstroEnvironment = isRunnableDevEnvironment(astroEnvironment)
83+
? (astroEnvironment as RunnableDevEnvironment)
7284
: undefined;
73-
const prerenderHandler = runnablePrerenderEnvironment
74-
? await createHandler(runnablePrerenderEnvironment)
85+
const contentConfigLoad = runnableAstroEnvironment
86+
? kickOffContentConfigLoad({
87+
settings,
88+
fs: nodeFs,
89+
logger,
90+
environment: runnableAstroEnvironment,
91+
})
92+
: Promise.resolve();
93+
94+
const ssrHandlerPromise = runnableSsrEnvironment
95+
? contentConfigLoad.then(() => createHandler(runnableSsrEnvironment))
7596
: undefined;
97+
const prerenderHandlerPromise = runnablePrerenderEnvironment
98+
? contentConfigLoad.then(() => createHandler(runnablePrerenderEnvironment))
99+
: undefined;
100+
101+
// Background setup uses the runnable environments' module runners. Keep
102+
// shutdown from disconnecting those runners while an import is in flight.
103+
(viteServer as any)[devServerAppReadySymbol] = Promise.allSettled(
104+
[contentConfigLoad, ssrHandlerPromise, prerenderHandlerPromise].filter(Boolean),
105+
).then(() => undefined);
106+
107+
// Compile failures surface here so startup still reports them, and also
108+
// as a rejected lazy await in the request handlers below.
109+
ssrHandlerPromise?.catch((error) => {
110+
logger.error(null, `Failed to create the dev server app: ${error?.message ?? error}`);
111+
});
112+
prerenderHandlerPromise?.catch((error) => {
113+
logger.error(null, `Failed to create the prerender server app: ${error?.message ?? error}`);
114+
});
76115
const localStorage = new AsyncLocalStorage();
77116

78-
function handleUnhandledRejection(rejection: any) {
117+
async function handleUnhandledRejection(rejection: any) {
79118
const error = AstroError.is(rejection)
80119
? rejection
81120
: new AstroError({
@@ -84,9 +123,11 @@ export default function createVitePluginAstroServer({
84123
});
85124
const store = localStorage.getStore();
86125
const handlers = [];
87-
if (ssrHandler) handlers.push(ssrHandler);
88-
if (prerenderHandler) handlers.push(prerenderHandler);
126+
if (ssrHandlerPromise) handlers.push(await ssrHandlerPromise.catch(() => undefined));
127+
if (prerenderHandlerPromise)
128+
handlers.push(await prerenderHandlerPromise.catch(() => undefined));
89129
for (const currentHandler of handlers) {
130+
if (!currentHandler) continue;
90131
if (store instanceof IncomingMessage) {
91132
setRouteError(currentHandler.controller.state, store.url!, error);
92133
}
@@ -104,7 +145,7 @@ export default function createVitePluginAstroServer({
104145
}
105146
}
106147

107-
if (ssrHandler || prerenderHandler) {
148+
if (runnableSsrEnvironment || runnablePrerenderEnvironment) {
108149
process.on('unhandledRejection', handleUnhandledRejection);
109150
viteServer.httpServer?.on('close', () => {
110151
process.off('unhandledRejection', handleUnhandledRejection);
@@ -137,7 +178,7 @@ export default function createVitePluginAstroServer({
137178
handle: secFetchMiddleware(logger, settings.config.security?.allowedDomains),
138179
});
139180

140-
if (prerenderHandler && shouldHandlePrerenderInCore) {
181+
if (runnablePrerenderEnvironment && shouldHandlePrerenderInCore) {
141182
viteServer.middlewares.use(
142183
async function astroDevPrerenderHandler(request, response, next) {
143184
if (request.url === undefined || !request.method) {
@@ -155,6 +196,7 @@ export default function createVitePluginAstroServer({
155196
}
156197

157198
try {
199+
const prerenderHandler = await prerenderHandlerPromise!;
158200
const pathname = decodeURI(new URL(request.url, 'http://localhost').pathname);
159201
const { routes } = (await prerenderHandler.environment.runner.import(
160202
'virtual:astro:routes',
@@ -184,7 +226,7 @@ export default function createVitePluginAstroServer({
184226
);
185227
}
186228

187-
if (ssrHandler) {
229+
if (runnableSsrEnvironment) {
188230
// Note that this function has a name so other middleware can find it.
189231
viteServer.middlewares.use(async function astroDevHandler(request, response) {
190232
if (request.url === undefined || !request.method) {
@@ -193,6 +235,7 @@ export default function createVitePluginAstroServer({
193235
return;
194236
}
195237

238+
const ssrHandler = await ssrHandlerPromise!;
196239
localStorage.run(request, () => {
197240
ssrHandler.handler(request, response);
198241
});

‎packages/astro/test/content-frontmatter.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('frontmatter (loadFixture)', () => {
2626
fs.writeFileSync(blogPath, originalContent);
2727
});
2828

29-
it('errors in content/ does not crash server', { timeout: 2000 }, async () => {
29+
it('errors in content/ does not crash server', { timeout: 5000 }, async () => {
3030
// Verify server is alive
3131
const res1 = await fixture.fetch('/');
3232
assert.equal(res1.status, 200);

0 commit comments

Comments
 (0)