feat(storage-vercel-blob): allow fallback to disk if token not set (#10005)
Previously with `@payloadcms/plugin-storage-blob`, if token was not set, the plugin would throw an error. This caused a less-than-ideal developer experience. With this change, if the `token` value is undefined: - Local storage will be used as a fallback - The error will no longer be thrown.
This commit is contained in:
@@ -52,8 +52,10 @@ export type VercelBlobStorageOptions = {
|
|||||||
* Vercel Blob storage read/write token
|
* Vercel Blob storage read/write token
|
||||||
*
|
*
|
||||||
* Usually process.env.BLOB_READ_WRITE_TOKEN set by Vercel
|
* Usually process.env.BLOB_READ_WRITE_TOKEN set by Vercel
|
||||||
|
*
|
||||||
|
* If unset, the plugin will be disabled and will fallback to local storage
|
||||||
*/
|
*/
|
||||||
token: string
|
token: string | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultUploadOptions: Partial<VercelBlobStorageOptions> = {
|
const defaultUploadOptions: Partial<VercelBlobStorageOptions> = {
|
||||||
@@ -68,14 +70,11 @@ type VercelBlobStoragePlugin = (vercelBlobStorageOpts: VercelBlobStorageOptions)
|
|||||||
export const vercelBlobStorage: VercelBlobStoragePlugin =
|
export const vercelBlobStorage: VercelBlobStoragePlugin =
|
||||||
(options: VercelBlobStorageOptions) =>
|
(options: VercelBlobStorageOptions) =>
|
||||||
(incomingConfig: Config): Config => {
|
(incomingConfig: Config): Config => {
|
||||||
if (options.enabled === false) {
|
// If the plugin is disabled or no token is provided, do not enable the plugin
|
||||||
|
if (options.enabled === false || !options.token) {
|
||||||
return incomingConfig
|
return incomingConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!options.token) {
|
|
||||||
throw new Error('The token argument is required for the Vercel Blob adapter.')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse storeId from token
|
// 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()
|
||||||
|
|
||||||
@@ -136,10 +135,15 @@ function vercelBlobStorageInternal(
|
|||||||
): Adapter {
|
): Adapter {
|
||||||
return ({ collection, prefix }): GeneratedAdapter => {
|
return ({ collection, prefix }): GeneratedAdapter => {
|
||||||
const { access, addRandomSuffix, baseUrl, cacheControlMaxAge, token } = options
|
const { access, addRandomSuffix, baseUrl, cacheControlMaxAge, token } = options
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
throw new Error('Vercel Blob storage token is required')
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: 'vercel-blob',
|
name: 'vercel-blob',
|
||||||
generateURL: getGenerateUrl({ baseUrl, prefix }),
|
generateURL: getGenerateUrl({ baseUrl, prefix }),
|
||||||
handleDelete: getHandleDelete({ baseUrl, prefix, token: options.token }),
|
handleDelete: getHandleDelete({ baseUrl, prefix, token }),
|
||||||
handleUpload: getHandleUpload({
|
handleUpload: getHandleUpload({
|
||||||
access,
|
access,
|
||||||
addRandomSuffix,
|
addRandomSuffix,
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import type { CollectionConfig } from 'payload'
|
|||||||
export const Media: CollectionConfig = {
|
export const Media: CollectionConfig = {
|
||||||
slug: 'media',
|
slug: 'media',
|
||||||
upload: {
|
upload: {
|
||||||
disableLocalStorage: true,
|
|
||||||
resizeOptions: {
|
resizeOptions: {
|
||||||
position: 'center',
|
position: 'center',
|
||||||
width: 200,
|
width: 200,
|
||||||
|
|||||||
Reference in New Issue
Block a user