Skip to content

Commit

Permalink
refactor: use URL obj
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi committed Aug 5, 2024
1 parent 654623c commit 409d532
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ function getSocketProtocol(assetPrefix: string): string {
export function getSocketUrl(assetPrefix: string | undefined): string {
const { hostname, port } = window.location
const protocol = getSocketProtocol(assetPrefix || '')
// if original assetPrefix is a URL with protocol
// the prefix should be normalized to pathname
const prefix = normalizedAssetPrefix(assetPrefix)

// if original assetPrefix is a full URL with protocol
// we just update to use the correct `ws` protocol
if (assetPrefix?.startsWith('http')) {
return `${protocol}://${prefix}`
}

return `${protocol}://${hostname}:${port}${prefix}`
}
11 changes: 5 additions & 6 deletions packages/next/src/shared/lib/normalized-asset-prefix.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export function normalizedAssetPrefix(assetPrefix: string | undefined): string {
const escapedAssetPrefix = assetPrefix?.replace(/^\/+/, '') || false

if (escapedAssetPrefix && escapedAssetPrefix.startsWith('http')) {
// remove protocol for socket url
// https://example.com/path/to/asset -> example.com/path/to/asset
return escapedAssetPrefix.split('://', 2)[1]
// if assetPrefix is a URL, we return the pathname
if (assetPrefix?.startsWith('http')) {
return new URL(assetPrefix).pathname
}

const escapedAssetPrefix = assetPrefix?.replace(/^\/+/, '') || false

// assetPrefix is set to `undefined` or '/'
if (!escapedAssetPrefix) {
return ''
Expand Down

0 comments on commit 409d532

Please sign in to comment.