Skip to content
Open
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/fix-missing-initial-files-deletion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Only record existing files in `initialFiles` during `parseEntry` so missing asset or content paths are not requested for deletion on save or delete.
204 changes: 204 additions & 0 deletions packages/keystatic/src/app/useItemData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { describe, expect, test } from 'vitest';

import { Config, collection } from '../config';
import { AssetsFormField, fields } from '../form/api';
import { getCollectionFormat } from './path-utils';
import { parseEntry } from './useItemData';

const encoder = new TextEncoder();

describe('parseEntry', () => {
test('does not include missing or mismatched asset files in initialFiles', () => {
const schema = {
name: fields.slug({ name: { label: 'Name' } }),
imageFeatured: fields.image({
label: 'Featured',
directory: 'public/images/wines',
publicPath: '/images/wines/',
}),
imageDetail: fields.image({
label: 'Detail',
directory: 'public/images/wines',
publicPath: '/images/wines/',
}),
technicalSheets: fields.array(
fields.object({
label: fields.text({ label: 'Label' }),
fileRef: fields.text({ label: 'File ref' }),
}),
{ label: 'Sheets' }
),
body: fields.emptyContent({ extension: 'md' }),
};

const config: Config = {
storage: { kind: 'local' },
collections: {
wines: collection({
label: 'Wines',
slugField: 'name',
path: 'src/content/wines/*',
format: { contentField: 'body' },
schema,
}),
},
};

const files = new Map<string, Uint8Array>([
[
'src/content/wines/alma.md',
encoder.encode(
`---
name: Alma
imageFeatured: /images/wines/alma/missing-renamed.avif
imageDetail: /images/wines/chateau-alma.avif
technicalSheets: []
---
`
),
],
['public/images/wines/alma/unrelated.avif', new Uint8Array([9, 9])],
]);

const { initialFiles, initialState } = parseEntry(
{
dirpath: 'src/content/wines/alma',
format: getCollectionFormat(config, 'wines'),
schema,
slug: { field: 'name', slug: 'alma' },
},
files
);

expect(initialState.imageFeatured).toBeNull();
expect(initialState.imageDetail).toBeNull();
expect(initialFiles).toEqual(['src/content/wines/alma.md']);
});

test('includes existing referenced asset files in initialFiles while ignoring unreferenced siblings', () => {
const schema = {
name: fields.slug({ name: { label: 'Name' } }),
imageFeatured: fields.image({
label: 'Featured',
directory: 'public/images/wines',
publicPath: '/images/wines/',
}),
body: fields.emptyContent({ extension: 'md' }),
};

const config: Config = {
storage: { kind: 'local' },
collections: {
wines: collection({
label: 'Wines',
slugField: 'name',
path: 'src/content/wines/*',
format: { contentField: 'body' },
schema,
}),
},
};

const files = new Map<string, Uint8Array>([
[
'src/content/wines/alma.md',
encoder.encode(
`---
name: Alma
imageFeatured: /images/wines/alma/imageFeatured.avif
---
`
),
],
[
'public/images/wines/alma/imageFeatured.avif',
new Uint8Array([1, 2, 3]),
],
['public/images/wines/alma/unrelated.avif', new Uint8Array([4, 5, 6])],
]);

const { initialFiles, initialState } = parseEntry(
{
dirpath: 'src/content/wines/alma',
format: getCollectionFormat(config, 'wines'),
schema,
slug: { field: 'name', slug: 'alma' },
},
files
);

expect(initialState.imageFeatured).not.toBeNull();
expect(initialFiles).toEqual([
'src/content/wines/alma.md',
'public/images/wines/alma/imageFeatured.avif',
]);
});

test('does not record missing content or TrackedMap asset lookups in initialFiles', () => {
const probeField: AssetsFormField<string, string, string> = {
kind: 'form',
formKind: 'assets',
directories: ['public/media'],
defaultValue: () => '',
Input: () => null,
validate: value => value,
parse(value, args) {
args.other.get('existing-inline.txt');
args.other.get('missing-inline.txt');
const media = args.external.get('public/media');
media?.get('existing-ext.png');
media?.get('missing-ext.png');
return typeof value === 'string' ? value : '';
},
serialize: value => ({
value,
other: new Map(),
external: new Map(),
}),
reader: {
parse: value => (typeof value === 'string' ? value : ''),
},
};

const schema = {
title: fields.slug({ name: { label: 'Title' } }),
probe: probeField,
body: fields.markdoc({ label: 'Body' }),
};

const config: Config = {
storage: { kind: 'local' },
collections: {
posts: collection({
label: 'Posts',
slugField: 'title',
path: 'posts/*/',
schema,
}),
},
};

const files = new Map<string, Uint8Array>([
['posts/hello/index.yaml', encoder.encode('title: Hello\nprobe: ok\n')],
['posts/hello/probe/existing-inline.txt', encoder.encode('hi')],
['public/media/hello/existing-ext.png', new Uint8Array([7])],
['public/media/hello/unreferenced.png', new Uint8Array([8])],
]);

const { initialFiles } = parseEntry(
{
dirpath: 'posts/hello',
format: getCollectionFormat(config, 'posts'),
schema,
slug: { field: 'title', slug: 'hello' },
},
files
);

expect(initialFiles).toEqual([
'posts/hello/index.yaml',
'posts/hello/probe/existing-inline.txt',
'public/media/hello/existing-ext.png',
]);
});
});
8 changes: 6 additions & 2 deletions packages/keystatic/src/app/useItemData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class TrackedMap<K, V> extends Map<K, V> {
this.#onGet = onGet;
}
get(key: K) {
this.#onGet(key);
if (super.has(key)) {
this.#onGet(key);
}
return super.get(key);
}
}
Expand Down Expand Up @@ -76,7 +78,9 @@ export function parseEntry(
let initialState;

const getFile = (filepath: string) => {
usedFiles.add(filepath);
if (filesWithFakeFile.has(filepath)) {
usedFiles.add(filepath);
}
return filesWithFakeFile.get(filepath);
};
const getFilesForAssetsOrContentField = (
Expand Down
Loading