From 881311edb137a956b036edf493d1badf6c67c04c Mon Sep 17 00:00:00 2001 From: Anders Semb Hermansen Date: Fri, 3 Jan 2025 15:27:32 +0100 Subject: [PATCH] fix(storage-vercel-blob): return 404 when file is not found --- packages/storage-vercel-blob/src/staticHandler.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/storage-vercel-blob/src/staticHandler.ts b/packages/storage-vercel-blob/src/staticHandler.ts index aa2743fd7c5..203ad27b279 100644 --- a/packages/storage-vercel-blob/src/staticHandler.ts +++ b/packages/storage-vercel-blob/src/staticHandler.ts @@ -2,7 +2,7 @@ import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types' import type { CollectionConfig } from 'payload' import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities' -import { head } from '@vercel/blob' +import { BlobNotFoundError, head } from '@vercel/blob' import path from 'path' type StaticHandlerArgs = { @@ -24,10 +24,6 @@ export const getStaticHandler = ( const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match') const blobMetadata = await head(fileUrl, { token }) - if (!blobMetadata) { - return new Response(null, { status: 404, statusText: 'Not Found' }) - } - const uploadedAtString = blobMetadata.uploadedAt.toISOString() const ETag = `"${fileKey}-${uploadedAtString}"` @@ -70,6 +66,9 @@ export const getStaticHandler = ( status: 200, }) } catch (err: unknown) { + if (err instanceof BlobNotFoundError) { + return new Response(null, { status: 404, statusText: 'Not Found' }) + } req.payload.logger.error({ err, msg: 'Unexpected error in staticHandler' }) return new Response('Internal Server Error', { status: 500 }) }