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`.
447 lines
8.9 KiB
TypeScript
447 lines
8.9 KiB
TypeScript
import type { ArrayField, Block, TextFieldSingleValidation } from 'payload'
|
|
|
|
import { BlocksFeature, FixedToolbarFeature, lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
|
|
import { textFieldsSlug } from '../Text/shared.js'
|
|
|
|
async function asyncFunction(param: string) {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve(param?.toUpperCase())
|
|
}, 1000)
|
|
})
|
|
}
|
|
|
|
export const FilterOptionsBlock: Block = {
|
|
slug: 'filterOptionsBlock',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'group',
|
|
type: 'group',
|
|
fields: [
|
|
{
|
|
name: 'groupText',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'dependsOnDocData',
|
|
type: 'relationship',
|
|
relationTo: 'text-fields',
|
|
filterOptions: ({ data }) => {
|
|
if (!data.title) {
|
|
return true
|
|
}
|
|
return {
|
|
text: {
|
|
equals: data.title,
|
|
},
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: 'dependsOnSiblingData',
|
|
type: 'relationship',
|
|
relationTo: 'text-fields',
|
|
filterOptions: ({ siblingData }) => {
|
|
// @ts-expect-error
|
|
if (!siblingData?.groupText) {
|
|
return true
|
|
}
|
|
return {
|
|
text: {
|
|
equals: (siblingData as any)?.groupText,
|
|
},
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: 'dependsOnBlockData',
|
|
type: 'relationship',
|
|
relationTo: 'text-fields',
|
|
filterOptions: ({ blockData }) => {
|
|
if (!blockData?.text) {
|
|
return true
|
|
}
|
|
return {
|
|
text: {
|
|
equals: blockData?.text,
|
|
},
|
|
}
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
export const ValidationBlock: Block = {
|
|
slug: 'validationBlock',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'group',
|
|
type: 'group',
|
|
fields: [
|
|
{
|
|
name: 'groupText',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'textDependsOnDocData',
|
|
type: 'text',
|
|
validate: ((value, { data }) => {
|
|
if ((data as any)?.title === 'invalid') {
|
|
return 'doc title cannot be invalid'
|
|
}
|
|
return true
|
|
}) as TextFieldSingleValidation,
|
|
},
|
|
{
|
|
name: 'textDependsOnSiblingData',
|
|
type: 'text',
|
|
validate: ((value, { siblingData }) => {
|
|
if ((siblingData as any)?.groupText === 'invalid') {
|
|
return 'textDependsOnSiblingData sibling field cannot be invalid'
|
|
}
|
|
}) as TextFieldSingleValidation,
|
|
},
|
|
{
|
|
name: 'textDependsOnBlockData',
|
|
type: 'text',
|
|
validate: ((value, { blockData }) => {
|
|
if ((blockData as any)?.text === 'invalid') {
|
|
return 'textDependsOnBlockData sibling field cannot be invalid'
|
|
}
|
|
}) as TextFieldSingleValidation,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
export const AsyncHooksBlock: Block = {
|
|
slug: 'asyncHooksBlock',
|
|
fields: [
|
|
{
|
|
name: 'test1',
|
|
label: 'Text',
|
|
type: 'text',
|
|
hooks: {
|
|
afterRead: [
|
|
({ value }) => {
|
|
return value?.toUpperCase()
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{
|
|
name: 'test2',
|
|
label: 'Text',
|
|
type: 'text',
|
|
hooks: {
|
|
afterRead: [
|
|
async ({ value }) => {
|
|
const valuenew = await asyncFunction(value)
|
|
return valuenew
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
}
|
|
|
|
export const BlockColumns = ({ name }: { name: string }): ArrayField => ({
|
|
type: 'array',
|
|
name,
|
|
interfaceName: 'BlockColumns',
|
|
admin: {
|
|
initCollapsed: true,
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'subArray',
|
|
type: 'array',
|
|
fields: [
|
|
{
|
|
name: 'requiredText',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|
|
export const ConditionalLayoutBlock: Block = {
|
|
fields: [
|
|
{
|
|
label: 'Layout',
|
|
name: 'layout',
|
|
type: 'select',
|
|
options: ['1', '2', '3'],
|
|
defaultValue: '1',
|
|
required: true,
|
|
},
|
|
{
|
|
...BlockColumns({ name: 'columns' }),
|
|
admin: {
|
|
condition: (data, siblingData) => {
|
|
return ['1'].includes(siblingData.layout)
|
|
},
|
|
},
|
|
minRows: 1,
|
|
maxRows: 1,
|
|
},
|
|
{
|
|
...BlockColumns({ name: 'columns2' }),
|
|
admin: {
|
|
condition: (data, siblingData) => {
|
|
return ['2'].includes(siblingData.layout)
|
|
},
|
|
},
|
|
minRows: 2,
|
|
maxRows: 2,
|
|
},
|
|
{
|
|
...BlockColumns({ name: 'columns3' }),
|
|
admin: {
|
|
condition: (data, siblingData) => {
|
|
return ['3'].includes(siblingData.layout)
|
|
},
|
|
},
|
|
minRows: 3,
|
|
maxRows: 3,
|
|
},
|
|
],
|
|
slug: 'conditionalLayout',
|
|
}
|
|
|
|
export const TextBlock: Block = {
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
],
|
|
slug: 'textRequired',
|
|
}
|
|
|
|
export const RadioButtonsBlock: Block = {
|
|
interfaceName: 'LexicalBlocksRadioButtonsBlock',
|
|
fields: [
|
|
{
|
|
name: 'radioButtons',
|
|
type: 'radio',
|
|
options: [
|
|
{
|
|
label: 'Option 1',
|
|
value: 'option1',
|
|
},
|
|
{
|
|
label: 'Option 2',
|
|
value: 'option2',
|
|
},
|
|
{
|
|
label: 'Option 3',
|
|
value: 'option3',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
slug: 'radioButtons',
|
|
}
|
|
|
|
export const RichTextBlock: Block = {
|
|
fields: [
|
|
{
|
|
name: 'richTextField',
|
|
type: 'richText',
|
|
editor: lexicalEditor({
|
|
features: ({ defaultFeatures }) => [
|
|
...defaultFeatures,
|
|
FixedToolbarFeature(),
|
|
BlocksFeature({
|
|
blocks: [
|
|
{
|
|
fields: [
|
|
{
|
|
name: 'subRichTextField',
|
|
type: 'richText',
|
|
editor: lexicalEditor({}),
|
|
},
|
|
{
|
|
name: 'subUploadField',
|
|
type: 'upload',
|
|
relationTo: 'uploads',
|
|
},
|
|
],
|
|
slug: 'lexicalAndUploadBlock',
|
|
},
|
|
],
|
|
}),
|
|
],
|
|
}),
|
|
},
|
|
],
|
|
slug: 'richTextBlock',
|
|
}
|
|
|
|
export const UploadAndRichTextBlock: Block = {
|
|
fields: [
|
|
{
|
|
name: 'upload',
|
|
type: 'upload',
|
|
relationTo: 'uploads',
|
|
required: true,
|
|
},
|
|
{
|
|
name: 'richText',
|
|
type: 'richText',
|
|
editor: lexicalEditor(),
|
|
},
|
|
],
|
|
slug: 'uploadAndRichText',
|
|
}
|
|
|
|
export const RelationshipHasManyBlock: Block = {
|
|
fields: [
|
|
{
|
|
name: 'rel',
|
|
type: 'relationship',
|
|
hasMany: true,
|
|
relationTo: [textFieldsSlug, 'uploads'],
|
|
required: true,
|
|
},
|
|
],
|
|
slug: 'relationshipHasManyBlock',
|
|
}
|
|
export const RelationshipBlock: Block = {
|
|
fields: [
|
|
{
|
|
name: 'rel',
|
|
type: 'relationship',
|
|
relationTo: 'uploads',
|
|
required: true,
|
|
},
|
|
],
|
|
slug: 'relationshipBlock',
|
|
}
|
|
|
|
export const SelectFieldBlock: Block = {
|
|
fields: [
|
|
{
|
|
name: 'select',
|
|
type: 'select',
|
|
options: [
|
|
{
|
|
label: 'Option 1',
|
|
value: 'option1',
|
|
},
|
|
{
|
|
label: 'Option 2',
|
|
value: 'option2',
|
|
},
|
|
{
|
|
label: 'Option 3',
|
|
value: 'option3',
|
|
},
|
|
{
|
|
label: 'Option 4',
|
|
value: 'option4',
|
|
},
|
|
{
|
|
label: 'Option 5',
|
|
value: 'option5',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
slug: 'select',
|
|
}
|
|
|
|
export const SubBlockBlock: Block = {
|
|
slug: 'subBlockLexical',
|
|
fields: [
|
|
{
|
|
name: 'subBlocksLexical',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'contentBlock',
|
|
fields: [
|
|
{
|
|
name: 'richText',
|
|
type: 'richText',
|
|
required: true,
|
|
editor: lexicalEditor(),
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'textArea',
|
|
fields: [
|
|
{
|
|
name: 'content',
|
|
type: 'textarea',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
SelectFieldBlock,
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
export const TabBlock: Block = {
|
|
slug: 'tabBlock',
|
|
fields: [
|
|
{
|
|
type: 'tabs',
|
|
tabs: [
|
|
{
|
|
label: 'Tab1',
|
|
name: 'tab1',
|
|
fields: [
|
|
{
|
|
name: 'text1',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
label: 'Tab2',
|
|
name: 'tab2',
|
|
fields: [
|
|
{
|
|
name: 'text2',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
export const CodeBlock: Block = {
|
|
fields: [
|
|
{
|
|
name: 'code',
|
|
type: 'code',
|
|
},
|
|
],
|
|
slug: 'code',
|
|
}
|