fix(storage-uploadthing): set url field on uploaded media documents #14250
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
The UploadThing storage adapter had two critical bugs that prevented images from loading correctly:
Missing URL field on upload: The
handleUpload
function only set_key
andfilename
fields from the UploadThing API response, but not theurl
field. This caused newly uploaded images to have no URL in the database.Disabled generateURL hook: The
disablePayloadAccessControl
setting was commented out, which prevented Payload's cloud storage plugin from callingadapter.generateURL()
in theafterRead
hook. This caused the API to fall back to generating/api/media/file/*
URLs instead of using UploadThing CDN URLs (utfs.io
), making all images inaccessible.How?
This PR includes two fixes, and a documentation change:
Set URL on upload (
handleUpload.ts
): Adddata.url = res.data?.url
for both main file uploads and image size variant uploads, so newly uploaded files have the correct UploadThing CDN URL stored in the database.Enable disablePayloadAccessControl (
index.ts
): Uncomment thedisablePayloadAccessControl: true
setting when ACL ispublic-read
. This ensures Payload callsadapter.generateURL()
when serializing documents, which generates the correct UploadThing CDN URLs for existing files.Document JWT token requirement (
README.md
): Add documentation clarifying that this plugin requires UploadThing tokens in JWT format (starting withey
), not thesk_
secret key format. I spent a while trying to figure out why uploadThing wasn't working - their dashboard has 2 quick-copyable different formats for API key, and I was using the wrong one!Testing
https://utfs.io/f/{key}
URLs instead of/api/media/file/*
URLsFixes: Images returning 400 errors and failing to load from
/api/media/file/*