Repository navigation
fix: full image urls stored in DB - #15089
Conversation
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
|
Hi @JarrodMFlesch, it seems like this PR is somewhat related to the following issue I created a while back: #14562 |
|
@jhb-dev yes this should resolve that open issue since it uses a before change hook to set the correct url. I brought up the issue regarding the /api/media/file/filename being stored earlier today as well. Personally I would like to see it just stored as undefined or null if the file is stored locally. But decided to keep it as is until 4.0 since it is technically a breaking change. |
There was a problem hiding this comment.
Pull request overview
This PR ensures that upload URL fields store relative paths in the database for local files while maintaining absolute URLs for external resources. This change makes the database portable across different deployment environments without requiring URL updates when the serverURL changes.
Key Changes:
- Adds
beforeChangehooks to URL fields that convert full URLs to relative paths for local files before database storage - Refactors image resizing logic by splitting the monolithic
imageResizer.tsinto smaller, focused modules - Updates tests to verify relative URLs are stored in the database while absolute URLs are returned via API
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/uploads/int.spec.ts | Adds comprehensive tests verifying relative URL storage in database for create, update, and duplicate operations |
| test/uploads/e2e.spec.ts | Updates assertions to use regex patterns instead of exact string matches for URL validation |
| test/uploads/config.ts | Sets serverURL to enable full URL testing scenarios |
| packages/payload/src/uploads/types.ts | Adds optional url field to FileSize type and exports FocalPoint type |
| packages/payload/src/uploads/getBaseFields.ts | Adds beforeChange hooks to convert URLs to relative paths before database storage |
| packages/payload/src/uploads/generateFilePathOrURL.ts | New utility to generate file paths or URLs with control over relative/absolute format |
| packages/payload/src/uploads/generateFileData.ts | Updates focal point handling and fixes focal point coordinate assignment bug |
| packages/payload/src/uploads/image-resizing/*.ts | Refactored image resizing functionality into modular components |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
DanRibbens
left a comment
There was a problem hiding this comment.
Looking good other than the test config change.
|
Stoked to see this merged, thank you! I think this may also address my PR here - #14250 |
|
@DanRibbens I think so - I'll test as soon as this makes it into a release |
|
Hi @JarrodMFlesch, in the PR description it states that for external files, the absolute URL is stored in the DB. However, I don't see this behavior when using the S3 plugin. In the API result, the URL field holds the actual S3 URL, but in the DB, the relative path to the file ( I opened an issue with a reproduction: #15382 |
Ah the description should be updated, this was intended behavior. BUT I believe you are correct about when disablePayloadAccessControl is true that we should store the full url for external files. |
…create (#15401) Fixes #15382 ## Summary Adds beforeChange hook to url fields in the cloud-storage-plugin this way urls are stored on create. Which in turn fixes #15382 because the url will not fallback to generating a relative url. The default url field's [beforeChange hook](https://github.com/payloadcms/payload/blob/main/packages/payload/src/uploads/getBaseFields.ts#L139-L149) runs after we create the full url so it will skip creating the relative path. ## Issue If you set `disablePayloadAccessControl: true` you want the full url in the DB - #15089 fixed an issue where `create` was not saving the file url - so this plugin would have a relative url generated for the url fields (payload internal behavior from #15089) no matter what ## Fix Adds beforeChange hook to the `@payloadcms/plugin-cloud-storage` that generates the full url if `disablePayloadAccessControl: true` before the internal url generation runs. This skips the internal url generation because the url will not meet the [requirements](https://github.com/payloadcms/payload/blob/main/packages/payload/src/uploads/generateFilePathOrURL.ts#L30-L33) to create a relative url.
Issue
URL field values can be set as full URLs, but should be stored as paths if they are on the local file system. Previously this was not being handled which is not portable, you should be able to change your serverURL without needing to update the DB documents.
This PR adds a beforeChange hook to the url fields that converts the data to the correct relative url for local files. If the url is external, then that gets returned as is.
Why
serverURLis set #14945 where URLs are being fetched via fetch instead of with local filesystem reads (fixed with beforeChange hook)Flow before:
New Flow:
The new flow also ensures URLs are saved as relative URLs when stored on the local filesystem. This makes files more portable, removing the need to update the DB i.e. if you change your domain for example.
URL fields are stored in the DB as:
/:apiRoute/:collectionSlug/file/:filenamehttps://some-fake-external-website.com/images/1URL field values are returned as:
https://your-domain.com/:apiRoute/:collectionSlug/file/:filename/:apiRoute/:collectionSlug/file/:filenamehttps://some-fake-external-website.com/images/1The majority of the files added in this PR were moved from the imageResizer file.
Clean up old DB entries
If you would like to clean up pre-existing (uploads added before 3.71.0) you can use the following script: