From 011fa0f00ce457cb6b582d36b6b5b17aa89f0a70 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 6 Jan 2025 16:11:34 +0100 Subject: [PATCH] fix: do not generate a stub for content types (#12909) --- .changeset/great-hotels-reply.md | 5 +++++ packages/astro/src/core/sync/index.ts | 10 +++++----- packages/astro/src/types/astro.ts | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 .changeset/great-hotels-reply.md diff --git a/.changeset/great-hotels-reply.md b/.changeset/great-hotels-reply.md new file mode 100644 index 0000000000..b46aefff0b --- /dev/null +++ b/.changeset/great-hotels-reply.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a case where `astro:content` types would be erased when restarting the dev server diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index 9e2a2de22d..b1e75097da 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -150,17 +150,15 @@ export async function syncInternal({ settings.timer.end('Sync content layer'); } else { const paths = getContentPaths(settings.config, fs); - // Content is synced after writeFiles. That means references are not created - // To work around it, we create a stub so the reference is created and content - // sync will override the empty file if ( paths.config.exists || // Legacy collections don't require a config file (settings.config.legacy?.collections && fs.existsSync(paths.contentDir)) ) { + // We only create the reference, without a stub to avoid overriding the + // already generated types settings.injectedTypes.push({ filename: CONTENT_TYPES_FILE, - content: '', }); } } @@ -182,7 +180,9 @@ function writeInjectedTypes(settings: AstroSettings, fs: typeof fsMod) { for (const { filename, content } of settings.injectedTypes) { const filepath = fileURLToPath(new URL(filename, settings.dotAstroDir)); fs.mkdirSync(dirname(filepath), { recursive: true }); - fs.writeFileSync(filepath, content, 'utf-8'); + if (content) { + fs.writeFileSync(filepath, content, 'utf-8'); + } references.push(normalizePath(relative(fileURLToPath(settings.dotAstroDir), filepath))); } diff --git a/packages/astro/src/types/astro.ts b/packages/astro/src/types/astro.ts index 1227ed381a..b3150d9632 100644 --- a/packages/astro/src/types/astro.ts +++ b/packages/astro/src/types/astro.ts @@ -64,7 +64,8 @@ export interface AstroSettings { latestAstroVersion: string | undefined; serverIslandMap: NonNullable; serverIslandNameMap: NonNullable; - injectedTypes: Array; + // This makes content optional. Internal only so it's not optional on InjectedType + injectedTypes: Array & Partial>>; /** * Determine if the build output should be a static, dist folder or a adapter-based server output * undefined when unknown