Skip to content

Commit 6714d2a

Browse files
mattcompilesgregberge
authored andcommitted
fix: Add extra props option for links (#212)
1 parent 69dbb8b commit 6714d2a

File tree

2 files changed

+95
-13
lines changed

2 files changed

+95
-13
lines changed

packages/server/src/ChunkExtractor.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ function getAssets(chunks, getAsset) {
2121
}
2222

2323
function extraPropsToString(extraProps) {
24-
return Object.keys(extraProps).reduce((acc, key) => `${acc} ${key}="${extraProps[key]}"`, '');
24+
return Object.keys(extraProps).reduce(
25+
(acc, key) => `${acc} ${key}="${extraProps[key]}"`,
26+
'',
27+
)
2528
}
2629

2730
function assetToScriptTag(asset, extraProps) {
@@ -32,7 +35,13 @@ function assetToScriptTag(asset, extraProps) {
3235

3336
function assetToScriptElement(asset, extraProps) {
3437
return (
35-
<script key={asset.url} async data-chunk={asset.chunk} src={asset.url} {...extraProps} />
38+
<script
39+
key={asset.url}
40+
async
41+
data-chunk={asset.chunk}
42+
src={asset.url}
43+
{...extraProps}
44+
/>
3645
)
3746
}
3847

@@ -62,7 +71,9 @@ function assetToStyleTagInline(asset, extraProps) {
6271
return
6372
}
6473
resolve(
65-
`<style type="text/css" data-chunk="${asset.chunk}"${extraPropsToString(extraProps)}>
74+
`<style type="text/css" data-chunk="${asset.chunk}"${extraPropsToString(
75+
extraProps,
76+
)}>
6677
${data}
6778
</style>`,
6879
)
@@ -106,21 +117,22 @@ const LINK_ASSET_HINTS = {
106117
childAsset: 'data-parent-chunk',
107118
}
108119

109-
function assetToLinkTag(asset) {
120+
function assetToLinkTag(asset, extraProps) {
110121
const hint = LINK_ASSET_HINTS[asset.type]
111122
return `<link ${hint}="${asset.chunk}" rel="${asset.linkType}" as="${
112123
asset.scriptType
113-
}" href="${asset.url}">`
124+
}" href="${asset.url}"${extraPropsToString(extraProps)}>`
114125
}
115126

116-
function assetToLinkElement(asset) {
127+
function assetToLinkElement(asset, extraProps) {
117128
const hint = LINK_ASSET_HINTS[asset.type]
118129
const props = {
119130
key: asset.url,
120131
[hint]: asset.chunk,
121132
rel: asset.linkType,
122133
as: asset.scriptType,
123134
href: asset.url,
135+
...extraProps,
124136
}
125137
return <link {...props} />
126138
}
@@ -232,7 +244,9 @@ class ChunkExtractor {
232244
}
233245

234246
getRequiredChunksScriptTag(extraProps) {
235-
return `<script${extraPropsToString(extraProps)}>${this.getRequiredChunksScriptContent()}</script>`
247+
return `<script${extraPropsToString(
248+
extraProps,
249+
)}>${this.getRequiredChunksScriptContent()}</script>`
236250
}
237251

238252
getRequiredChunksScriptElement(extraProps) {
@@ -285,12 +299,16 @@ class ChunkExtractor {
285299
getScriptTags(extraProps = {}) {
286300
const requiredScriptTag = this.getRequiredChunksScriptTag(extraProps)
287301
const mainAssets = this.getMainAssets('script')
288-
const assetsScriptTags = mainAssets.map(asset => assetToScriptTag(asset, extraProps))
302+
const assetsScriptTags = mainAssets.map(asset =>
303+
assetToScriptTag(asset, extraProps),
304+
)
289305
return joinTags([requiredScriptTag, ...assetsScriptTags])
290306
}
291307

292308
getScriptElements(extraProps = {}) {
293-
const requiredScriptElement = this.getRequiredChunksScriptElement(extraProps)
309+
const requiredScriptElement = this.getRequiredChunksScriptElement(
310+
extraProps,
311+
)
294312
const mainAssets = this.getMainAssets('script')
295313
const assetsScriptElements = mainAssets.map(asset =>
296314
assetToScriptElement(asset, extraProps),
@@ -342,15 +360,15 @@ class ChunkExtractor {
342360
return [...mainAssets, ...preloadAssets, ...prefetchAssets]
343361
}
344362

345-
getLinkTags() {
363+
getLinkTags(extraProps = {}) {
346364
const assets = this.getPreAssets()
347-
const linkTags = assets.map(asset => assetToLinkTag(asset))
365+
const linkTags = assets.map(asset => assetToLinkTag(asset, extraProps))
348366
return joinTags(linkTags)
349367
}
350368

351-
getLinkElements() {
369+
getLinkElements(extraProps = {}) {
352370
const assets = this.getPreAssets()
353-
return assets.map(asset => assetToLinkElement(asset))
371+
return assets.map(asset => assetToLinkElement(asset, extraProps))
354372
}
355373
}
356374

packages/server/src/ChunkExtractor.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ h1 {
334334
<link data-chunk=\\"main\\" rel=\\"preload\\" as=\\"script\\" href=\\"/dist/node/main.js\\">
335335
<link data-parent-chunk=\\"main\\" rel=\\"preload\\" as=\\"script\\" href=\\"/dist/node/letters-C.js\\">
336336
<link data-parent-chunk=\\"main\\" rel=\\"prefetch\\" as=\\"script\\" href=\\"/dist/node/letters-D.js\\">"
337+
`)
338+
})
339+
340+
it('should add extraProps if specified', () => {
341+
extractor.addChunk('letters-A')
342+
expect(extractor.getLinkTags({ nonce: 'testnonce' }))
343+
.toMatchInlineSnapshot(`
344+
"<link data-chunk=\\"letters-A\\" rel=\\"preload\\" as=\\"style\\" href=\\"/dist/node/letters-A.css\\" nonce=\\"testnonce\\">
345+
<link data-chunk=\\"letters-A\\" rel=\\"preload\\" as=\\"script\\" href=\\"/dist/node/letters-A.js\\" nonce=\\"testnonce\\">
346+
<link data-chunk=\\"main\\" rel=\\"preload\\" as=\\"style\\" href=\\"/dist/node/main.css\\" nonce=\\"testnonce\\">
347+
<link data-chunk=\\"main\\" rel=\\"preload\\" as=\\"script\\" href=\\"/dist/node/main.js\\" nonce=\\"testnonce\\">
348+
<link data-parent-chunk=\\"main\\" rel=\\"preload\\" as=\\"script\\" href=\\"/dist/node/letters-C.js\\" nonce=\\"testnonce\\">
349+
<link data-parent-chunk=\\"main\\" rel=\\"prefetch\\" as=\\"script\\" href=\\"/dist/node/letters-D.js\\" nonce=\\"testnonce\\">"
337350
`)
338351
})
339352
})
@@ -411,6 +424,57 @@ Array [
411424
rel="prefetch"
412425
/>,
413426
]
427+
`)
428+
})
429+
430+
it('should add extraProps if specified', () => {
431+
extractor.addChunk('letters-A')
432+
expect(extractor.getLinkElements({ nonce: 'testnonce' }))
433+
.toMatchInlineSnapshot(`
434+
Array [
435+
<link
436+
as="style"
437+
data-chunk="letters-A"
438+
href="/dist/node/letters-A.css"
439+
nonce="testnonce"
440+
rel="preload"
441+
/>,
442+
<link
443+
as="script"
444+
data-chunk="letters-A"
445+
href="/dist/node/letters-A.js"
446+
nonce="testnonce"
447+
rel="preload"
448+
/>,
449+
<link
450+
as="style"
451+
data-chunk="main"
452+
href="/dist/node/main.css"
453+
nonce="testnonce"
454+
rel="preload"
455+
/>,
456+
<link
457+
as="script"
458+
data-chunk="main"
459+
href="/dist/node/main.js"
460+
nonce="testnonce"
461+
rel="preload"
462+
/>,
463+
<link
464+
as="script"
465+
data-parent-chunk="main"
466+
href="/dist/node/letters-C.js"
467+
nonce="testnonce"
468+
rel="preload"
469+
/>,
470+
<link
471+
as="script"
472+
data-parent-chunk="main"
473+
href="/dist/node/letters-D.js"
474+
nonce="testnonce"
475+
rel="prefetch"
476+
/>,
477+
]
414478
`)
415479
})
416480
})

0 commit comments

Comments
 (0)