Skip to content

Commit dfddee2

Browse files
authored
fix(storage-*): ensure client handler is always added to the import map, even if the plugin is disabled (#11438)
Ensures that even if you pass `enabled: false` to the storage adapter options, e.g: ```ts s3Storage({ enabled: false, collections: { [mediaSlug]: true, }, bucket: process.env.S3_BUCKET, config: { credentials: { accessKeyId: process.env.S3_ACCESS_KEY_ID, secretAccessKey: process.env.S3_SECRET_ACCESS_KEY, }, }, }) ``` the client handler component is added to the import map. This prevents errors when you use the adapter only on production, but you don't regenerate the import map before running the build
1 parent e055565 commit dfddee2

File tree

5 files changed

+62
-73
lines changed

5 files changed

+62
-73
lines changed

packages/storage-azure/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,19 @@ type AzureStoragePlugin = (azureStorageArgs: AzureStorageOptions) => Plugin
6464
export const azureStorage: AzureStoragePlugin =
6565
(azureStorageOptions: AzureStorageOptions) =>
6666
(incomingConfig: Config): Config => {
67-
if (azureStorageOptions.enabled === false) {
68-
return incomingConfig
69-
}
70-
7167
const getStorageClient = () =>
7268
getStorageClientFunc({
7369
connectionString: azureStorageOptions.connectionString,
7470
containerName: azureStorageOptions.containerName,
7571
})
7672

73+
const isPluginDisabled = azureStorageOptions.enabled === false
74+
7775
initClientUploads({
7876
clientHandler: '@payloadcms/storage-azure/client#AzureClientUploadHandler',
7977
collections: azureStorageOptions.collections,
8078
config: incomingConfig,
81-
enabled: !!azureStorageOptions.clientUploads,
79+
enabled: !isPluginDisabled && Boolean(azureStorageOptions.clientUploads),
8280
serverHandler: getGenerateSignedURLHandler({
8381
access:
8482
typeof azureStorageOptions.clientUploads === 'object'
@@ -91,6 +89,10 @@ export const azureStorage: AzureStoragePlugin =
9189
serverHandlerPath: '/storage-azure-generate-signed-url',
9290
})
9391

92+
if (isPluginDisabled) {
93+
return incomingConfig
94+
}
95+
9496
const adapter = azureStorageInternal(getStorageClient, azureStorageOptions)
9597

9698
// Add adapter to each collection option object

packages/storage-gcs/src/index.ts

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { Config, Plugin, UploadCollectionSlug } from 'payload'
1010

1111
import { Storage } from '@google-cloud/storage'
1212
import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'
13+
import { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities'
1314

1415
import { getGenerateSignedURLHandler } from './generateSignedURL.js'
1516
import { getGenerateURL } from './generateURL.js'
@@ -52,10 +53,6 @@ type GcsStoragePlugin = (gcsStorageArgs: GcsStorageOptions) => Plugin
5253
export const gcsStorage: GcsStoragePlugin =
5354
(gcsStorageOptions: GcsStorageOptions) =>
5455
(incomingConfig: Config): Config => {
55-
if (gcsStorageOptions.enabled === false) {
56-
return incomingConfig
57-
}
58-
5956
let storageClient: null | Storage = null
6057

6158
const getStorageClient = (): Storage => {
@@ -69,46 +66,27 @@ export const gcsStorage: GcsStoragePlugin =
6966

7067
const adapter = gcsStorageInternal(getStorageClient, gcsStorageOptions)
7168

72-
if (gcsStorageOptions.clientUploads) {
73-
if (!incomingConfig.endpoints) {
74-
incomingConfig.endpoints = []
75-
}
76-
77-
incomingConfig.endpoints.push({
78-
handler: getGenerateSignedURLHandler({
79-
access:
80-
typeof gcsStorageOptions.clientUploads === 'object'
81-
? gcsStorageOptions.clientUploads.access
82-
: undefined,
83-
bucket: gcsStorageOptions.bucket,
84-
collections: gcsStorageOptions.collections,
85-
getStorageClient,
86-
}),
87-
method: 'post',
88-
path: '/storage-gcs-generate-signed-url',
89-
})
90-
}
91-
92-
if (!incomingConfig.admin) {
93-
incomingConfig.admin = {}
94-
}
95-
96-
if (!incomingConfig.admin.components) {
97-
incomingConfig.admin.components = {}
98-
}
99-
100-
if (!incomingConfig.admin.components.providers) {
101-
incomingConfig.admin.components.providers = []
102-
}
69+
const isPluginDisabled = gcsStorageOptions.enabled === false
70+
71+
initClientUploads({
72+
clientHandler: '@payloadcms/storage-gcs/client#GcsClientUploadHandler',
73+
collections: gcsStorageOptions.collections,
74+
config: incomingConfig,
75+
enabled: !isPluginDisabled && Boolean(gcsStorageOptions.enabled),
76+
serverHandler: getGenerateSignedURLHandler({
77+
access:
78+
typeof gcsStorageOptions.clientUploads === 'object'
79+
? gcsStorageOptions.clientUploads.access
80+
: undefined,
81+
bucket: gcsStorageOptions.bucket,
82+
collections: gcsStorageOptions.collections,
83+
getStorageClient,
84+
}),
85+
serverHandlerPath: '/storage-gcs-generate-signed-url',
86+
})
10387

104-
for (const collectionSlug in gcsStorageOptions.collections) {
105-
incomingConfig.admin.components.providers.push({
106-
clientProps: {
107-
collectionSlug,
108-
enabled: !!gcsStorageOptions.clientUploads,
109-
},
110-
path: '@payloadcms/storage-gcs/client#GcsClientUploadHandler',
111-
})
88+
if (isPluginDisabled) {
89+
return incomingConfig
11290
}
11391

11492
// Add adapter to each collection option object

packages/storage-s3/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ type S3StoragePlugin = (storageS3Args: S3StorageOptions) => Plugin
6767
export const s3Storage: S3StoragePlugin =
6868
(s3StorageOptions: S3StorageOptions) =>
6969
(incomingConfig: Config): Config => {
70-
if (s3StorageOptions.enabled === false) {
71-
return incomingConfig
72-
}
73-
7470
let storageClient: AWS.S3 | null = null
7571

7672
const getStorageClient: () => AWS.S3 = () => {
@@ -81,11 +77,13 @@ export const s3Storage: S3StoragePlugin =
8177
return storageClient
8278
}
8379

80+
const isPluginDisabled = s3StorageOptions.enabled === false
81+
8482
initClientUploads({
8583
clientHandler: '@payloadcms/storage-s3/client#S3ClientUploadHandler',
8684
collections: s3StorageOptions.collections,
8785
config: incomingConfig,
88-
enabled: !!s3StorageOptions.clientUploads,
86+
enabled: !isPluginDisabled && Boolean(s3StorageOptions.clientUploads),
8987
serverHandler: getGenerateSignedURLHandler({
9088
access:
9189
typeof s3StorageOptions.clientUploads === 'object'
@@ -99,6 +97,10 @@ export const s3Storage: S3StoragePlugin =
9997
serverHandlerPath: '/storage-s3-generate-signed-url',
10098
})
10199

100+
if (isPluginDisabled) {
101+
return incomingConfig
102+
}
103+
102104
const adapter = s3StorageInternal(getStorageClient, s3StorageOptions)
103105

104106
// Add adapter to each collection option object

packages/storage-uploadthing/src/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,13 @@ export type ACL = 'private' | 'public-read'
5656
export const uploadthingStorage: UploadthingPlugin =
5757
(uploadthingStorageOptions: UploadthingStorageOptions) =>
5858
(incomingConfig: Config): Config => {
59-
if (uploadthingStorageOptions.enabled === false) {
60-
return incomingConfig
61-
}
62-
63-
// Default ACL to public-read
64-
if (!uploadthingStorageOptions.options.acl) {
65-
uploadthingStorageOptions.options.acl = 'public-read'
66-
}
67-
68-
const adapter = uploadthingInternal(uploadthingStorageOptions)
59+
const isPluginDisabled = uploadthingStorageOptions.enabled === false
6960

7061
initClientUploads({
7162
clientHandler: '@payloadcms/storage-uploadthing/client#UploadthingClientUploadHandler',
7263
collections: uploadthingStorageOptions.collections,
7364
config: incomingConfig,
74-
enabled: !!uploadthingStorageOptions.clientUploads,
65+
enabled: !isPluginDisabled && Boolean(uploadthingStorageOptions.clientUploads),
7566
serverHandler: getClientUploadRoute({
7667
access:
7768
typeof uploadthingStorageOptions.clientUploads === 'object'
@@ -83,6 +74,17 @@ export const uploadthingStorage: UploadthingPlugin =
8374
serverHandlerPath: '/storage-uploadthing-client-upload-route',
8475
})
8576

77+
if (isPluginDisabled) {
78+
return incomingConfig
79+
}
80+
81+
// Default ACL to public-read
82+
if (!uploadthingStorageOptions.options.acl) {
83+
uploadthingStorageOptions.options.acl = 'public-read'
84+
}
85+
86+
const adapter = uploadthingInternal(uploadthingStorageOptions)
87+
8688
// Add adapter to each collection option object
8789
const collectionsWithAdapter: CloudStoragePluginOptions['collections'] = Object.entries(
8890
uploadthingStorageOptions.collections,

packages/storage-vercel-blob/src/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ type VercelBlobStoragePlugin = (vercelBlobStorageOpts: VercelBlobStorageOptions)
8080
export const vercelBlobStorage: VercelBlobStoragePlugin =
8181
(options: VercelBlobStorageOptions) =>
8282
(incomingConfig: Config): Config => {
83-
// If the plugin is disabled or no token is provided, do not enable the plugin
84-
if (options.enabled === false || !options.token) {
85-
return incomingConfig
86-
}
87-
8883
// Parse storeId from token
89-
const storeId = options.token.match(/^vercel_blob_rw_([a-z\d]+)_[a-z\d]+$/i)?.[1]?.toLowerCase()
84+
const storeId = options.token
85+
?.match(/^vercel_blob_rw_([a-z\d]+)_[a-z\d]+$/i)?.[1]
86+
?.toLowerCase()
87+
88+
const isPluginDisabled = options.enabled === false || !options.token
9089

91-
if (!storeId) {
90+
// Don't throw if the plugin is disabled
91+
if (!storeId && !isPluginDisabled) {
9292
throw new Error(
9393
'Invalid token format for Vercel Blob adapter. Should be vercel_blob_rw_<store_id>_<random_string>.',
9494
)
@@ -108,7 +108,7 @@ export const vercelBlobStorage: VercelBlobStoragePlugin =
108108
clientHandler: '@payloadcms/storage-vercel-blob/client#VercelBlobClientUploadHandler',
109109
collections: options.collections,
110110
config: incomingConfig,
111-
enabled: !!options.clientUploads,
111+
enabled: !isPluginDisabled && Boolean(options.clientUploads),
112112
extraClientHandlerProps: (collection) => ({
113113
addRandomSuffix: !!optionsWithDefaults.addRandomSuffix,
114114
baseURL: baseUrl,
@@ -119,11 +119,16 @@ export const vercelBlobStorage: VercelBlobStoragePlugin =
119119
typeof options.clientUploads === 'object' ? options.clientUploads.access : undefined,
120120
addRandomSuffix: optionsWithDefaults.addRandomSuffix,
121121
cacheControlMaxAge: options.cacheControlMaxAge,
122-
token: options.token,
122+
token: options.token ?? '',
123123
}),
124124
serverHandlerPath: '/vercel-blob-client-upload-route',
125125
})
126126

127+
// If the plugin is disabled or no token is provided, do not enable the plugin
128+
if (isPluginDisabled) {
129+
return incomingConfig
130+
}
131+
127132
const adapter = vercelBlobStorageInternal({ ...optionsWithDefaults, baseUrl })
128133

129134
// Add adapter to each collection option object

0 commit comments

Comments
 (0)