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: {
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
}
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
// requests.
......
const ASSET_PREFIX = 'asset-prefix'
const ASSET_PREFIX = 'custom-asset-prefix'
module.exports = {
assetPrefix: ASSET_PREFIX,
......@@ -11,7 +11,7 @@ module.exports = {
beforeFiles: [
{
source: `/:locale/${ASSET_PREFIX}/_next/:path*`,
destination: '/_next/:path*',
destination: `/${ASSET_PREFIX}/_next/:path*`,
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(
'app-dir assetPrefix handling',
{
describe('app-dir assetPrefix handling', () => {
const { next } = nextTestSetup({
files: __dirname,
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 () => {
const $ = await next.render$('/')
expect($('#to-a-trailing-slash').attr('href')).toBe('/a')
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 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 render link', async () => {
const $ = await next.render$('/')
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 () => {
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')
expect(status).toBe(200)
})
}
)
})
})
module.exports = {
assetPrefix: '/assets',
rewrites() {
return {
beforeFiles: [{ source: '/assets/:path*', destination: '/:path*' }],
}
},
assetPrefix: '/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