Skip to content

Commit fa49e04

Browse files
authored
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.
1 parent f54e180 commit fa49e04

File tree

2 files changed

+11
-8
lines changed
  • packages/storage-vercel-blob/src
  • test/storage-vercel-blob/collections

2 files changed

+11
-8
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ export type VercelBlobStorageOptions = {
5252
* Vercel Blob storage read/write token
5353
*
5454
* Usually process.env.BLOB_READ_WRITE_TOKEN set by Vercel
55+
*
56+
* If unset, the plugin will be disabled and will fallback to local storage
5557
*/
56-
token: string
58+
token: string | undefined
5759
}
5860

5961
const defaultUploadOptions: Partial<VercelBlobStorageOptions> = {
@@ -68,14 +70,11 @@ type VercelBlobStoragePlugin = (vercelBlobStorageOpts: VercelBlobStorageOptions)
6870
export const vercelBlobStorage: VercelBlobStoragePlugin =
6971
(options: VercelBlobStorageOptions) =>
7072
(incomingConfig: Config): Config => {
71-
if (options.enabled === false) {
73+
// If the plugin is disabled or no token is provided, do not enable the plugin
74+
if (options.enabled === false || !options.token) {
7275
return incomingConfig
7376
}
7477

75-
if (!options.token) {
76-
throw new Error('The token argument is required for the Vercel Blob adapter.')
77-
}
78-
7978
// Parse storeId from token
8079
const storeId = options.token.match(/^vercel_blob_rw_([a-z\d]+)_[a-z\d]+$/i)?.[1]?.toLowerCase()
8180

@@ -136,10 +135,15 @@ function vercelBlobStorageInternal(
136135
): Adapter {
137136
return ({ collection, prefix }): GeneratedAdapter => {
138137
const { access, addRandomSuffix, baseUrl, cacheControlMaxAge, token } = options
138+
139+
if (!token) {
140+
throw new Error('Vercel Blob storage token is required')
141+
}
142+
139143
return {
140144
name: 'vercel-blob',
141145
generateURL: getGenerateUrl({ baseUrl, prefix }),
142-
handleDelete: getHandleDelete({ baseUrl, prefix, token: options.token }),
146+
handleDelete: getHandleDelete({ baseUrl, prefix, token }),
143147
handleUpload: getHandleUpload({
144148
access,
145149
addRandomSuffix,

test/storage-vercel-blob/collections/Media.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { CollectionConfig } from 'payload'
33
export const Media: CollectionConfig = {
44
slug: 'media',
55
upload: {
6-
disableLocalStorage: true,
76
resizeOptions: {
87
position: 'center',
98
width: 200,

0 commit comments

Comments
 (0)