diff --git a/packages/next-polyfill-module/src/index.js b/packages/next-polyfill-module/src/index.js index 803e2c51ee312..f5cd21abf4721 100644 --- a/packages/next-polyfill-module/src/index.js +++ b/packages/next-polyfill-module/src/index.js @@ -161,3 +161,23 @@ if (!Object.hasOwn) { return Object.prototype.hasOwnProperty.call(Object(object), property) } } + +/** + * Available in: + * Edge: 120 + * Firefox: 115 + * Chrome: 120 + * Safari: 17.0 + * + * https://caniuse.com/mdn-api_url_canparse_static + */ +// Modified from https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.can-parse.js +if (!('canParse' in URL)) { + URL.canParse = function (url, base) { + try { + return !!new URL(url, base) + } catch { + return false + } + } +} diff --git a/packages/next-polyfill-nomodule/package.json b/packages/next-polyfill-nomodule/package.json index f0a5f91c8f31b..9a0c671b50c8f 100644 --- a/packages/next-polyfill-nomodule/package.json +++ b/packages/next-polyfill-nomodule/package.json @@ -14,7 +14,7 @@ "prepublishOnly": "cd ../../ && turbo run build" }, "devDependencies": { - "core-js": "3.6.5", + "core-js": "3.38.1", "microbundle": "0.15.0", "object-assign": "4.1.1", "whatwg-fetch": "3.0.0" diff --git a/packages/next-polyfill-nomodule/src/index.js b/packages/next-polyfill-nomodule/src/index.js index 677deeba184e4..f7fa20a032b53 100644 --- a/packages/next-polyfill-nomodule/src/index.js +++ b/packages/next-polyfill-nomodule/src/index.js @@ -49,6 +49,7 @@ import 'core-js/features/string/trim-left' import 'core-js/features/string/trim-right' import 'core-js/features/url' import 'core-js/features/url/to-json' +import 'core-js/features/url/can-parse' import 'core-js/features/url-search-params' import 'core-js/features/weak-map' import 'core-js/features/weak-set' diff --git a/packages/next/src/client/components/react-dev-overlay/internal/helpers/get-socket-url.ts b/packages/next/src/client/components/react-dev-overlay/internal/helpers/get-socket-url.ts index a2e51cb138689..3a3b87ac28a5b 100644 --- a/packages/next/src/client/components/react-dev-overlay/internal/helpers/get-socket-url.ts +++ b/packages/next/src/client/components/react-dev-overlay/internal/helpers/get-socket-url.ts @@ -1,4 +1,3 @@ -import { canParseUrl } from '../../../../../lib/url' import { normalizedAssetPrefix } from '../../../../../shared/lib/normalized-asset-prefix' function getSocketProtocol(assetPrefix: string): string { @@ -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') diff --git a/packages/next/src/lib/url.ts b/packages/next/src/lib/url.ts index 2d301e03c6f9a..cc0b37d23c365 100644 --- a/packages/next/src/lib/url.ts +++ b/packages/next/src/lib/url.ts @@ -20,15 +20,3 @@ 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 -} diff --git a/packages/next/src/server/lib/router-server.ts b/packages/next/src/server/lib/router-server.ts index bea8e56d13e40..999a4d0805dd2 100644 --- a/packages/next/src/server/lib/router-server.ts +++ b/packages/next/src/server/lib/router-server.ts @@ -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) => @@ -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 diff --git a/packages/next/src/shared/lib/normalized-asset-prefix.ts b/packages/next/src/shared/lib/normalized-asset-prefix.ts index 34b7dbca6b44f..352e836698c5c 100644 --- a/packages/next/src/shared/lib/normalized-asset-prefix.ts +++ b/packages/next/src/shared/lib/normalized-asset-prefix.ts @@ -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 @@ -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 } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d3e372476d965..5ca11843bd779 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -857,7 +857,7 @@ importers: version: 0.5.13 babel-plugin-react-compiler: specifier: '*' - version: 0.0.0-experimental-ca8e0be-20240916 + version: 0.0.0-experimental-24ec0eb-20240918 busboy: specifier: 1.6.0 version: 1.6.0 @@ -1566,8 +1566,8 @@ importers: packages/next-polyfill-nomodule: devDependencies: core-js: - specifier: 3.6.5 - version: 3.6.5 + specifier: 3.38.1 + version: 3.38.1 microbundle: specifier: 0.15.0 version: 0.15.0(@types/babel__core@7.20.5) @@ -5934,12 +5934,12 @@ packages: peerDependencies: '@babel/core': 7.22.5 + babel-plugin-react-compiler@0.0.0-experimental-24ec0eb-20240918: + resolution: {integrity: sha512-3uFbIPB/q68AUwoPlv4nleIARMdLnzco0FL9f1Iv05kb0/wwOX8sE6rHz+6fyXEScBWf8cogTq1lnlHBc/dyNQ==} + babel-plugin-react-compiler@0.0.0-experimental-c23de8d-20240515: resolution: {integrity: sha512-0XN2gmpT55QtAz5n7d5g91y1AuO9tRhWBaLgCRyc4ExHrlr7+LfxW+YTb3mOwxngkkiggwM8HyYsaEK9MqhnlQ==} - babel-plugin-react-compiler@0.0.0-experimental-ca8e0be-20240916: - resolution: {integrity: sha512-S/fMyIedoXdIVEleWMctmllsjXIDQwGiB8Z5v12L+lGKJ7n28K/8aFzVFcDpICnIIP3tXuAo0rF1bfn0MYb7YQ==} - babel-plugin-transform-async-to-promises@0.8.15: resolution: {integrity: sha512-fDXP68ZqcinZO2WCiimCL9zhGjGXOnn3D33zvbh+yheZ/qOrNVVDDIBtAaM3Faz8TRvQzHiRKsu3hfrBAhEncQ==} @@ -6687,6 +6687,9 @@ packages: core-js-compat@3.37.1: resolution: {integrity: sha512-9TNiImhKvQqSUkOvk/mMRZzOANTiEVC7WaBNhHcKM7x+/5E1l5NvsysR19zuDQScE8k+kfQXWRN3AtS/eOSHpg==} + core-js@3.38.1: + resolution: {integrity: sha512-OP35aUorbU3Zvlx7pjsFdu1rGNnD4pgw/CWoYzRY3t2EzoVT7shKHY1dlAy3f41cGIO7ZDPQimhGFTlEYkG/Hw==} + core-js@3.6.5: resolution: {integrity: sha512-vZVEEwZoIsI+vPEuoF9Iqf5H7/M3eeQqWlQnYa8FSKKePuYTf5MWnxb5SDAzCa60b3JBRS5g9b+Dq7b1y/RCrA==} deprecated: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js. @@ -20464,7 +20467,7 @@ snapshots: - supports-color optional: true - babel-plugin-react-compiler@0.0.0-experimental-c23de8d-20240515: + babel-plugin-react-compiler@0.0.0-experimental-24ec0eb-20240918: dependencies: '@babel/generator': 7.2.0 '@babel/types': 7.22.5 @@ -20474,7 +20477,7 @@ snapshots: zod: 3.23.8 zod-validation-error: 2.1.0(zod@3.23.8) - babel-plugin-react-compiler@0.0.0-experimental-ca8e0be-20240916: + babel-plugin-react-compiler@0.0.0-experimental-c23de8d-20240515: dependencies: '@babel/generator': 7.2.0 '@babel/types': 7.22.5 @@ -21357,6 +21360,8 @@ snapshots: browserslist: 4.22.2 optional: true + core-js@3.38.1: {} + core-js@3.6.5: {} core-util-is@1.0.2: {}