Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: significantly reduce form-state response size by up to 2x #9388

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/utilities/initPage/handleAdminPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function getRouteInfo({
globalConfig = config.globals.find((global) => global.slug === globalSlug)
}

// If the collection is using a custom ID, we need to determine it's type
// If the collection is using a custom ID, we need to determine its type
if (collectionConfig && payload) {
if (payload.collections?.[collectionSlug]?.customIDType) {
idType = payload.collections?.[collectionSlug].customIDType
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/views/Account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export const Account: React.FC<AdminViewProps> = async ({
data,
docPermissions,
docPreferences,
experimental: {
optimized: true,
},
locale: locale?.code,
operation: 'update',
renderAllFields: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/views/CreateFirstUser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export const CreateFirstUserView: React.FC<AdminViewProps> = async ({ initPageRe
data,
docPermissions,
docPreferences,
experimental: {
optimized: true,
},
locale: locale?.code,
operation: 'create',
renderAllFields: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/views/Document/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export const renderDocument = async ({
data: doc,
docPermissions,
docPreferences,
experimental: {
optimized: true,
},
fallbackLocale: false,
globalSlug,
locale: locale?.code,
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/admin/forms/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
export type ClientFieldWithOptionalType = MarkOptional<ClientField, 'type'>

export type ClientComponentProps = {
customComponents: FormField['customComponents']
customComponents?: FormField['customComponents']
field: ClientBlock | ClientField | ClientTab
forceRender?: boolean
readOnly?: boolean
Expand Down
21 changes: 18 additions & 3 deletions packages/payload/src/admin/forms/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ export type FieldState = {
*/
fieldSchema?: Field
filterOptions?: FilterOptionsResult
initialValue: unknown
initialValue?: unknown
/**
* @deprecated
* This is a legacy property that is no longer used.
* Please use `fieldIsSidebar(field)` from `payload` instead
* Or check `field.admin.position === 'sidebar'` directly.
**/
isSidebar?: boolean
passesCondition?: boolean
requiresRender?: boolean
rows?: Row[]
valid: boolean
valid?: boolean
validate?: Validate
value: unknown
value?: unknown
}

export type FieldStateWithoutComponents = Omit<FieldState, 'customComponents'>
Expand All @@ -68,6 +74,15 @@ export type BuildFormStateArgs = {
data?: Data
docPermissions: SanitizedDocumentPermissions | undefined
docPreferences: DocumentPreferences
experimental?: {
/**
* If true, makes form state as small as possible by omitting unnecessary properties from the response
* Examples include the `isSidebar`, `passesCondition`, or `valid`, properties. See https://github.com/payloadcms/payload/pull/9388.
* In the next major version, this will be the default behavior.
* @default false
**/
optimized: boolean
}
fallbackLocale?: false | TypedLocale
formState?: FormState
id?: number | string
Expand Down
8 changes: 7 additions & 1 deletion packages/payload/src/admin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,13 @@ export type RenderedField = {
Field: React.ReactNode
indexPath?: string
initialSchemaPath?: string
isSidebar: boolean
/**
* @deprecated
* This is a legacy property that is no longer used.
* Please use `fieldIsSidebar(field)` from `payload` instead
* Or check `field.admin.position === 'sidebar'` directly.
**/
isSidebar?: boolean
path: string
schemaPath: string
type: FieldTypes
Expand Down
9 changes: 1 addition & 8 deletions packages/richtext-lexical/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@
"rootDir": "./src" /* Specify the root folder within your source files. */,
"strict": true
},
"exclude": [
"dist",
"build",
"tests",
"test",
"node_modules",
"eslint.config.js",
],
"exclude": ["dist", "build", "tests", "test", "node_modules", "eslint.config.js"],
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.d.ts", "src/**/*.json"],
"references": [
{ "path": "../payload" },
Expand Down
6 changes: 5 additions & 1 deletion packages/ui/src/forms/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export const Form: React.FC<FormProps> = (props) => {
// Execute server side validations
if (Array.isArray(beforeSubmit)) {
let revalidatedFormState: FormState

const serializableFields = deepCopyObjectSimpleWithoutReactComponents(
contextRef.current.fields,
)
Expand All @@ -241,7 +242,9 @@ export const Form: React.FC<FormProps> = (props) => {
revalidatedFormState = result
}, Promise.resolve())

const isValid = Object.entries(revalidatedFormState).every(([, field]) => field.valid)
const isValid = Object.entries(revalidatedFormState).every(
([, field]) => field.valid !== false,
)

if (!isValid) {
setProcessing(false)
Expand All @@ -268,6 +271,7 @@ export const Form: React.FC<FormProps> = (props) => {
const serializableFields = deepCopyObjectSimpleWithoutReactComponents(
contextRef.current.fields,
)

const data = reduceFieldsToValues(serializableFields, true)

if (overrides) {
Expand Down
Loading