Skip to content

Commit a5e2a02

Browse files
authored
fix: normalize-asset-prefix adding leading slash when URL assetPrefix is provided (#68518)
### Why? URL validation of `normalizeAssetPrefix` was wrong as it checks if is starting with `://`: https://github.com/vercel/next.js/blob/37df9ab243690187725a236cbf2debbcaeb33cfa/packages/next/src/shared/lib/normalized-asset-prefix.ts#L4-L7 This resulted the value to be `/https://example.com` instead of `example.com`. x-ref: #68518 (comment) ### How? As `normalizeAssetPrefix` function was added for `getSocketUrl`, it removes the protocol of the URL. But for reusability, this could be a friction to other callers. Therefore we validate the URL and let the callers handle the URL (e.g. protocol).
1 parent f17f570 commit a5e2a02

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

packages/next/src/client/components/react-dev-overlay/internal/helpers/get-socket-url.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ function getSocketProtocol(assetPrefix: string): string {
88
protocol = new URL(assetPrefix).protocol
99
} catch {}
1010

11-
return protocol === 'http:' ? 'ws' : 'wss'
11+
return protocol === 'http:' ? 'ws:' : 'wss:'
1212
}
1313

1414
export function getSocketUrl(assetPrefix: string | undefined): string {
15-
const { hostname, port } = window.location
16-
const protocol = getSocketProtocol(assetPrefix || '')
1715
const prefix = normalizedAssetPrefix(assetPrefix)
16+
const protocol = getSocketProtocol(assetPrefix || '')
1817

19-
// if original assetPrefix is a full URL with protocol
20-
// we just update to use the correct `ws` protocol
21-
if (assetPrefix?.replace(/^\/+/, '').includes('://')) {
22-
return `${protocol}://${prefix}`
18+
if (URL.canParse(prefix)) {
19+
// since normalized asset prefix is ensured to be a URL format,
20+
// we can safely replace the protocol
21+
return prefix.replace(/^http/, 'ws')
2322
}
2423

25-
return `${protocol}://${hostname}:${port}${prefix}`
24+
const { hostname, port } = window.location
25+
return `${protocol}//${hostname}${port ? `:${port}` : ''}${prefix}`
2626
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { normalizedAssetPrefix } from './normalized-asset-prefix'
2+
3+
describe('normalizedAssetPrefix', () => {
4+
it('should return an empty string when assetPrefix is nullish', () => {
5+
expect(normalizedAssetPrefix(undefined)).toBe('')
6+
})
7+
8+
it('should return an empty string when assetPrefix is an empty string', () => {
9+
expect(normalizedAssetPrefix('')).toBe('')
10+
})
11+
12+
it('should return an empty string when assetPrefix is a single slash', () => {
13+
expect(normalizedAssetPrefix('/')).toBe('')
14+
})
15+
16+
// we expect an empty string because it could be an unnecessary trailing slash
17+
it('should remove leading slash(es) when assetPrefix has more than one', () => {
18+
expect(normalizedAssetPrefix('///path/to/asset')).toBe('/path/to/asset')
19+
})
20+
21+
it('should not remove the leading slash when assetPrefix has only one', () => {
22+
expect(normalizedAssetPrefix('/path/to/asset')).toBe('/path/to/asset')
23+
})
24+
25+
it('should add a leading slash when assetPrefix is missing one', () => {
26+
expect(normalizedAssetPrefix('path/to/asset')).toBe('/path/to/asset')
27+
})
28+
29+
it('should remove all trailing slash(es) when assetPrefix has one', () => {
30+
expect(normalizedAssetPrefix('/path/to/asset///')).toBe('/path/to/asset')
31+
})
32+
33+
it('should return the URL when assetPrefix is a URL', () => {
34+
expect(normalizedAssetPrefix('https://example.com/path/to/asset')).toBe(
35+
'https://example.com/path/to/asset'
36+
)
37+
})
38+
39+
it('should not leave a trailing slash when assetPrefix is a URL with no pathname', () => {
40+
expect(normalizedAssetPrefix('https://example.com')).toBe(
41+
'https://example.com'
42+
)
43+
})
44+
})
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
export function normalizedAssetPrefix(assetPrefix: string | undefined): string {
2-
const escapedAssetPrefix = assetPrefix?.replace(/^\/+/, '') || false
2+
// remove all leading slashes and trailing slashes
3+
const escapedAssetPrefix = assetPrefix?.replace(/^\/+|\/+$/g, '') || false
34

4-
// assetPrefix as a url
5-
if (escapedAssetPrefix && escapedAssetPrefix.startsWith('://')) {
6-
return escapedAssetPrefix.split('://', 2)[1]
7-
}
8-
9-
// assetPrefix is set to `undefined` or '/'
5+
// if an assetPrefix was '/', we return empty string
6+
// because it could be an unnecessary trailing slash
107
if (!escapedAssetPrefix) {
118
return ''
129
}
1310

14-
// assetPrefix is a common path but escaped so let's add one leading slash
11+
if (URL.canParse(escapedAssetPrefix)) {
12+
const url = new URL(escapedAssetPrefix).toString()
13+
return url.endsWith('/') ? url.slice(0, -1) : url
14+
}
15+
16+
// assuming assetPrefix here is a pathname-style,
17+
// restore the leading slash
1518
return `/${escapedAssetPrefix}`
1619
}

0 commit comments

Comments
 (0)