-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix: full image urls stored in DB #15089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good other than the test config change.
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.