Repository navigation
fix(content): support non-cloneable values in content collection data - #17596
Closed
Constantine1916 wants to merge 2 commits into
Closed
Constantine1916 wants to merge 2 commits into
Constantine1916 wants to merge 2 commits into
Conversation
updateImageReferencesInData() called structuredClone() on every entry's data, which throws DataCloneError for values that structured clone cannot handle, such as Temporal.PlainDate or class instances produced by Zod transform() in a collection schema. Image reference replacement only ever rewrites strings nested in plain objects and arrays, so only those containers need to be copied to avoid mutating the data store. Replace structuredClone() with a selective clone that copies plain objects and arrays and passes every other value (Map, Set, class instances, etc.) through by reference. Map/Set handling from withastro#16701 is preserved and covered by existing tests. Fixes withastro#17589 Co-Authored-By: Claude Fable 5 <[email protected]>
🦋 Changeset detectedLatest commit: f6f0c1a The changes in this PR will be included in the next version bump. This PR includes changesets to release 402 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Preallocate the array copy and use an indexed loop, and check the WeakMap cache with !== undefined. Suggested by @EldarAgalarov in withastro#17589. Co-Authored-By: Claude Fable 5 <[email protected]>
Contributor
|
I'll review further but I don't think we want to take on trying to serialize every data type. |
Author
|
Closing in favor of #17631, which has been merged — resolving image references via paths recorded at write time is a cleaner fix than patching the clone, since it avoids cloning user data altogether. Thanks @matthewp for the quick turnaround, and @EldarAgalarov for the review! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
getCollection()/getEntry()threwDataCloneErrorwhen a collection schema produced values thatstructuredClonecannot handle — e.g. a Zodtransform()returningTemporal.PlainDateor any other class instance.structuredClone(data)inupdateImageReferencesInData()(introduced in fix(astro): fix brokenMapandSetin content collections #16701 to preserveMap/Set) is replaced with a selective clone: only plain objects and arrays — the containers whose nested strings may be rewritten into image metadata — are copied. Everything else (Map,Set, class instances,Temporalobjects, …) passes through by reference.ctx.update()on strings prefixed with__ASTRO_IMAGE_; non-plain values are never mutation targets, so sharing them between the store data and the returned copy cannot leak mutations.WeakMap, matchingstructuredClone's behavior there.astro).Testing
packages/astro/test/units/content-collections/image-references.test.tsusing a class instance as a stand-in forTemporal.PlainDate(the test first assertsstructuredClonerejects it withDataCloneError, then thatupdateImageReferencesInDatapreserves it by reference, including when nested).image-references.test.tspass, including theMap/Setpreservation tests from fix(astro): fix brokenMapandSetin content collections #16701; the other content-collections unit suites (get-entry-info,get-entry-type,mutable-data-store) also pass.Docs
No docs changes needed — this removes an undocumented limitation rather than changing documented behavior.
🤖 Generated with Claude Code