-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: setting
assetPrefix
to URL format breaks HMR (#70040)
- Loading branch information
1 parent
9954a21
commit 276ddf3
Showing
8 changed files
with
130 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/next/src/shared/lib/normalized-asset-prefix.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { normalizedAssetPrefix } from './normalized-asset-prefix' | ||
|
||
describe('normalizedAssetPrefix', () => { | ||
it('should return an empty string when assetPrefix is nullish', () => { | ||
expect(normalizedAssetPrefix(undefined)).toBe('') | ||
}) | ||
|
||
it('should return an empty string when assetPrefix is an empty string', () => { | ||
expect(normalizedAssetPrefix('')).toBe('') | ||
}) | ||
|
||
it('should return an empty string when assetPrefix is a single slash', () => { | ||
expect(normalizedAssetPrefix('/')).toBe('') | ||
}) | ||
|
||
// we expect an empty string because it could be an unnecessary trailing slash | ||
it('should remove leading slash(es) when assetPrefix has more than one', () => { | ||
expect(normalizedAssetPrefix('///path/to/asset')).toBe('/path/to/asset') | ||
}) | ||
|
||
it('should not remove the leading slash when assetPrefix has only one', () => { | ||
expect(normalizedAssetPrefix('/path/to/asset')).toBe('/path/to/asset') | ||
}) | ||
|
||
it('should add a leading slash when assetPrefix is missing one', () => { | ||
expect(normalizedAssetPrefix('path/to/asset')).toBe('/path/to/asset') | ||
}) | ||
|
||
it('should remove all trailing slash(es) when assetPrefix has one', () => { | ||
expect(normalizedAssetPrefix('/path/to/asset///')).toBe('/path/to/asset') | ||
}) | ||
|
||
it('should return the URL when assetPrefix is a URL', () => { | ||
expect(normalizedAssetPrefix('https://example.com/path/to/asset')).toBe( | ||
'https://example.com/path/to/asset' | ||
) | ||
}) | ||
|
||
it('should not leave a trailing slash when assetPrefix is a URL with no pathname', () => { | ||
expect(normalizedAssetPrefix('https://example.com')).toBe( | ||
'https://example.com' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
export function normalizedAssetPrefix(assetPrefix: string | undefined): string { | ||
const escapedAssetPrefix = assetPrefix?.replace(/^\/+/, '') || false | ||
// remove all leading slashes and trailing slashes | ||
const escapedAssetPrefix = assetPrefix?.replace(/^\/+|\/+$/g, '') || false | ||
|
||
// assetPrefix as a url | ||
if (escapedAssetPrefix && escapedAssetPrefix.startsWith('://')) { | ||
return escapedAssetPrefix.split('://', 2)[1] | ||
} | ||
|
||
// assetPrefix is set to `undefined` or '/' | ||
// if an assetPrefix was '/', we return empty string | ||
// because it could be an unnecessary trailing slash | ||
if (!escapedAssetPrefix) { | ||
return '' | ||
} | ||
|
||
// assetPrefix is a common path but escaped so let's add one leading slash | ||
if (URL.canParse(escapedAssetPrefix)) { | ||
const url = new URL(escapedAssetPrefix).toString() | ||
return url.endsWith('/') ? url.slice(0, -1) : url | ||
} | ||
|
||
// assuming assetPrefix here is a pathname-style, | ||
// restore the leading slash | ||
return `/${escapedAssetPrefix}` | ||
} |
7 changes: 7 additions & 0 deletions
7
test/development/app-dir/hmr-asset-prefix-full-url/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Root({ children }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/development/app-dir/hmr-asset-prefix-full-url/app/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <p>before edit</p> | ||
} |
32 changes: 32 additions & 0 deletions
32
test/development/app-dir/hmr-asset-prefix-full-url/asset-prefix.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { findPort, retry } from 'next-test-utils' | ||
|
||
describe('app-dir assetPrefix full URL', () => { | ||
let next, forcedPort | ||
beforeAll(async () => { | ||
forcedPort = ((await findPort()) ?? '54321').toString() | ||
|
||
next = await createNext({ | ||
files: __dirname, | ||
forcedPort, | ||
nextConfig: { | ||
assetPrefix: `http://localhost:${forcedPort}`, | ||
}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('should not break HMR when asset prefix set to full URL', async () => { | ||
const browser = await next.browser('/') | ||
const text = await browser.elementByCss('p').text() | ||
expect(text).toBe('before edit') | ||
|
||
await next.patchFile('app/page.tsx', (content) => { | ||
return content.replace('before', 'after') | ||
}) | ||
|
||
await retry(async () => { | ||
expect(await browser.elementByCss('p').text()).toBe('after edit') | ||
}) | ||
}) | ||
}) |