Repository navigation
perf: deduplicate blocks used in multiple places using new config.blocks property - #10905
Merged
Merged
Conversation
AlessioGr
added a commit
that referenced
this pull request
Feb 7, 2025
This PR exposes the `ClientConfig` as an argument to the lexical `ClientFeature`. This is a requirement for #10905, as we need to get the ClientBlocks from the `clientConfig.blocksMap` if they are strings. ## Example ```tsx export const BlocksFeatureClient = createClientFeature( ({ config, featureClientSchemaMap, props, schemaPath }) => { // <= config is the new argument // Return ClientFeature }) ```
AlessioGr
requested review from
GermanJablo,
JarrodMFlesch and
jacobsfletch
as code owners
February 12, 2025 23:53
GermanJablo
reviewed
Feb 13, 2025
jmikrut
reviewed
Feb 13, 2025
jmikrut
reviewed
Feb 13, 2025
jmikrut
reviewed
Feb 13, 2025
jmikrut
reviewed
Feb 13, 2025
jmikrut
reviewed
Feb 13, 2025
AlessioGr
enabled auto-merge (squash)
February 13, 2025 22:43
DanRibbens
reviewed
Feb 14, 2025
DanRibbens
left a comment
Contributor
There was a problem hiding this comment.
We export the payload/src/utilities/traverseFields for other plugins to make use of and this PR includes config as an arg that wasn't before.
r1tsuu
added a commit
that referenced
this pull request
Feb 16, 2025
…nside `blockReferences` (#11195) This PR adds a new unit test assertion to existing https://github.com/payloadcms/payload/blob/main/packages/db-mongodb/src/utilities/sanitizeRelationshipIDs.spec.ts that ensures relationships are sanitized to `ObjectID`s correctly when saved to the database for relationships inside the new `blockReferences` #10905
jacobsfletch
pushed a commit
that referenced
this pull request
Feb 17, 2025
…cks property (#10905) If you have multiple blocks that are used in multiple places, this can quickly blow up the size of your Payload Config. This will incur a performance hit, as more data is 1. sent to the client (=> bloated `ClientConfig` and large initial html) and 2. processed on the server (permissions are calculated every single time you navigate to a page - this iterates through all blocks you have defined, even if they're duplicative) This can be optimized by defining your block **once** in your Payload Config, and just referencing the block slug whenever it's used, instead of passing the entire block config. To do this, the block can be defined in the `blocks` array of the Payload Config. The slug can then be passed to the `blockReferences` array in the Blocks Field - the `blocks` array has to be empty for compatibility reasons. ```ts import { buildConfig } from 'payload' import { lexicalEditor, BlocksFeature } from '@payloadcms/richtext-lexical' // Payload Config const config = buildConfig({ // Define the block once blocks: [ { slug: 'TextBlock', fields: [ { name: 'text', type: 'text', }, ], }, ], collections: [ { slug: 'collection1', fields: [ { name: 'content', type: 'blocks', // Reference the block by slug blockReferences: ['TextBlock'], blocks: [], // Required to be empty, for compatibility reasons }, ], }, { slug: 'collection2', fields: [ { name: 'editor', type: 'richText', editor: lexicalEditor({ BlocksFeature({ // Same reference can be reused anywhere, even in the lexical editor, without incurred performance hit blocks: ['TextBlock'], }) }) }, ], }, ], }) ``` ## v4.0 Plans In 4.0, we will remove the `blockReferences` property, and allow string block references to be passed directly to the blocks `property`. Essentially, we'd remove the `blocks` property and rename `blockReferences` to `blocks`. The reason we opted to a new property in this PR is to avoid breaking changes. Allowing strings to be passed to the `blocks` property will prevent plugins that iterate through fields / blocks from compiling. ## PR Changes - Testing: This PR introduces a plugin that automatically converts blocks to block references. This is done in the fields__blocks test suite, to run our existing test suite using block references. - Block References support: Most changes are similar. Everywhere we iterate through blocks, we have to now do the following: 1. Check if `field.blockReferences` is provided. If so, only iterate through that. 2. Check if the block is an object (= actual block), or string 3. If it's a string, pull the actual block from the Payload Config or from `payload.blocks`. The exception is config sanitization and block type generations. This PR optimizes them so that each block is only handled once, instead of every time the block is referenced. ## Benchmarks 60 Block fields, each block field having the same 600 Blocks. ### Before: **Initial HTML:** 195 kB **Generated types:** takes 11 minutes, 461,209 lines https://github.com/user-attachments/assets/11d49a4e-5414-4579-8050-e6346e552f56 ### After: **Initial HTML:** 73.6 kB **Generated types:** takes 2 seconds, 35,810 lines https://github.com/user-attachments/assets/3eab1a99-6c29-489d-add5-698df67780a3 ### After Permissions Optimization (follow-up PR) Initial HTML: 73.6 kB https://github.com/user-attachments/assets/a909202e-45a8-4bf6-9a38-8c85813f1312 ## Future Plans 1. This PR does not yet deduplicate block references during permissions calculation. We'll optimize that in a separate PR, as this one is already large enough 2. The same optimization can be done to deduplicate fields. One common use-case would be link field groups that may be referenced in multiple entities, outside of blocks. We might explore adding a new `fieldReferences` property, that allows you to reference those same `config.blocks`.
Contributor
|
🚀 This is included in version v3.24.0 |
kendelljoseph
pushed a commit
that referenced
this pull request
Feb 21, 2025
This PR exposes the `ClientConfig` as an argument to the lexical `ClientFeature`. This is a requirement for #10905, as we need to get the ClientBlocks from the `clientConfig.blocksMap` if they are strings. ## Example ```tsx export const BlocksFeatureClient = createClientFeature( ({ config, featureClientSchemaMap, props, schemaPath }) => { // <= config is the new argument // Return ClientFeature }) ```
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.
If you have multiple blocks that are used in multiple places, this can quickly blow up the size of your Payload Config. This will incur a performance hit, as more data is
ClientConfigand large initial html) andThis can be optimized by defining your block once in your Payload Config, and just referencing the block slug whenever it's used, instead of passing the entire block config. To do this, the block can be defined in the
blocksarray of the Payload Config. The slug can then be passed to theblockReferencesarray in the Blocks Field - theblocksarray has to be empty for compatibility reasons.v4.0 Plans
In 4.0, we will remove the
blockReferencesproperty, and allow string block references to be passed directly to the blocksproperty. Essentially, we'd remove theblocksproperty and renameblockReferencestoblocks.The reason we opted to a new property in this PR is to avoid breaking changes. Allowing strings to be passed to the
blocksproperty will prevent plugins that iterate through fields / blocks from compiling.PR Changes
Testing: This PR introduces a plugin that automatically converts blocks to block references. This is done in the fields__blocks test suite, to run our existing test suite using block references.
Block References support: Most changes are similar. Everywhere we iterate through blocks, we have to now do the following:
field.blockReferencesis provided. If so, only iterate through that.payload.blocks.The exception is config sanitization and block type generations. This PR optimizes them so that each block is only handled once, instead of every time the block is referenced.
Benchmarks
60 Block fields, each block field having the same 600 Blocks.
Before:
Initial HTML: 195 kB
Generated types: takes 11 minutes, 461,209 lines
CleanShot.2025-02-13.at.10.23.18.mp4
After:
Initial HTML: 73.6 kB
Generated types: takes 2 seconds, 35,810 lines
CleanShot.2025-02-13.at.10.20.50.mp4
After Permissions Optimization (follow-up PR)
Initial HTML: 73.6 kB
CleanShot.2025-02-13.at.10.28.10.mp4
Future Plans
fieldReferencesproperty, that allows you to reference those sameconfig.blocks.