[backport] Ensure setAssetPrefix updates config instance #82165
[backport] Ensure setAssetPrefix updates config instance #82165
Conversation
This ensures we update `nextConfig.assetPrefix` instead of `renderOpts` as we aren't relying on `renderOpts` for these values anymore. Closes: #82150
|
|
||
| public setAssetPrefix(prefix?: string): void { | ||
| this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : '' | ||
| this.nextConfig.assetPrefix = prefix ? prefix.replace(/\/$/, '') : '' |
There was a problem hiding this comment.
The setAssetPrefix() method now sets this.nextConfig.assetPrefix instead of this.renderOpts.assetPrefix, but rendering code expects the asset prefix to be available in renderOpts.
View Details
Analysis
The change from this.renderOpts.assetPrefix = prefix to this.nextConfig.assetPrefix = prefix breaks the asset prefix functionality during rendering. Multiple parts of the rendering pipeline expect renderOpts.assetPrefix to be available:
packages/next/src/server/post-process.ts:45usesrenderOpts.assetPrefixfor CSS optimizationpackages/next/src/server/async-storage/work-store.ts:137usesrenderOpts?.assetPrefix || ''for work store setup- Both the Pages and App Router
RenderOptsPartialtype definitions includeassetPrefix?: stringas an expected property
The renderOpts object is created by spreading this.renderOpts (lines 1798 and 2552), but the initial renderOpts construction (line 562+) doesn't include assetPrefix from nextConfig. The asset prefix is meant to be set dynamically via the setAssetPrefix() method, but now it's being set on the wrong object.
This will cause asset prefix functionality to fail, resulting in incorrect URLs for static assets, potentially breaking CSS optimization, and causing the work store to default to an empty asset prefix.
Recommendation
Change line 1710 back to set the asset prefix on renderOpts instead of nextConfig:
this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''If there's a need to also update nextConfig.assetPrefix for consistency, both should be set:
const normalizedPrefix = prefix ? prefix.replace(/\/$/, '') : ''
this.renderOpts.assetPrefix = normalizedPrefix
this.nextConfig.assetPrefix = normalizedPrefix
Backports #82160 to our next-15-4 release branch