Skip to content

Commit 0ef1037

Browse files
committed
test: fix load-custom-routes test
1 parent 020d5db commit 0ef1037

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

packages/next/src/lib/load-custom-routes.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,25 @@ describe('loadCustomRoutes', () => {
144144

145145
it('does not insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL', async () => {
146146
const customRoutes = await loadCustomRoutes({
147-
assetPrefix: 'https://example.com/custom-asset-prefix',
147+
assetPrefix: 'https://example.com',
148148
})
149149
expect(customRoutes.rewrites.beforeFiles).toEqual([])
150150
expect(customRoutes.rewrites.afterFiles).toEqual([])
151151
})
152152

153+
it('automatically insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL with a path', async () => {
154+
const customRoutes = await loadCustomRoutes({
155+
assetPrefix: 'https://example.com/custom-asset-prefix',
156+
})
157+
expect(customRoutes.rewrites.beforeFiles).toEqual([
158+
{
159+
destination: '/_next/:path+',
160+
source: '/custom-asset-prefix/_next/:path+',
161+
},
162+
])
163+
expect(customRoutes.rewrites.afterFiles).toEqual([])
164+
})
165+
153166
it('does not add rewrite when assetPrefix === basePath', async () => {
154167
const customRoutes = await loadCustomRoutes({
155168
assetPrefix: '/base',

packages/next/src/lib/load-custom-routes.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -608,14 +608,16 @@ async function loadRewrites(config: NextConfig) {
608608
prefix = new URL(config.assetPrefix).pathname
609609
}
610610

611-
const assetPrefix = prefix.startsWith('/') ? prefix : `/${prefix}`
612-
const basePath = config.basePath || ''
613-
// If these are the same, then this would result in an infinite rewrite.
614-
if (assetPrefix !== basePath) {
615-
maybeAssetPrefixRewrite.push({
616-
source: `${assetPrefix}/_next/:path+`,
617-
destination: `${basePath}/_next/:path+`,
618-
})
611+
if (prefix && prefix !== '/') {
612+
const assetPrefix = prefix.startsWith('/') ? prefix : `/${prefix}`
613+
const basePath = config.basePath || ''
614+
// If these are the same, then this would result in an infinite rewrite.
615+
if (assetPrefix !== basePath) {
616+
maybeAssetPrefixRewrite.push({
617+
source: `${assetPrefix}/_next/:path+`,
618+
destination: `${basePath}/_next/:path+`,
619+
})
620+
}
619621
}
620622
}
621623

0 commit comments

Comments
 (0)