Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add polyfill of URL.canParse for browser compatibility #70228

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { canParseUrl } from '../../../../../lib/url'
import { normalizedAssetPrefix } from '../../../../../shared/lib/normalized-asset-prefix'

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

if (canParseUrl(prefix)) {
if (URL.canParse(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
21 changes: 12 additions & 9 deletions packages/next/src/lib/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ 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)
// patch URL.canParse to be available in older browsers e.g. Safari 16
// x-ref: https://github.com/vercel/next.js/pull/70215
// x-ref: https://caniuse.com/?search=URL.canParse
// Since URL.canParse is available in Node.js v18.17.0 and later,
// we only need to patch it for the browser.
if (typeof window !== 'undefined' && !('canParse' in URL)) {
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
;(URL as any).canParse = (url: string, base?: string): boolean => {
try {
return !!new URL(url, base)
} catch {
return false
}
}
try {
new URL(url)
return true
} catch {}

return false
}
3 changes: 1 addition & 2 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ 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 @@ -677,7 +676,7 @@ export async function initialize(opts: {
if (assetPrefix) {
hmrPrefix = normalizedAssetPrefix(assetPrefix)

if (canParseUrl(hmrPrefix)) {
if (URL.canParse(hmrPrefix)) {
// remove trailing slash from pathname
// return empty string if pathname is '/'
// to avoid conflicts with '/_next' below
Expand Down
4 changes: 1 addition & 3 deletions packages/next/src/shared/lib/normalized-asset-prefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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 @@ -10,7 +8,7 @@ export function normalizedAssetPrefix(assetPrefix: string | undefined): string {
return ''
}

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