templates: fix broken images on Next.js 16 by using relative paths for local media#16058
Conversation
| images: { | ||
| localPatterns: [ | ||
| { | ||
| pathname: '/api/media/file/**', |
There was a problem hiding this comment.
We can't make /api/ dynamic based on the config.routes.api can we?
There was a problem hiding this comment.
@DanRibbens I believe the next.config runs before the Payload config is loaded so I don't think we could.
@AlessioGr thoughts?
There was a problem hiding this comment.
We could import the payload config in the next config and read config.routes.api - I just don't know if we should / if this will cause any performance degradations, since the payload config can be a heavy import, and will run sanitization.
There was a problem hiding this comment.
I believe the next.config runs before the Payload config is loaded so I don't think we could.
We could just import it. Then we'd need a top-level await to run sanitization. Even then I'm worried about any performance implications, I have not tested or investigated this
There was a problem hiding this comment.
I am fine with merging this asap.
We don't need to my question delay this since right now templates are broken.
📦 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.
|
|
🚀 This is included in version v3.81.0 |
…r local media (payloadcms#16058) Fixes payloadcms#16051 Next.js 16 tightened image security so that absolute URLs resolving to private IPs are now blocked. This broke all media in the templates because `getMediaUrl` was prepending `getClientSideURL()` to build absolute URLs like `http://localhost:3000/api/media/file/image.webp`, which then got rejected during image optimization. This PR removes the `getClientSideURL()` prepend from `getMediaUrl` for local paths and adds a required `localPatterns` entry. External URLs from storage plugins are still returned as-is. --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1213794221084158
Fixes #16051
Next.js 16 tightened image security so that absolute URLs resolving to private IPs are now blocked. This broke all media in the templates because
getMediaUrlwas prependinggetClientSideURL()to build absolute URLs likehttp://localhost:3000/api/media/file/image.webp, which then got rejected during image optimization.This PR removes the
getClientSideURL()prepend fromgetMediaUrlfor local paths and adds a requiredlocalPatternsentry. External URLs from storage plugins are still returned as-is.