Commit 62fbd7f7 authored by Jiwon Choi's avatar Jiwon Choi Committed by JJ Kasper

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

### Why?

Since `URL.canParse` is incompatible with older browsers like Safari 16,
we added `canParseUrl` util at
https://github.com/vercel/next.js/pull/70215.
x-ref: https://caniuse.com/?search=URL.canParse

To improve maintenance, we decided to add polyfill for `URL.canParse`.
x-ref: [slack
thread](https://vercel.slack.com/archives/C07BVA6HM17/p1726664071636899?thread_ts=1726620161.450109&cid=C07BVA6HM17)

Closes NDX-302

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
# Conflicts:
#	packages/next/src/lib/url.ts
#	packages/next/src/server/lib/router-server.ts
#	pnpm-lock.yaml
parent b503b8eb
...@@ -161,3 +161,23 @@ if (!Object.hasOwn) { ...@@ -161,3 +161,23 @@ if (!Object.hasOwn) {
return Object.prototype.hasOwnProperty.call(Object(object), property) 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
}
}
}
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"prepublishOnly": "cd ../../ && turbo run build" "prepublishOnly": "cd ../../ && turbo run build"
}, },
"devDependencies": { "devDependencies": {
"core-js": "3.6.5", "core-js": "3.38.1",
"microbundle": "0.15.0", "microbundle": "0.15.0",
"object-assign": "4.1.1", "object-assign": "4.1.1",
"whatwg-fetch": "3.0.0" "whatwg-fetch": "3.0.0"
......
...@@ -49,6 +49,7 @@ import 'core-js/features/string/trim-left' ...@@ -49,6 +49,7 @@ import 'core-js/features/string/trim-left'
import 'core-js/features/string/trim-right' import 'core-js/features/string/trim-right'
import 'core-js/features/url' import 'core-js/features/url'
import 'core-js/features/url/to-json' 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/url-search-params'
import 'core-js/features/weak-map' import 'core-js/features/weak-map'
import 'core-js/features/weak-set' import 'core-js/features/weak-set'
......
...@@ -1512,8 +1512,8 @@ importers: ...@@ -1512,8 +1512,8 @@ importers:
packages/next-polyfill-nomodule: packages/next-polyfill-nomodule:
devDependencies: devDependencies:
core-js: core-js:
specifier: 3.6.5 specifier: 3.38.1
version: 3.6.5 version: 3.38.1
microbundle: microbundle:
specifier: 0.15.0 specifier: 0.15.0
version: 0.15.0 version: 0.15.0
...@@ -9947,6 +9947,11 @@ packages: ...@@ -9947,6 +9947,11 @@ packages:
dependencies: dependencies:
browserslist: 4.22.2 browserslist: 4.22.2
/core-js@3.38.1:
resolution: {integrity: sha512-OP35aUorbU3Zvlx7pjsFdu1rGNnD4pgw/CWoYzRY3t2EzoVT7shKHY1dlAy3f41cGIO7ZDPQimhGFTlEYkG/Hw==}
requiresBuild: true
dev: true
/core-js@3.6.5: /core-js@3.6.5:
resolution: {integrity: sha512-vZVEEwZoIsI+vPEuoF9Iqf5H7/M3eeQqWlQnYa8FSKKePuYTf5MWnxb5SDAzCa60b3JBRS5g9b+Dq7b1y/RCrA==} 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. 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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment