diff --git a/.changeset/fix-missing-initial-files-deletion.md b/.changeset/fix-missing-initial-files-deletion.md new file mode 100644 index 000000000..efdde3bc1 --- /dev/null +++ b/.changeset/fix-missing-initial-files-deletion.md @@ -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. diff --git a/packages/keystatic/src/app/useItemData.test.ts b/packages/keystatic/src/app/useItemData.test.ts new file mode 100644 index 000000000..7610d440a --- /dev/null +++ b/packages/keystatic/src/app/useItemData.test.ts @@ -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([ + [ + '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([ + [ + '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 = { + 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([ + ['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', + ]); + }); +}); diff --git a/packages/keystatic/src/app/useItemData.ts b/packages/keystatic/src/app/useItemData.ts index 48ba8d896..e214f5141 100644 --- a/packages/keystatic/src/app/useItemData.ts +++ b/packages/keystatic/src/app/useItemData.ts @@ -39,7 +39,9 @@ class TrackedMap extends Map { this.#onGet = onGet; } get(key: K) { - this.#onGet(key); + if (super.has(key)) { + this.#onGet(key); + } return super.get(key); } } @@ -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 = (