fix(collection): guard against collections with empty or missing source#438
fix(collection): guard against collections with empty or missing source#438humanbird wants to merge 1 commit intonuxt-content:mainnuxt-content/nuxt-studio:mainfrom humanbird:fix/collection-empty-source-guardshumanbird/nuxt-studio:fix/collection-empty-source-guardsCopy head branch name to clipboard
Conversation
`@nuxt/content` v3 exposes an internal `info` collection whose `source` is serialized as an empty array (`source: []`) in the preview manifest. Studio iterates every collection via `parseSourceBase`, `generateIdFromFsPath`, and `generateFsPathFromId`, which all assumed at least one `ResolvedCollectionSource` entry and crashed with `TypeError: Cannot read properties of undefined (reading 'include' | 'prefix')` as soon as the editor tried to list content. The upstream payload fix is in nuxt/content#3771, but the guards here are still the correct shape defensively — Studio should tolerate any future source-less collection without corrupting the editor. - `parseSourceBase` now short-circuits to empty `fixed`/`dynamic` when the source (or its `include`) is missing. - `getCollectionSourceById` skips sources without `include` during matching instead of throwing. - `generateIdFromFsPath` falls back to `join(name, path)` when the collection has no source. - `generateFsPathFromId` returns the raw path when no source is given. Resolves nuxt-content#425
|
@humanbird is attempting to deploy a commit to the Nuxt Team on Vercel. A member of the Team first needs to authorize it. |
|
Hey @humanbird, thanks for the PR. However I'm not able to reproduce your case. Could you share a reproduction? I've seen and approved nuxt/content#3771 that should be enough to fix what you're experiencing IMO. If not, a better solution can be to filter at source (in |
|
Hey @larbish, you're right. Ran the unpatched nuxt-studio@1.6.0 utils against a real The only residual crash is a theoretical HMR path in Closing as redundant. If it makes sense later to dedupe the filter into |
🔗 Linked issue
Resolves #425 (paired with upstream fix nuxt/content#3771)
❓ Type of change
📚 Description
@nuxt/contentv3 exposes an internal `info` collection whose `source` is serialized as an empty array (`source: []`) in the preview manifest (see nuxt/content#3767). Studio iterates every collection via `parseSourceBase`, `generateIdFromFsPath`, and `generateFsPathFromId`, which all assumed at least one `ResolvedCollectionSource` entry — so the editor crashed with:```
TypeError: Cannot read properties of undefined (reading 'include')
at parseSourceBase (source.js)
…
TypeError: Cannot read properties of undefined (reading 'prefix')
at generateIdFromFsPath (collection.js)
```
The upstream payload fix (nuxt/content#3771) removes source-less collections from the preview manifest entirely, but these null guards are still the right shape defensively — Studio should tolerate any future source-less collection without corrupting the editor.
What changed
Tests
New cases added in `src/module/test/utils/source.test.ts` and `src/module/test/utils/collection.test.ts` covering each guarded path. `pnpm vitest run src/module/test/utils/source.test.ts src/module/test/utils/collection.test.ts` → 31/31 pass. Full suite: 184 tests pass; the 5 unrelated file-level failures (`document.test.ts`, `host.test.ts`, `draft-base.test.ts`, `compare.test.ts`) are caused by `nuxt-studio/app/utils` resolution in the playground setup on `main` and are reproducible without this PR.
📝 Checklist