feat!: on demand rsc (#8364)
Currently, Payload renders all custom components on initial compile of the admin panel. This is problematic for two key reasons: 1. Custom components do not receive contextual data, i.e. fields do not receive their field data, edit views do not receive their document data, etc. 2. Components are unnecessarily rendered before they are used This was initially required to support React Server Components within the Payload Admin Panel for two key reasons: 1. Fields can be dynamically rendered within arrays, blocks, etc. 2. Documents can be recursively rendered within a "drawer" UI, i.e. relationship fields 3. Payload supports server/client component composition In order to achieve this, components need to be rendered on the server and passed as "slots" to the client. Currently, the pattern for this is to render custom server components in the "client config". Then when a view or field is needed to be rendered, we first check the client config for a "pre-rendered" component, otherwise render our client-side fallback component. But for the reasons listed above, this pattern doesn't exactly make custom server components very useful within the Payload Admin Panel, which is where this PR comes in. Now, instead of pre-rendering all components on initial compile, we're able to render custom components _on demand_, only as they are needed. To achieve this, we've established [this pattern](https://github.com/payloadcms/payload/pull/8481) of React Server Functions in the Payload Admin Panel. With Server Functions, we can iterate the Payload Config and return JSX through React's `text/x-component` content-type. This means we're able to pass contextual props to custom components, such as data for fields and views. ## Breaking Changes 1. Add the following to your root layout file, typically located at `(app)/(payload)/layout.tsx`: ```diff /* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */ /* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */ + import type { ServerFunctionClient } from 'payload' import config from '@payload-config' import { RootLayout } from '@payloadcms/next/layouts' import { handleServerFunctions } from '@payloadcms/next/utilities' import React from 'react' import { importMap } from './admin/importMap.js' import './custom.scss' type Args = { children: React.ReactNode } + const serverFunctions: ServerFunctionClient = async function (args) { + 'use server' + return handleServerFunctions({ + ...args, + config, + importMap, + }) + } const Layout = ({ children }: Args) => ( <RootLayout config={config} importMap={importMap} + serverFunctions={serverFunctions} > {children} </RootLayout> ) export default Layout ``` 2. If you were previously posting to the `/api/form-state` endpoint, it no longer exists. Instead, you'll need to invoke the `form-state` Server Function, which can be done through the _new_ `getFormState` utility: ```diff - import { getFormState } from '@payloadcms/ui' - const { state } = await getFormState({ - apiRoute: '', - body: { - // ... - }, - serverURL: '' - }) + const { getFormState } = useServerFunctions() + + const { state } = await getFormState({ + // ... + }) ``` ## Breaking Changes ```diff - useFieldProps() - useCellProps() ``` More details coming soon. --------- Co-authored-by: Alessio Gravili <alessio@gravili.de> Co-authored-by: Jarrod Flesch <jarrodmflesch@gmail.com> Co-authored-by: James <james@trbl.design>
This commit is contained in:
@@ -427,21 +427,6 @@ export default buildConfigWithDefaults({
|
||||
onInit: async (payload) => {
|
||||
const collection = localizedPostsSlug
|
||||
|
||||
console.log('SEED BEGIN')
|
||||
|
||||
if (payload.db.name === 'mongoose') {
|
||||
await new Promise((resolve, reject) => {
|
||||
payload.db?.collections[localizedPostsSlug]?.ensureIndexes(function (err) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
}
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
console.log('INDEXES CREATED')
|
||||
|
||||
await payload.create({
|
||||
collection,
|
||||
data: {
|
||||
|
||||
@@ -31,16 +31,45 @@ export interface Config {
|
||||
'payload-preferences': PayloadPreference;
|
||||
'payload-migrations': PayloadMigration;
|
||||
};
|
||||
collectionsJoins: {};
|
||||
collectionsSelect: {
|
||||
'blocks-fields': BlocksFieldsSelect<false> | BlocksFieldsSelect<true>;
|
||||
'nested-arrays': NestedArraysSelect<false> | NestedArraysSelect<true>;
|
||||
'nested-field-tables': NestedFieldTablesSelect<false> | NestedFieldTablesSelect<true>;
|
||||
users: UsersSelect<false> | UsersSelect<true>;
|
||||
'localized-posts': LocalizedPostsSelect<false> | LocalizedPostsSelect<true>;
|
||||
'array-fields': ArrayFieldsSelect<false> | ArrayFieldsSelect<true>;
|
||||
'localized-required': LocalizedRequiredSelect<false> | LocalizedRequiredSelect<true>;
|
||||
'with-localized-relationship': WithLocalizedRelationshipSelect<false> | WithLocalizedRelationshipSelect<true>;
|
||||
'relationship-localized': RelationshipLocalizedSelect<false> | RelationshipLocalizedSelect<true>;
|
||||
dummy: DummySelect<false> | DummySelect<true>;
|
||||
nested: NestedSelect<false> | NestedSelect<true>;
|
||||
groups: GroupsSelect<false> | GroupsSelect<true>;
|
||||
tabs: TabsSelect<false> | TabsSelect<true>;
|
||||
'localized-sort': LocalizedSortSelect<false> | LocalizedSortSelect<true>;
|
||||
'blocks-same-name': BlocksSameNameSelect<false> | BlocksSameNameSelect<true>;
|
||||
'localized-within-localized': LocalizedWithinLocalizedSelect<false> | LocalizedWithinLocalizedSelect<true>;
|
||||
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
|
||||
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
|
||||
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
|
||||
};
|
||||
db: {
|
||||
defaultIDType: string;
|
||||
};
|
||||
globals: {
|
||||
'global-array': GlobalArray;
|
||||
};
|
||||
globalsSelect: {
|
||||
'global-array': GlobalArraySelect<false> | GlobalArraySelect<true>;
|
||||
};
|
||||
locale: 'en' | 'es' | 'pt' | 'ar' | 'hu';
|
||||
user: User & {
|
||||
collection: 'users';
|
||||
};
|
||||
jobs?: {
|
||||
tasks: unknown;
|
||||
workflows?: unknown;
|
||||
};
|
||||
}
|
||||
export interface UserAuthOperations {
|
||||
forgotPassword: {
|
||||
@@ -399,12 +428,12 @@ export interface Nested {
|
||||
*/
|
||||
export interface Group {
|
||||
id: string;
|
||||
groupLocalized?: {
|
||||
title?: string | null;
|
||||
};
|
||||
groupLocalizedRow?: {
|
||||
text?: string | null;
|
||||
};
|
||||
groupLocalized?: {
|
||||
title?: string | null;
|
||||
};
|
||||
group?: {
|
||||
title?: string | null;
|
||||
};
|
||||
@@ -636,6 +665,495 @@ export interface PayloadMigration {
|
||||
updatedAt: string;
|
||||
createdAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "blocks-fields_select".
|
||||
*/
|
||||
export interface BlocksFieldsSelect<T extends boolean = true> {
|
||||
content?:
|
||||
| T
|
||||
| {
|
||||
blockInsideBlock?:
|
||||
| T
|
||||
| {
|
||||
content?:
|
||||
| T
|
||||
| {
|
||||
textBlock?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
link?:
|
||||
| T
|
||||
| {
|
||||
label?: T;
|
||||
};
|
||||
id?: T;
|
||||
};
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "nested-arrays_select".
|
||||
*/
|
||||
export interface NestedArraysSelect<T extends boolean = true> {
|
||||
arrayWithBlocks?:
|
||||
| T
|
||||
| {
|
||||
blocksWithinArray?:
|
||||
| T
|
||||
| {
|
||||
someBlock?:
|
||||
| T
|
||||
| {
|
||||
relationWithinBlock?: T;
|
||||
myGroup?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
};
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
id?: T;
|
||||
};
|
||||
arrayWithLocalizedRelation?:
|
||||
| T
|
||||
| {
|
||||
localizedRelation?: T;
|
||||
id?: T;
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
_status?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "nested-field-tables_select".
|
||||
*/
|
||||
export interface NestedFieldTablesSelect<T extends boolean = true> {
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
relation?: T;
|
||||
hasManyRelation?: T;
|
||||
hasManyPolyRelation?: T;
|
||||
select?: T;
|
||||
number?: T;
|
||||
text?: T;
|
||||
id?: T;
|
||||
};
|
||||
blocks?:
|
||||
| T
|
||||
| {
|
||||
block?:
|
||||
| T
|
||||
| {
|
||||
nestedBlocks?:
|
||||
| T
|
||||
| {
|
||||
content?:
|
||||
| T
|
||||
| {
|
||||
relation?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
relation?: T;
|
||||
id?: T;
|
||||
};
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "users_select".
|
||||
*/
|
||||
export interface UsersSelect<T extends boolean = true> {
|
||||
relation?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
email?: T;
|
||||
resetPasswordToken?: T;
|
||||
resetPasswordExpiration?: T;
|
||||
salt?: T;
|
||||
hash?: T;
|
||||
loginAttempts?: T;
|
||||
lockUntil?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "localized-posts_select".
|
||||
*/
|
||||
export interface LocalizedPostsSelect<T extends boolean = true> {
|
||||
title?: T;
|
||||
description?: T;
|
||||
localizedDescription?: T;
|
||||
localizedCheckbox?: T;
|
||||
children?: T;
|
||||
group?:
|
||||
| T
|
||||
| {
|
||||
children?: T;
|
||||
};
|
||||
unique?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "array-fields_select".
|
||||
*/
|
||||
export interface ArrayFieldsSelect<T extends boolean = true> {
|
||||
items?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
id?: T;
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "localized-required_select".
|
||||
*/
|
||||
export interface LocalizedRequiredSelect<T extends boolean = true> {
|
||||
title?: T;
|
||||
nav?:
|
||||
| T
|
||||
| {
|
||||
layout?:
|
||||
| T
|
||||
| {
|
||||
text?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
nestedArray?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
l2?:
|
||||
| T
|
||||
| {
|
||||
l3?:
|
||||
| T
|
||||
| {
|
||||
l4?:
|
||||
| T
|
||||
| {
|
||||
superNestedText?: T;
|
||||
id?: T;
|
||||
};
|
||||
id?: T;
|
||||
};
|
||||
id?: T;
|
||||
};
|
||||
id?: T;
|
||||
};
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
number?:
|
||||
| T
|
||||
| {
|
||||
number?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
};
|
||||
myTab?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
group?:
|
||||
| T
|
||||
| {
|
||||
nestedArray2?:
|
||||
| T
|
||||
| {
|
||||
nestedText?: T;
|
||||
id?: T;
|
||||
};
|
||||
nestedText?: T;
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "with-localized-relationship_select".
|
||||
*/
|
||||
export interface WithLocalizedRelationshipSelect<T extends boolean = true> {
|
||||
localizedRelationship?: T;
|
||||
localizedRelationHasManyField?: T;
|
||||
localizedRelationMultiRelationTo?: T;
|
||||
localizedRelationMultiRelationToHasMany?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "relationship-localized_select".
|
||||
*/
|
||||
export interface RelationshipLocalizedSelect<T extends boolean = true> {
|
||||
relationship?: T;
|
||||
relationshipHasMany?: T;
|
||||
relationMultiRelationTo?: T;
|
||||
relationMultiRelationToHasMany?: T;
|
||||
arrayField?:
|
||||
| T
|
||||
| {
|
||||
nestedRelation?: T;
|
||||
id?: T;
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "dummy_select".
|
||||
*/
|
||||
export interface DummySelect<T extends boolean = true> {
|
||||
name?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "nested_select".
|
||||
*/
|
||||
export interface NestedSelect<T extends boolean = true> {
|
||||
blocks?:
|
||||
| T
|
||||
| {
|
||||
block?:
|
||||
| T
|
||||
| {
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
textNotLocalized?: T;
|
||||
id?: T;
|
||||
};
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "groups_select".
|
||||
*/
|
||||
export interface GroupsSelect<T extends boolean = true> {
|
||||
groupLocalizedRow?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
};
|
||||
groupLocalized?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
};
|
||||
group?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
};
|
||||
deep?:
|
||||
| T
|
||||
| {
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
id?: T;
|
||||
};
|
||||
blocks?:
|
||||
| T
|
||||
| {
|
||||
first?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "tabs_select".
|
||||
*/
|
||||
export interface TabsSelect<T extends boolean = true> {
|
||||
tabLocalized?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
};
|
||||
tab?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
};
|
||||
deep?:
|
||||
| T
|
||||
| {
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
id?: T;
|
||||
};
|
||||
blocks?:
|
||||
| T
|
||||
| {
|
||||
first?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "localized-sort_select".
|
||||
*/
|
||||
export interface LocalizedSortSelect<T extends boolean = true> {
|
||||
title?: T;
|
||||
date?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "blocks-same-name_select".
|
||||
*/
|
||||
export interface BlocksSameNameSelect<T extends boolean = true> {
|
||||
blocks?:
|
||||
| T
|
||||
| {
|
||||
block_first?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
block_second?:
|
||||
| T
|
||||
| {
|
||||
title?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "localized-within-localized_select".
|
||||
*/
|
||||
export interface LocalizedWithinLocalizedSelect<T extends boolean = true> {
|
||||
myTab?:
|
||||
| T
|
||||
| {
|
||||
shouldNotBeLocalized?: T;
|
||||
};
|
||||
myArray?:
|
||||
| T
|
||||
| {
|
||||
shouldNotBeLocalized?: T;
|
||||
id?: T;
|
||||
};
|
||||
myBlocks?:
|
||||
| T
|
||||
| {
|
||||
myBlock?:
|
||||
| T
|
||||
| {
|
||||
shouldNotBeLocalized?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
myGroup?:
|
||||
| T
|
||||
| {
|
||||
shouldNotBeLocalized?: T;
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "payload-locked-documents_select".
|
||||
*/
|
||||
export interface PayloadLockedDocumentsSelect<T extends boolean = true> {
|
||||
document?: T;
|
||||
globalSlug?: T;
|
||||
user?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "payload-preferences_select".
|
||||
*/
|
||||
export interface PayloadPreferencesSelect<T extends boolean = true> {
|
||||
user?: T;
|
||||
key?: T;
|
||||
value?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "payload-migrations_select".
|
||||
*/
|
||||
export interface PayloadMigrationsSelect<T extends boolean = true> {
|
||||
name?: T;
|
||||
batch?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "global-array".
|
||||
@@ -651,6 +1169,21 @@ export interface GlobalArray {
|
||||
updatedAt?: string | null;
|
||||
createdAt?: string | null;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "global-array_select".
|
||||
*/
|
||||
export interface GlobalArraySelect<T extends boolean = true> {
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
text?: T;
|
||||
id?: T;
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
globalType?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "auth".
|
||||
|
||||
Reference in New Issue
Block a user