Unverified Commit 4efe1423 authored by Maikel's avatar Maikel Committed by GitHub

fix: bundle fetching with asset prefix (#63627)

Closes: https://github.com/vercel/next.js/issues/63623

When a relative assetPrefix was set (e.g. `/custom-asset-prefix`),
bundle fetching would always return a 404 as the assetPrefix was not
removed from filesystem path

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 5cfb7a08
...@@ -421,12 +421,22 @@ export async function setupFsCheck(opts: { ...@@ -421,12 +421,22 @@ export async function setupFsCheck(opts: {
return lruResult return lruResult
} }
const { basePath } = opts.config const { basePath, assetPrefix } = opts.config
if (basePath && !pathHasPrefix(itemPath, basePath)) { const hasBasePath = pathHasPrefix(itemPath, basePath)
const hasAssetPrefix = pathHasPrefix(itemPath, assetPrefix)
// Return null if either path doesn't start with basePath or assetPrefix
if ((basePath || assetPrefix) && !hasBasePath && !hasAssetPrefix) {
return null return null
} }
itemPath = removePathPrefix(itemPath, basePath) || '/'
// Either remove basePath or assetPrefix, not both (due to routes with same name as basePath)
if (basePath && hasBasePath) {
itemPath = removePathPrefix(itemPath, basePath) || '/'
} else if (assetPrefix && hasAssetPrefix) {
itemPath = removePathPrefix(itemPath, assetPrefix) || '/'
}
// Simulate minimal mode requests by normalizing RSC and postponed // Simulate minimal mode requests by normalizing RSC and postponed
// requests. // requests.
......
const ASSET_PREFIX = 'asset-prefix' const ASSET_PREFIX = 'custom-asset-prefix'
module.exports = { module.exports = {
assetPrefix: ASSET_PREFIX, assetPrefix: ASSET_PREFIX,
...@@ -11,7 +11,7 @@ module.exports = { ...@@ -11,7 +11,7 @@ module.exports = {
beforeFiles: [ beforeFiles: [
{ {
source: `/:locale/${ASSET_PREFIX}/_next/:path*`, source: `/:locale/${ASSET_PREFIX}/_next/:path*`,
destination: '/_next/:path*', destination: `/${ASSET_PREFIX}/_next/:path*`,
locale: false, locale: false,
}, },
], ],
......
import Link from 'next/link'
export default function HomePage() {
return (
<>
<h1 id="a-page">A page</h1>
<Link href="/">To home</Link>
</>
)
}
export default function Root({ children }) {
return (
<html>
<head>
<title>Hello</title>
</head>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function HomePage() {
return (
<>
<p>
<Link href="/a/" id="to-a-trailing-slash">
To a with trailing slash
</Link>
</p>
</>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('app-dir assetPrefix with basePath handling', () => {
const { next } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
it('should redirect route when requesting it directly', async () => {
const res = await next.fetch('/custom-base-path/a/', {
redirect: 'manual',
})
expect(res.status).toBe(308)
expect(new URL(res.headers.get('location'), next.url).pathname).toBe(
'/custom-base-path/a'
)
})
it('should render link', async () => {
const $ = await next.render$('/custom-base-path')
expect($('#to-a-trailing-slash').attr('href')).toBe('/custom-base-path/a')
})
it('should redirect route when requesting it directly by browser', async () => {
const browser = await next.browser('/custom-base-path/a')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
it('should redirect route when clicking link', async () => {
const browser = await next.browser('/custom-base-path')
await browser
.elementByCss('#to-a-trailing-slash')
.click()
.waitForElementByCss('#a-page')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
it('bundles should return 200 on served assetPrefix', async () => {
const $ = await next.render$('/custom-base-path')
let bundles = []
for (const script of $('script').toArray()) {
const { src } = script.attribs
if (src?.includes('/custom-asset-prefix/_next/static')) {
bundles.push(src)
}
}
expect(bundles.length).toBeGreaterThan(0)
bundles.forEach(async (src) => {
const { status } = await next.fetch(decodeURI(src))
expect(status).toBe(200)
})
})
})
module.exports = {
assetPrefix: '/custom-asset-prefix',
basePath: '/custom-base-path',
}
import { createNextDescribe } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
createNextDescribe( describe('app-dir assetPrefix handling', () => {
'app-dir assetPrefix handling', const { next } = nextTestSetup({
{
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
}, })
({ next }) => {
it('should redirect route when requesting it directly', async () => {
const res = await next.fetch('/a/', {
redirect: 'manual',
})
expect(res.status).toBe(308)
expect(new URL(res.headers.get('location'), next.url).pathname).toBe('/a')
})
it('should render link', async () => { it('should redirect route when requesting it directly', async () => {
const $ = await next.render$('/') const res = await next.fetch('/a/', {
expect($('#to-a-trailing-slash').attr('href')).toBe('/a') redirect: 'manual',
}) })
expect(res.status).toBe(308)
expect(new URL(res.headers.get('location'), next.url).pathname).toBe('/a')
})
it('should redirect route when requesting it directly by browser', async () => { it('should render link', async () => {
const browser = await next.browser('/a') const $ = await next.render$('/')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page') expect($('#to-a-trailing-slash').attr('href')).toBe('/a')
}) })
it('should redirect route when requesting it directly by browser', async () => {
const browser = await next.browser('/a')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
it('should redirect route when clicking link', async () => {
const browser = await next.browser('/')
await browser
.elementByCss('#to-a-trailing-slash')
.click()
.waitForElementByCss('#a-page')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
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('/custom-asset-prefix/_next/static')) {
bundles.push(src)
}
}
expect(bundles.length).toBeGreaterThan(0)
bundles.forEach(async (src) => {
const { status } = await next.fetch(decodeURI(src))
it('should redirect route when clicking link', async () => { expect(status).toBe(200)
const browser = await next.browser('/')
await browser
.elementByCss('#to-a-trailing-slash')
.click()
.waitForElementByCss('#a-page')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
}) })
} })
) })
module.exports = { module.exports = {
assetPrefix: '/assets', assetPrefix: '/custom-asset-prefix',
rewrites() {
return {
beforeFiles: [{ source: '/assets/:path*', destination: '/:path*' }],
}
},
} }
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