Skip to content

Commit 485497d

Browse files
authored
chore: remove asset prefix warning log and update docs (#68681)
### Why? In #68622 we fixed a bug that HMR breaks when `assetPrefix` is set to full URL. The fix was targeted to the edge case where the user may set `localhost` to the `assetPrefix`, and technically, when the request is valid the HMR should work. However, we do not recommend this pattern as the `assetPrefix` is intended to be used for setting a CDN URL. ### How? Therefore we modify the docs to gently imply that the `assetPrefix` is for CDN URL. Also, removed the warning log that could be confusing whether we support it or not. Setting localhost will still work, but we do not mention about it within the app.
1 parent 7c10fcf commit 485497d

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

docs/02-app/02-api-reference/05-next-config-js/assetPrefix.mdx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@ description: Learn how to use the assetPrefix config option to configure your CD
2323
> suited for hosting your application on a sub-path like `/docs`.
2424
> We do not suggest you use a custom Asset Prefix for this use case.
2525
26-
To set up a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network), you can set up an asset prefix and configure your CDN's origin to resolve to the domain that Next.js is hosted on.
27-
28-
Open `next.config.js` and add the `assetPrefix` config:
26+
## Set up a CDN
2927

30-
```js filename="next.config.js"
31-
const isProd = process.env.NODE_ENV === 'production'
28+
To set up a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network), you can set up an asset prefix and configure your CDN's origin to resolve to the domain that Next.js is hosted on.
3229

33-
module.exports = {
34-
// Use the CDN in production and localhost for development.
35-
assetPrefix: isProd ? 'https://cdn.mydomain.com' : undefined,
30+
Open `next.config.mjs` and add the `assetPrefix` config based on the [phase](/docs/app/api-reference/next-config-js#async-configuration):
31+
32+
```js filename="next.config.mjs"
33+
// @ts-check
34+
import { PHASE_DEVELOPMENT_SERVER } from 'next/constants'
35+
36+
export default (phase) => {
37+
const isDev = phase === PHASE_DEVELOPMENT_SERVER
38+
/**
39+
* @type {import('next').NextConfig}
40+
*/
41+
const nextConfig = {
42+
assetPrefix: isDev ? undefined : 'https://cdn.mydomain.com',
43+
}
44+
return nextConfig
3645
}
3746
```
3847

packages/next/src/server/config.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,16 +1069,6 @@ export default async function loadConfig(
10691069
}
10701070
}
10711071

1072-
if (
1073-
phase === PHASE_DEVELOPMENT_SERVER &&
1074-
URL.canParse(userConfig.assetPrefix ?? '')
1075-
) {
1076-
curLog.warn(
1077-
`Absolute URL assetPrefix "${userConfig.assetPrefix}" may disrupt development HMR.\n` +
1078-
'See more info here https://nextjs.org/docs/app/api-reference/next-config-js/assetPrefix'
1079-
)
1080-
}
1081-
10821072
if (userConfig.target && userConfig.target !== 'server') {
10831073
throw new Error(
10841074
`The "target" property is no longer supported in ${configFileName}.\n` +

test/development/app-dir/hmr-asset-prefix-full-url/asset-prefix.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('app-dir assetPrefix full URL', () => {
2626
})
2727

2828
await retry(async () => {
29-
expect(await browser.elementByCss('p').text()).toContain('after edit')
29+
expect(await browser.elementByCss('p').text()).toBe('after edit')
3030
})
3131
})
3232
})

0 commit comments

Comments
 (0)