Fixed notfound headers, r2storage

This commit is contained in:
2026-10-13 15:57:59 +02:00
parent 86c873e4e7
commit 786dd346a9
24 changed files with 1506 additions and 2765 deletions
+48 -3
View File
@@ -1,6 +1,27 @@
import type { Plugin } from 'payload'
import { s3Storage } from '@payloadcms/storage-s3'
type GenerateFileURL = (args: { filename: string; prefix?: string }) => string
/**
* Minimal shape of `s3Storage` from @payloadcms/storage-s3 — the plugin never
* imports that package (it's optional: projects on local disk don't install it).
* The project injects the real function; TypeScript checks it fits this shape.
*/
export type S3StorageFactory = (options: {
bucket: string
collections: Record<string, { generateFileURL: GenerateFileURL } | true>
config: {
credentials: { accessKeyId: string; secretAccessKey: string }
endpoint: string
forcePathStyle: boolean
region: string
}
}) => Plugin
export type BuildR2StorageDeps = {
/** `import { s3Storage } from '@payloadcms/storage-s3'` — required when R2 env is set. */
s3Storage?: S3StorageFactory
}
/**
* Cloudflare R2 media storage — configured from environment variables (agency
@@ -16,9 +37,22 @@ import { s3Storage } from '@payloadcms/storage-s3'
* storage (useful in dev without R2). This mirrors how mailAdapter degrades
* gracefully when a transport isn't configured.
*
* The S3 adapter is INJECTED by the project (same pattern as 2FA / NextResponse):
*
* import { s3Storage } from '@payloadcms/storage-s3'
* plugins: [buildR2Storage(['media'], { s3Storage })]
*
* A static import here would make @payloadcms/storage-s3 mandatory for every
* project — including ones on local disk — because this module is exported
* from the package's main entry.
*
* @param collections - slugs of upload collections to offload to R2 (e.g. ['media'])
* @param deps - `{ s3Storage }` from @payloadcms/storage-s3 (needed only when R2 env is set)
*/
export const buildR2Storage = (collections: string[] = ['media']): Plugin => {
export const buildR2Storage = (
collections: string[] = ['media'],
deps: BuildR2StorageDeps = {},
): Plugin => {
const bucket = process.env.R2_BUCKET
const endpoint = process.env.R2_ENDPOINT
const accessKeyId = process.env.R2_ACCESS_KEY_ID
@@ -54,7 +88,18 @@ export const buildR2Storage = (collections: string[] = ['media']): Plugin => {
[publicUrl, prefix, filename].filter(Boolean).join('/')
: undefined
const collectionsConfig: Record<string, { generateFileURL: typeof generateFileURL } | true> = {}
// R2 env is complete → the adapter is required. Fail loudly at boot rather
// than silently storing media on the container disk (lost on redeploy).
const { s3Storage } = deps
if (!s3Storage) {
throw new Error(
'[ipal] R2 storage: R2_* env is set but the S3 adapter was not injected. ' +
"Install @payloadcms/storage-s3 and pass it: buildR2Storage(['media'], { s3Storage }) " +
"with `import { s3Storage } from '@payloadcms/storage-s3'`. See docs/storage.md.",
)
}
const collectionsConfig: Record<string, { generateFileURL: GenerateFileURL } | true> = {}
for (const slug of collections) {
collectionsConfig[slug] = generateFileURL ? { generateFileURL } : true
}
+1
View File
@@ -1 +1,2 @@
export { buildR2Storage } from './buildR2Storage.js'
export type { BuildR2StorageDeps, S3StorageFactory } from './buildR2Storage.js'