Skip to content

Commit f5c8fee

Browse files
authoredMay 17, 2024··
Prevent cache content from being left in dist folder (#11073)
1 parent 8a80221 commit f5c8fee

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed
 

‎.changeset/brown-pens-type.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Prevent cache content from being left in dist folder
6+
7+
When `contentCollectionsCache` is enabled temporary cached content is copied into the `outDir` for processing. This fixes it so that this content is cleaned out, along with the rest of the temporary build JS.
+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const CHUNKS_PATH = 'chunks/';
2+
export const CONTENT_PATH = 'content/';

‎packages/astro/src/core/build/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ class AstroBuilder {
195195
viteConfig,
196196
};
197197

198-
const { internals, ssrOutputChunkNames } = await viteBuild(opts);
199-
await staticBuild(opts, internals, ssrOutputChunkNames);
198+
const { internals, ssrOutputChunkNames, contentFileNames } = await viteBuild(opts);
199+
await staticBuild(opts, internals, ssrOutputChunkNames, contentFileNames);
200200

201201
// Write any additionally generated assets to disk.
202202
this.timer.assetsStart = performance.now();

‎packages/astro/src/core/build/plugins/plugin-content.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ import {
2020
} from '../../path.js';
2121
import { isContentCollectionsCacheEnabled } from '../../util.js';
2222
import { addRollupInput } from '../add-rollup-input.js';
23-
import { CHUNKS_PATH } from '../consts.js';
23+
import { CHUNKS_PATH, CONTENT_PATH } from '../consts.js';
2424
import { type BuildInternals } from '../internal.js';
2525
import type { AstroBuildPlugin } from '../plugin.js';
2626
import { copyFiles } from '../static-build.js';
2727
import type { StaticBuildOptions } from '../types.js';
2828
import { encodeName } from '../util.js';
2929
import { extendManualChunks } from './util.js';
30+
import glob from 'fast-glob';
3031

31-
const CONTENT_CACHE_DIR = './content/';
32+
const CONTENT_CACHE_DIR = './' + CONTENT_PATH;
3233
const CONTENT_MANIFEST_FILE = './manifest.json';
3334
// IMPORTANT: Update this version when making significant changes to the manifest format.
3435
// Only manifests generated with the same version number can be compared.
@@ -454,9 +455,24 @@ export async function copyContentToCache(opts: StaticBuildOptions) {
454455

455456
await fsMod.promises.mkdir(cacheTmp, { recursive: true });
456457
await copyFiles(distContentRoot, cacheTmp, true);
457-
458458
await copyFiles(cacheTmp, contentCacheDir);
459+
460+
// Read the files from `dist/content/*` and `dist/chunks/*` so that
461+
// we can clean them out of the dist folder
462+
let files: string[] = [];
463+
await Promise.all([
464+
glob(`**/*.{mjs,json}`,{
465+
cwd: fileURLToPath(cacheTmp)
466+
}).then(f => files.push(...f.map(file => CONTENT_PATH + file))),
467+
glob(`**/*.{mjs,json}`,{
468+
cwd: fileURLToPath(new URL('./' + CHUNKS_PATH, config.outDir)),
469+
}).then(f => files.push(...f.map(file => CHUNKS_PATH + file))),
470+
]);
471+
472+
// Remove the tmp folder that's no longer needed.
459473
await fsMod.promises.rm(cacheTmp, { recursive: true, force: true });
474+
475+
return files;
460476
}
461477

462478
export function pluginContent(

‎packages/astro/src/core/build/static-build.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ export async function viteBuild(opts: StaticBuildOptions) {
107107
const ssrOutputs = viteBuildReturnToRollupOutputs(ssrOutput);
108108
const clientOutputs = viteBuildReturnToRollupOutputs(clientOutput ?? []);
109109
await runPostBuildHooks(container, ssrOutputs, clientOutputs);
110+
let contentFileNames: string[] | undefined = undefined;
110111
if (opts.settings.config.experimental.contentCollectionCache) {
111-
await copyContentToCache(opts);
112+
contentFileNames = await copyContentToCache(opts);
112113
}
113114
settings.timer.end('Client build');
114115

@@ -129,20 +130,21 @@ export async function viteBuild(opts: StaticBuildOptions) {
129130
}
130131
}
131132

132-
return { internals, ssrOutputChunkNames };
133+
return { internals, ssrOutputChunkNames, contentFileNames };
133134
}
134135

135136
export async function staticBuild(
136137
opts: StaticBuildOptions,
137138
internals: BuildInternals,
138-
ssrOutputChunkNames: string[]
139+
ssrOutputChunkNames: string[],
140+
contentFileNames?: string[]
139141
) {
140142
const { settings } = opts;
141143
switch (true) {
142144
case settings.config.output === 'static': {
143145
settings.timer.start('Static generate');
144146
await generatePages(opts, internals);
145-
await cleanServerOutput(opts, ssrOutputChunkNames, internals);
147+
await cleanServerOutput(opts, ssrOutputChunkNames, contentFileNames, internals);
146148
settings.timer.end('Static generate');
147149
return;
148150
}
@@ -431,11 +433,13 @@ async function cleanStaticOutput(
431433
async function cleanServerOutput(
432434
opts: StaticBuildOptions,
433435
ssrOutputChunkNames: string[],
436+
contentFileNames: string[] | undefined,
434437
internals: BuildInternals
435438
) {
436439
const out = getOutDirWithinCwd(opts.settings.config.outDir);
437440
// The SSR output chunks for Astro are all .mjs files
438-
const files = ssrOutputChunkNames.filter((f) => f.endsWith('.mjs'));
441+
const files = ssrOutputChunkNames.filter((f) => f.endsWith('.mjs'))
442+
.concat(contentFileNames ?? []);
439443
if (internals.manifestFileName) {
440444
files.push(internals.manifestFileName);
441445
}

‎packages/astro/test/experimental-content-collections-render.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ if (!isWindows) {
121121
// Includes styles
122122
assert.equal($('link[rel=stylesheet]').length, 1);
123123
});
124+
125+
it('content folder is cleaned', async () => {
126+
let found = true;
127+
try {
128+
await fixture.readFile('content/manifest.json');
129+
} catch {
130+
found = false;
131+
}
132+
assert.equal(found, false, 'manifest not in dist folder');
133+
});
134+
135+
it('chunks folder is cleaned', async () => {
136+
const files = await fixture.readdir('');
137+
assert.equal(files.includes('chunks'), false, 'chunks folder removed');
138+
});
124139
});
125140
});
126141
});

0 commit comments

Comments
 (0)
Please sign in to comment.