Skip to content

Commit fe21ff8

Browse files
committed
clarify conditions in normalized-asset-prefix helper + adjust types and returned prefix in getSocketUrl
1 parent b1f7cc6 commit fe21ff8

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ function getSocketProtocol(assetPrefix: string): string {
1111
return protocol === 'http:' ? 'ws' : 'wss'
1212
}
1313

14-
export function getSocketUrl(assetPrefix: string): string {
14+
export function getSocketUrl(assetPrefix: string | undefined): string {
1515
const { hostname, port } = window.location
16-
const protocol = getSocketProtocol(assetPrefix)
16+
const protocol = getSocketProtocol(assetPrefix || '')
1717
const prefix = normalizedAssetPrefix(assetPrefix)
1818

19-
let url = `${protocol}://${hostname}:${port}${prefix && prefix !== '' ? `/${prefix}` : ''}`
20-
21-
if (prefix.startsWith('http')) {
22-
url = `${protocol}://${prefix.split('://', 2)[1]}`
19+
// same check but on original assetPrefix value
20+
if (assetPrefix?.replace(/^\/+/, '').startsWith('http')) {
21+
return `${protocol}://${prefix}`
2322
}
2423

25-
return url
24+
return `${protocol}://${hostname}:${port}${prefix}`
2625
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
export function normalizedAssetPrefix(assetPrefix: string): string {
2-
const escapedAssetPrefix = assetPrefix.replace(/^\/+/, '')
1+
export function normalizedAssetPrefix(assetPrefix: string | undefined): string {
2+
const escapedAssetPrefix = assetPrefix?.replace(/^\/+/, '') || false
33

4-
if (escapedAssetPrefix.startsWith('http')) {
4+
// assetPrefix as a url
5+
if (escapedAssetPrefix && escapedAssetPrefix.startsWith('http')) {
56
return escapedAssetPrefix.split('://', 2)[1]
67
}
78

8-
return `${escapedAssetPrefix ? `/${escapedAssetPrefix}` : ''}`
9+
// assetPrefix is set to `undefined` or '/'
10+
if (!escapedAssetPrefix || escapedAssetPrefix === '') {
11+
return ''
12+
}
13+
14+
// assetPrefix is a common path but escaped so let's add one leading slash
15+
return `/${escapedAssetPrefix}`
916
}

0 commit comments

Comments
 (0)