Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean content layer with --force #11541

Merged
merged 3 commits into from
Jul 24, 2024
Merged
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
4 changes: 4 additions & 0 deletions packages/astro/src/cli/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export async function build({ flags }: BuildOptions) {
tables: {
Flags: [
['--outDir <directory>', `Specify the output directory for the build.`],
[
'--force',
'Clear the content layer and content collection cache, forcing a full rebuild.',
],
['--help (-h)', 'See all available flags.'],
],
},
Expand Down
21 changes: 15 additions & 6 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
RuntimeMode,
} from '../../@types/astro.js';
import { injectImageEndpoint } from '../../assets/endpoint/config.js';
import { DATA_STORE_FILE } from '../../content/consts.js';
import { telemetry } from '../../events/index.js';
import { eventCliSession } from '../../events/session.js';
import {
Expand Down Expand Up @@ -67,12 +68,20 @@ export default async function build(
const logger = createNodeLogger(inlineConfig);
const { userConfig, astroConfig } = await resolveConfig(inlineConfig, 'build');
telemetry.record(eventCliSession('build', userConfig));
if (astroConfig.experimental.contentCollectionCache && options.force) {
const contentCacheDir = new URL('./content/', astroConfig.cacheDir);
if (fs.existsSync(contentCacheDir)) {
logger.debug('content', 'clearing content cache');
await fs.promises.rm(contentCacheDir, { force: true, recursive: true });
logger.warn('content', 'content cache cleared (force)');
if (options.force) {
if (astroConfig.experimental.contentCollectionCache) {
const contentCacheDir = new URL('./content/', astroConfig.cacheDir);
if (fs.existsSync(contentCacheDir)) {
logger.debug('content', 'clearing content cache');
await fs.promises.rm(contentCacheDir, { force: true, recursive: true });
logger.warn('content', 'content cache cleared (force)');
}
}
const dataStore = new URL(DATA_STORE_FILE, astroConfig.cacheDir);
if (fs.existsSync(dataStore)) {
logger.debug('content', 'clearing data store');
await fs.promises.rm(dataStore, { force: true });
logger.warn('content', 'data store cleared (force)');
}
}

Expand Down
17 changes: 16 additions & 1 deletion packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Content Layer', () => {
await fs
.unlink(new URL('./node_modules/.astro/data-store.json', fixture.config.root))
.catch(() => {});
await fixture.build({});
await fixture.build();
const rawJson = await fixture.readFile('/collections.json');
json = JSON.parse(rawJson);
});
Expand Down Expand Up @@ -133,6 +133,21 @@ describe('Content Layer', () => {
id: 'tabby',
});
});

it('updates the store on new builds', async () => {
assert.equal(json.increment.data.lastValue, 1);
await fixture.build();
const newJson = JSON.parse(await fixture.readFile('/collections.json'));
assert.equal(newJson.increment.data.lastValue, 2);
});

it('clears the store on new build with force flag', async () => {
let newJson = JSON.parse(await fixture.readFile('/collections.json'));
assert.equal(newJson.increment.data.lastValue, 2);
await fixture.build({}, { force: true });
newJson = JSON.parse(await fixture.readFile('/collections.json'));
assert.equal(newJson.increment.data.lastValue, 1);
});
});

describe('Dev', () => {
Expand Down
40 changes: 30 additions & 10 deletions packages/astro/test/fixtures/content-layer/src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,46 @@ const cats = defineCollection({
}),
});


// Absolute paths should also work
const absoluteRoot = new URL('../../content-outside-src', import.meta.url);

const spacecraft = defineCollection({
type: 'experimental_content',
loader: glob({ pattern: '*.md', base: absoluteRoot }),
schema: ({ image }) => z.object({
title: z.string(),
description: z.string(),
publishedDate: z.string(),
tags: z.array(z.string()),
heroImage: image().optional(),
cat: reference('cats').optional(),
}),
schema: ({ image }) =>
z.object({
title: z.string(),
description: z.string(),
publishedDate: z.string(),
tags: z.array(z.string()),
heroImage: image().optional(),
cat: reference('cats').optional(),
}),
});

const numbers = defineCollection({
type: 'experimental_content',
loader: glob({ pattern: 'src/data/glob-data/*', base: '.' }),
});

export const collections = { blog, dogs, cats, numbers, spacecraft };
const increment = defineCollection({
type: 'experimental_data',
loader: {
name: 'increment-loader',
load: async ({ store }) => {
const entry = store.get<{ lastValue: number }>('value');
const lastValue: number = entry?.data.lastValue ?? 0;
store.set({
id: 'value',
data: {
lastValue: lastValue + 1,
},
});
},
},
schema: z.object({
lastValue: z.number(),
}),
});

export const collections = { blog, dogs, cats, numbers, spacecraft, increment };
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ export async function GET() {
const entryWithReference = await getEntry('spacecraft', 'columbia-copy')
const referencedEntry = await getEntry(entryWithReference.data.cat)

return Response.json({ customLoader, fileLoader, dataEntry, simpleLoader, entryWithReference, referencedEntry });
const increment = await getEntry('increment', 'value')

return Response.json({ customLoader, fileLoader, dataEntry, simpleLoader, entryWithReference, referencedEntry, increment });
}
7 changes: 5 additions & 2 deletions packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ export async function loadFixture(inlineConfig) {
let devServer;

return {
build: async (extraInlineConfig = {}) => {
build: async (extraInlineConfig = {}, options = {}) => {
process.env.NODE_ENV = 'production';
return build(mergeConfig(inlineConfig, extraInlineConfig), { teardownCompiler: false });
return build(mergeConfig(inlineConfig, extraInlineConfig), {
teardownCompiler: false,
...options,
});
},
sync,
check: async (opts) => {
Expand Down
Loading