Unverified Commit 413fbd60 authored by Maikel van Dort's avatar Maikel van Dort Committed by GitHub

fix: absolute assetPrefix url with path (#77256)

### What?

When an absolute url with path has been set as the assetPrefix, the path
is not used when serving the bundles. Instead the bundles are served
directly on the root. The rewrite did not seem to take a path into
account, only a host.

e.g. when the assetPrefix is set to
`https://example.vercel.sh/custom-asset-prefix`, it does not serve the
bundles from `<host>/custom-asset-prefix/_next/*` instead only from
`<host>/_next/*`

Related; https://github.com/vercel/next.js/discussions/77118

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent f5198b48
...@@ -144,12 +144,25 @@ describe('loadCustomRoutes', () => { ...@@ -144,12 +144,25 @@ describe('loadCustomRoutes', () => {
it('does not insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL', async () => { it('does not insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL', async () => {
const customRoutes = await loadCustomRoutes({ const customRoutes = await loadCustomRoutes({
assetPrefix: 'https://example.com/custom-asset-prefix', assetPrefix: 'https://example.com',
}) })
expect(customRoutes.rewrites.beforeFiles).toEqual([]) expect(customRoutes.rewrites.beforeFiles).toEqual([])
expect(customRoutes.rewrites.afterFiles).toEqual([]) expect(customRoutes.rewrites.afterFiles).toEqual([])
}) })
it('automatically insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL with a path', async () => {
const customRoutes = await loadCustomRoutes({
assetPrefix: 'https://example.com/custom-asset-prefix',
})
expect(customRoutes.rewrites.beforeFiles).toEqual([
{
destination: '/_next/:path+',
source: '/custom-asset-prefix/_next/:path+',
},
])
expect(customRoutes.rewrites.afterFiles).toEqual([])
})
it('does not add rewrite when assetPrefix === basePath', async () => { it('does not add rewrite when assetPrefix === basePath', async () => {
const customRoutes = await loadCustomRoutes({ const customRoutes = await loadCustomRoutes({
assetPrefix: '/base', assetPrefix: '/base',
......
...@@ -596,19 +596,28 @@ async function loadRewrites(config: NextConfig) { ...@@ -596,19 +596,28 @@ async function loadRewrites(config: NextConfig) {
// If assetPrefix is set, add a rewrite for `/${assetPrefix}/_next/*` // If assetPrefix is set, add a rewrite for `/${assetPrefix}/_next/*`
// requests so that they are handled in any of dev, start, or deploy // requests so that they are handled in any of dev, start, or deploy
// automatically without the user having to configure this. // automatically without the user having to configure this.
// If the assetPrefix is an absolute URL, we can't add an automatic rewrite. // If the assetPrefix is an absolute URL, we still consider the path for automatic rewrite.
// but hostname routing must be handled by the user
let maybeAssetPrefixRewrite: Rewrite[] = [] let maybeAssetPrefixRewrite: Rewrite[] = []
if (config.assetPrefix && !isFullStringUrl(config.assetPrefix)) { if (config.assetPrefix) {
const assetPrefix = config.assetPrefix.startsWith('/') let prefix = config.assetPrefix
? config.assetPrefix if (
: `/${config.assetPrefix}` isFullStringUrl(config.assetPrefix) &&
const basePath = config.basePath || '' URL.canParse(config.assetPrefix)
// If these are the same, then this would result in an infinite rewrite. ) {
if (assetPrefix !== basePath) { prefix = new URL(config.assetPrefix).pathname
maybeAssetPrefixRewrite.push({ }
source: `${assetPrefix}/_next/:path+`,
destination: `${basePath}/_next/:path+`, if (prefix && prefix !== '/') {
}) const assetPrefix = prefix.startsWith('/') ? prefix : `/${prefix}`
const basePath = config.basePath || ''
// If these are the same, then this would result in an infinite rewrite.
if (assetPrefix !== basePath) {
maybeAssetPrefixRewrite.push({
source: `${assetPrefix}/_next/:path+`,
destination: `${basePath}/_next/:path+`,
})
}
} }
} }
......
export default function Root({ children }) {
return (
<html>
<head>
<title>Hello</title>
</head>
<body>{children}</body>
</html>
)
}
export default function HomePage() {
return <p>Hello world</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('app-dir absolute assetPrefix', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('bundles should return 200 on served assetPrefix', async () => {
const $ = await next.render$('/')
let bundles = []
for (const script of $('script').toArray()) {
const { src } = script.attribs
if (
src?.includes(
'https://example.vercel.sh/custom-asset-prefix/_next/static'
)
) {
bundles.push(src)
}
}
expect(bundles.length).toBeGreaterThan(0)
for (const src of bundles) {
// Remove hostname to check if pathname is still used for serving the bundles
const bundlePathWithoutHost = decodeURI(new URL(src).pathname)
const { status } = await next.fetch(bundlePathWithoutHost)
expect(status).toBe(200)
}
})
})
module.exports = {
assetPrefix: 'https://example.vercel.sh/custom-asset-prefix',
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment