Skip to content

Commit 62fbd7f

Browse files
devjiwonchoihuozhi
authored andcommitted
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 #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: Jiachi Liu <[email protected]> # Conflicts: # packages/next/src/lib/url.ts # packages/next/src/server/lib/router-server.ts # pnpm-lock.yaml
1 parent b503b8e commit 62fbd7f

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

packages/next-polyfill-module/src/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,23 @@ if (!Object.hasOwn) {
161161
return Object.prototype.hasOwnProperty.call(Object(object), property)
162162
}
163163
}
164+
165+
/**
166+
* Available in:
167+
* Edge: 120
168+
* Firefox: 115
169+
* Chrome: 120
170+
* Safari: 17.0
171+
*
172+
* https://caniuse.com/mdn-api_url_canparse_static
173+
*/
174+
// Modified from https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.can-parse.js
175+
if (!('canParse' in URL)) {
176+
URL.canParse = function (url, base) {
177+
try {
178+
return !!new URL(url, base)
179+
} catch {
180+
return false
181+
}
182+
}
183+
}

packages/next-polyfill-nomodule/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"prepublishOnly": "cd ../../ && turbo run build"
1515
},
1616
"devDependencies": {
17-
"core-js": "3.6.5",
17+
"core-js": "3.38.1",
1818
"microbundle": "0.15.0",
1919
"object-assign": "4.1.1",
2020
"whatwg-fetch": "3.0.0"

packages/next-polyfill-nomodule/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import 'core-js/features/string/trim-left'
4949
import 'core-js/features/string/trim-right'
5050
import 'core-js/features/url'
5151
import 'core-js/features/url/to-json'
52+
import 'core-js/features/url/can-parse'
5253
import 'core-js/features/url-search-params'
5354
import 'core-js/features/weak-map'
5455
import 'core-js/features/weak-set'

pnpm-lock.yaml

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)