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
This commit is contained in:
Sasha
2025-02-28 16:34:00 +02:00
committed by GitHub
parent e055565ca8
commit dfddee2125
5 changed files with 61 additions and 72 deletions

View File

@@ -80,15 +80,15 @@ type VercelBlobStoragePlugin = (vercelBlobStorageOpts: VercelBlobStorageOptions)
export const vercelBlobStorage: VercelBlobStoragePlugin =
(options: VercelBlobStorageOptions) =>
(incomingConfig: Config): Config => {
// If the plugin is disabled or no token is provided, do not enable the plugin
if (options.enabled === false || !options.token) {
return incomingConfig
}
// Parse storeId from token
const storeId = options.token.match(/^vercel_blob_rw_([a-z\d]+)_[a-z\d]+$/i)?.[1]?.toLowerCase()
const storeId = options.token
?.match(/^vercel_blob_rw_([a-z\d]+)_[a-z\d]+$/i)?.[1]
?.toLowerCase()
if (!storeId) {
const isPluginDisabled = options.enabled === false || !options.token
// Don't throw if the plugin is disabled
if (!storeId && !isPluginDisabled) {
throw new Error(
'Invalid token format for Vercel Blob adapter. Should be vercel_blob_rw_<store_id>_<random_string>.',
)
@@ -108,7 +108,7 @@ export const vercelBlobStorage: VercelBlobStoragePlugin =
clientHandler: '@payloadcms/storage-vercel-blob/client#VercelBlobClientUploadHandler',
collections: options.collections,
config: incomingConfig,
enabled: !!options.clientUploads,
enabled: !isPluginDisabled && Boolean(options.clientUploads),
extraClientHandlerProps: (collection) => ({
addRandomSuffix: !!optionsWithDefaults.addRandomSuffix,
baseURL: baseUrl,
@@ -119,11 +119,16 @@ export const vercelBlobStorage: VercelBlobStoragePlugin =
typeof options.clientUploads === 'object' ? options.clientUploads.access : undefined,
addRandomSuffix: optionsWithDefaults.addRandomSuffix,
cacheControlMaxAge: options.cacheControlMaxAge,
token: options.token,
token: options.token ?? '',
}),
serverHandlerPath: '/vercel-blob-client-upload-route',
})
// If the plugin is disabled or no token is provided, do not enable the plugin
if (isPluginDisabled) {
return incomingConfig
}
const adapter = vercelBlobStorageInternal({ ...optionsWithDefaults, baseUrl })
// Add adapter to each collection option object