Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/two-experts-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug where the `glob()` content loader kept stale entries in the data store after the last file in a collection was deleted. Empty collections are now pruned correctly, and the file watcher is registered in dev so the first file added to an empty collection is picked up without a restart.
1 change: 0 additions & 1 deletion packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ export function glob(globOptions: GlobOptions & { [secretLegacyFlag]?: boolean }
logger.warn(
`No files found matching "${globOptions.pattern}" in directory "${relativePath}"`,
);
return;
}

function configForFile(file: string) {
Expand Down
40 changes: 39 additions & 1 deletion packages/astro/test/units/content-layer/glob-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import { mkdirSync, writeFileSync } from 'node:fs';
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -513,6 +513,44 @@ describe('Glob Loader', () => {
assert.ok(warnings.some((w) => w.includes('No files found matching')));
});

it('prunes stale entries when the last file in a collection is deleted', async () => {
const tempDir = createTempDir();
const contentDir = join(fileURLToPath(tempDir), 'src', 'content', 'posts');
mkdirSync(contentDir, { recursive: true });
writeFileSync(join(contentDir, 'post.md'), '---\ntitle: Post MD\n---\nContent MD');

const store = new MutableDataStore();
const settings = createMinimalSettings(tempDir, {
contentEntryTypes: [createMarkdownEntryType()],
});
const logger = new AstroLogger({
destination: { write: () => true },
level: 'silent',
});

const collections = {
posts: defineCollection({
loader: glob({ pattern: '*.md', base: 'src/content/posts' }),
}),
};

const contentLayer = new ContentLayer({
settings,
logger,
store,
contentConfigObserver: createTestConfigObserver(collections),
});

await contentLayer.sync();
assert.equal(store.values('posts').length, 1);

// Delete the only file, leaving the (still existing) directory in place
rmSync(join(contentDir, 'post.md'));

await contentLayer.sync();
assert.equal(store.values('posts').length, 0);
});

it('throws on duplicate IDs when prerenderConflictBehavior is error', async () => {
const tempDir = createTempDir();
const contentDir = join(fileURLToPath(tempDir), 'src', 'content', 'posts');
Expand Down
Loading