Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4db0a4

Browse files
Peter Bengtssonheiskr
andauthored
Stop polyfilling structuredclone (#41823)
Co-authored-by: Kevin Heis <[email protected]>
1 parent a030a66 commit d4db0a4

File tree

2 files changed

+2
-38
lines changed

2 files changed

+2
-38
lines changed

src/content-render/unified/rewrite-asset-img-tags.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
import { visit, SKIP } from 'unist-util-visit'
22

3-
/**
4-
* `structuredClone` was added in Node 17 and onwards.
5-
* https://developer.mozilla.org/en-US/docs/Web/API/structuredClone#browser_compatibility
6-
*
7-
* At the time of writing, we use Node 18 in CI and in production, but
8-
* someone might be previewing locally with an older version so
9-
* let's make a quick polyfill.
10-
* We could add a specific (`js-core`) package for this polyfill, but it's
11-
* fortunately not necessary in this context because it's safe enough
12-
* clone by turning into a string and back again.
13-
*/
14-
function structuredClonePolyfill(obj) {
15-
if (typeof structuredClone !== 'undefined') {
16-
return structuredClone(obj)
17-
} else {
18-
// Note, that this naive clone would turn Date objects into strings.
19-
// So don't use this polyfill if certain values aren't primitives
20-
// that JSON.parse can handle.
21-
return JSON.parse(JSON.stringify(obj))
22-
}
23-
}
24-
253
// This number must match a width we're willing to accept in a dynamic
264
// asset URL.
275
// (note this is exported for the sake of end-to-end tests' assertions)
@@ -52,7 +30,7 @@ export default function rewriteAssetImgTags() {
5230
return (tree) => {
5331
visit(tree, matcher, (node) => {
5432
if (node.properties.src.endsWith('.png')) {
55-
const copyPNG = structuredClonePolyfill(node)
33+
const copyPNG = structuredClone(node)
5634

5735
const sourceWEBP = {
5836
type: 'element',

src/links/lib/update-internal-links.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ import getRedirect, { splitPathByLanguage } from '#src/redirects/lib/get-redirec
2020
import nonEnterpriseDefaultVersion from '#src/versions/lib/non-enterprise-default-version.js'
2121
import { deprecated } from '#src/versions/lib/enterprise-server-releases.js'
2222

23-
function objectClone(obj) {
24-
try {
25-
return structuredClone(obj)
26-
} catch {
27-
// Need to polyfill for Node 16 folks
28-
// Using `yaml.load(yaml.dump(...))` is safe enough because this
29-
// data itself came from the Yaml deserializing in frontmatter().
30-
return yaml.load(yaml.dump(obj))
31-
}
32-
}
33-
3423
// That magical string that can be turned into th actual title when
3524
// we, at runtime, render out the links
3625
const AUTOTITLE = 'AUTOTITLE'
@@ -94,10 +83,7 @@ async function updateFile(file, context, opts) {
9483
const replacements = []
9584
const warnings = []
9685

97-
// The day we know with confidence that everyone us on Node >=17,
98-
// we can change this to use `structuredClone` without the polyfill
99-
// technique.
100-
const newData = objectClone(data)
86+
const newData = structuredClone(data)
10187

10288
const ANY = Symbol('any')
10389
const IS_ARRAY = Symbol('is array')

0 commit comments

Comments
 (0)