Skip to content

Commit

Permalink
Replace URL.canParse
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 18, 2024
1 parent 5791706 commit 66f4ba0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { canParseUrl } from '../../../../../lib/url'
import { normalizedAssetPrefix } from '../../../../../shared/lib/normalized-asset-prefix'

function getSocketProtocol(assetPrefix: string): string {
Expand All @@ -15,7 +16,7 @@ export function getSocketUrl(assetPrefix: string | undefined): string {
const prefix = normalizedAssetPrefix(assetPrefix)
const protocol = getSocketProtocol(assetPrefix || '')

if (URL.canParse(prefix)) {
if (canParseUrl(prefix)) {
// since normalized asset prefix is ensured to be a URL format,
// we can safely replace the protocol
return prefix.replace(/^http/, 'ws')
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/lib/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ export function stripNextRscUnionQuery(relativeUrl: string): string {

return urlInstance.pathname + urlInstance.search
}

export function canParseUrl(url: string): boolean {
if (typeof URL.canParse === 'function') {
return URL.canParse(url)
}
try {
new URL(url)
return true
} catch {}

return false
}
3 changes: 2 additions & 1 deletion packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
} from '../dev/hot-reloader-types'
import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix'
import { NEXT_PATCH_SYMBOL } from './patch-fetch'
import { canParseUrl } from '../../lib/url'

const debug = setupDebug('next:router-server:main')
const isNextFont = (pathname: string | null) =>
Expand Down Expand Up @@ -676,7 +677,7 @@ export async function initialize(opts: {
if (assetPrefix) {
hmrPrefix = normalizedAssetPrefix(assetPrefix)

if (URL.canParse(hmrPrefix)) {
if (canParseUrl(hmrPrefix)) {
// remove trailing slash from pathname
// return empty string if pathname is '/'
// to avoid conflicts with '/_next' below
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/shared/lib/normalized-asset-prefix.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { canParseUrl } from '../../lib/url'

export function normalizedAssetPrefix(assetPrefix: string | undefined): string {
// remove all leading slashes and trailing slashes
const escapedAssetPrefix = assetPrefix?.replace(/^\/+|\/+$/g, '') || false
Expand All @@ -8,7 +10,7 @@ export function normalizedAssetPrefix(assetPrefix: string | undefined): string {
return ''
}

if (URL.canParse(escapedAssetPrefix)) {
if (canParseUrl(escapedAssetPrefix)) {
const url = new URL(escapedAssetPrefix).toString()
return url.endsWith('/') ? url.slice(0, -1) : url
}
Expand Down

0 comments on commit 66f4ba0

Please sign in to comment.