Skip to content

Commit bcb10b5

Browse files
authored
fix: restore missing properties to live preview client config (#12904)
Needed for #12860. The client config unnecessarily omits the `livePreview.collections` and `livePreview.globals` properties. This is because the root live preview config extends the type with these two additional properties without sharing it elsewhere. To led to the client sanitization function overlooking these additional properties, as there was no type indication that they exist. The `collections` and `globals` properties are now appended to the client config as expected, and the root live preview is standardized behind the `RootLivePreviewConfig` type to ensure no properties are lost. --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1210628466702823
1 parent 87c7952 commit bcb10b5

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

packages/payload/src/collections/config/client.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ export const createClientCollectionConfig = ({
122122
if (!collection.admin) {
123123
break
124124
}
125+
125126
clientCollection.admin = {} as ClientCollectionConfig['admin']
127+
126128
for (const adminKey in collection.admin) {
127129
if (serverOnlyCollectionAdminProperties.includes(adminKey as any)) {
128130
continue
@@ -139,78 +141,100 @@ export const createClientCollectionConfig = ({
139141
}
140142
} else if (typeof collection.admin.description === 'function') {
141143
const description = collection.admin.description({ t: i18n.t as TFunction })
144+
142145
if (description) {
143146
clientCollection.admin.description = description
144147
}
145148
}
146149
break
150+
147151
case 'livePreview':
148-
clientCollection.admin.livePreview =
149-
{} as ClientCollectionConfig['admin']['livePreview']
152+
clientCollection.admin.livePreview = {}
153+
150154
if (collection.admin.livePreview?.breakpoints) {
151-
clientCollection.admin.livePreview!.breakpoints =
155+
clientCollection.admin.livePreview.breakpoints =
152156
collection.admin.livePreview.breakpoints
153157
}
158+
154159
break
160+
155161
case 'preview':
156162
if (collection.admin.preview) {
157163
clientCollection.admin.preview = true
158164
}
165+
159166
break
167+
160168
default:
161169
;(clientCollection as any).admin[adminKey] =
162170
collection.admin[adminKey as keyof SanitizedCollectionConfig['admin']]
163171
}
164172
}
173+
165174
break
175+
166176
case 'auth':
167177
if (!collection.auth) {
168178
break
169179
}
180+
170181
clientCollection.auth = {} as { verify?: true } & SanitizedCollectionConfig['auth']
182+
171183
if (collection.auth.cookies) {
172184
clientCollection.auth.cookies = collection.auth.cookies
173185
}
186+
174187
if (collection.auth.depth !== undefined) {
175188
// Check for undefined as it can be a number (0)
176189
clientCollection.auth.depth = collection.auth.depth
177190
}
191+
178192
if (collection.auth.disableLocalStrategy) {
179193
clientCollection.auth.disableLocalStrategy = collection.auth.disableLocalStrategy
180194
}
195+
181196
if (collection.auth.lockTime !== undefined) {
182197
// Check for undefined as it can be a number (0)
183198
clientCollection.auth.lockTime = collection.auth.lockTime
184199
}
200+
185201
if (collection.auth.loginWithUsername) {
186202
clientCollection.auth.loginWithUsername = collection.auth.loginWithUsername
187203
}
204+
188205
if (collection.auth.maxLoginAttempts !== undefined) {
189206
// Check for undefined as it can be a number (0)
190207
clientCollection.auth.maxLoginAttempts = collection.auth.maxLoginAttempts
191208
}
209+
192210
if (collection.auth.removeTokenFromResponses) {
193211
clientCollection.auth.removeTokenFromResponses = collection.auth.removeTokenFromResponses
194212
}
195213

196214
if (collection.auth.useAPIKey) {
197215
clientCollection.auth.useAPIKey = collection.auth.useAPIKey
198216
}
217+
199218
if (collection.auth.tokenExpiration) {
200219
clientCollection.auth.tokenExpiration = collection.auth.tokenExpiration
201220
}
221+
202222
if (collection.auth.verify) {
203223
clientCollection.auth.verify = true
204224
}
225+
205226
break
227+
206228
case 'fields':
207229
clientCollection.fields = createClientFields({
208230
defaultIDType,
209231
fields: collection.fields,
210232
i18n,
211233
importMap,
212234
})
235+
213236
break
237+
214238
case 'labels':
215239
clientCollection.labels = {
216240
plural:
@@ -222,16 +246,21 @@ export const createClientCollectionConfig = ({
222246
? collection.labels.singular({ i18n, t: i18n.t as TFunction })
223247
: collection.labels.singular,
224248
}
249+
225250
break
251+
226252
case 'upload':
227253
if (!collection.upload) {
228254
break
229255
}
256+
230257
clientCollection.upload = {} as SanitizedUploadConfig
258+
231259
for (const uploadKey in collection.upload) {
232260
if (serverOnlyUploadProperties.includes(uploadKey as any)) {
233261
continue
234262
}
263+
235264
if (uploadKey === 'imageSizes') {
236265
clientCollection.upload.imageSizes = collection.upload.imageSizes?.map((size) => {
237266
const sanitizedSize = { ...size }
@@ -245,6 +274,7 @@ export const createClientCollectionConfig = ({
245274
collection.upload[uploadKey as keyof SanitizedUploadConfig]
246275
}
247276
}
277+
248278
break
249279

250280
default:

packages/payload/src/config/client.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ImportMap } from '../bin/generateImportMap/index.js'
55
import type { ClientBlock } from '../fields/config/types.js'
66
import type { BlockSlug } from '../index.js'
77
import type {
8-
LivePreviewConfig,
8+
RootLivePreviewConfig,
99
SanitizedConfig,
1010
ServerOnlyLivePreviewProperties,
1111
} from './types.js'
@@ -44,7 +44,7 @@ export type ServerOnlyRootAdminProperties = keyof Pick<SanitizedConfig['admin'],
4444

4545
export type UnsanitizedClientConfig = {
4646
admin: {
47-
livePreview?: Omit<LivePreviewConfig, ServerOnlyLivePreviewProperties>
47+
livePreview?: Omit<RootLivePreviewConfig, ServerOnlyLivePreviewProperties>
4848
} & Omit<SanitizedConfig['admin'], 'components' | 'dependencies' | 'livePreview'>
4949
blocks: ClientBlock[]
5050
collections: ClientCollectionConfig[]
@@ -54,7 +54,7 @@ export type UnsanitizedClientConfig = {
5454

5555
export type ClientConfig = {
5656
admin: {
57-
livePreview?: Omit<LivePreviewConfig, ServerOnlyLivePreviewProperties>
57+
livePreview?: Omit<RootLivePreviewConfig, ServerOnlyLivePreviewProperties>
5858
} & Omit<SanitizedConfig['admin'], 'components' | 'dependencies' | 'livePreview'>
5959
blocks: ClientBlock[]
6060
blocksMap: Record<BlockSlug, ClientBlock>
@@ -103,6 +103,7 @@ export const createClientConfig = ({
103103
if (serverOnlyConfigProperties.includes(key as any)) {
104104
continue
105105
}
106+
106107
switch (key) {
107108
case 'admin':
108109
clientConfig.admin = {
@@ -117,14 +118,25 @@ export const createClientConfig = ({
117118
timezones: config.admin.timezones,
118119
user: config.admin.user,
119120
}
121+
120122
if (config.admin.livePreview) {
121123
clientConfig.admin.livePreview = {}
122124

123125
if (config.admin.livePreview.breakpoints) {
124126
clientConfig.admin.livePreview.breakpoints = config.admin.livePreview.breakpoints
125127
}
128+
129+
if (config.admin.livePreview.collections) {
130+
clientConfig.admin.livePreview.collections = config.admin.livePreview.collections
131+
}
132+
133+
if (config.admin.livePreview.globals) {
134+
clientConfig.admin.livePreview.globals = config.admin.livePreview.globals
135+
}
126136
}
137+
127138
break
139+
128140
case 'blocks': {
129141
;(clientConfig.blocks as ClientBlock[]) = createClientBlocks({
130142
blocks: config.blocks!,
@@ -135,14 +147,17 @@ export const createClientConfig = ({
135147

136148
break
137149
}
150+
138151
case 'collections':
139152
;(clientConfig.collections as ClientCollectionConfig[]) = createClientCollectionConfigs({
140153
collections: config.collections,
141154
defaultIDType: config.db.defaultIDType,
142155
i18n,
143156
importMap,
144157
})
158+
145159
break
160+
146161
case 'folders':
147162
if (config.folders) {
148163
clientConfig.folders = {
@@ -152,6 +167,7 @@ export const createClientConfig = ({
152167
fieldName: config.folders.fieldName,
153168
}
154169
}
170+
155171
break
156172

157173
case 'globals':
@@ -161,49 +177,65 @@ export const createClientConfig = ({
161177
i18n,
162178
importMap,
163179
})
180+
164181
break
182+
165183
case 'localization':
166184
if (typeof config.localization === 'object' && config.localization) {
167185
clientConfig.localization = {}
186+
168187
if (config.localization.defaultLocale) {
169188
clientConfig.localization.defaultLocale = config.localization.defaultLocale
170189
}
190+
171191
if (config.localization.defaultLocalePublishOption) {
172192
clientConfig.localization.defaultLocalePublishOption =
173193
config.localization.defaultLocalePublishOption
174194
}
195+
175196
if (config.localization.fallback) {
176197
clientConfig.localization.fallback = config.localization.fallback
177198
}
199+
178200
if (config.localization.localeCodes) {
179201
clientConfig.localization.localeCodes = config.localization.localeCodes
180202
}
203+
181204
if (config.localization.locales) {
182205
clientConfig.localization.locales = []
206+
183207
for (const locale of config.localization.locales) {
184208
if (locale) {
185209
const clientLocale: Partial<(typeof config.localization.locales)[0]> = {}
210+
186211
if (locale.code) {
187212
clientLocale.code = locale.code
188213
}
214+
189215
if (locale.fallbackLocale) {
190216
clientLocale.fallbackLocale = locale.fallbackLocale
191217
}
218+
192219
if (locale.label) {
193220
clientLocale.label = locale.label
194221
}
222+
195223
if (locale.rtl) {
196224
clientLocale.rtl = locale.rtl
197225
}
226+
198227
clientConfig.localization.locales.push(clientLocale)
199228
}
200229
}
201230
}
202231
}
232+
203233
break
234+
204235
default:
205236
;(clientConfig as any)[key] = config[key as keyof SanitizedConfig]
206237
}
207238
}
239+
208240
return clientConfig as ClientConfig
209241
}

packages/payload/src/config/types.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ export type LivePreviewConfig = {
172172
| string
173173
}
174174

175+
export type RootLivePreviewConfig = {
176+
collections?: string[]
177+
globals?: string[]
178+
} & LivePreviewConfig
179+
175180
export type OGImageConfig = {
176181
alt?: string
177182
height?: number | string
@@ -202,7 +207,7 @@ export type MetaConfig = {
202207
titleSuffix?: string
203208
} & DeepClone<Metadata>
204209

205-
export type ServerOnlyLivePreviewProperties = keyof Pick<LivePreviewConfig, 'url'>
210+
export type ServerOnlyLivePreviewProperties = keyof Pick<RootLivePreviewConfig, 'url'>
206211

207212
type GeneratePreviewURLOptions = {
208213
locale: string
@@ -861,10 +866,7 @@ export type Config = {
861866
*/
862867
importMapFile?: string
863868
}
864-
livePreview?: {
865-
collections?: string[]
866-
globals?: string[]
867-
} & LivePreviewConfig
869+
livePreview?: RootLivePreviewConfig
868870
/** Base meta data to use for the Admin Panel. Included properties are titleSuffix, ogImage, and favicon. */
869871
meta?: MetaConfig
870872
routes?: {

0 commit comments

Comments
 (0)