Skip to content

Commit

Permalink
fix polyfill script integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Sep 29, 2022
1 parent e43ae22 commit fe2c494
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Expand Up @@ -17,17 +17,10 @@ export class SubresourceIntegrityPlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets) => {
// Collect all the entrypoint files.
// Collect all the assets.
let files = new Set<string>()
for (const entrypoint of compilation.entrypoints.values()) {
const iterator = entrypoint?.getFiles()
if (!iterator) {
continue
}

for (const file of iterator) {
files.add(file)
}
for (const asset of compilation.getAssets()) {
files.add(asset.name)
}

// For each file, deduped, calculate the file hash.
Expand Down
5 changes: 4 additions & 1 deletion packages/next/server/app-render.tsx
Expand Up @@ -1292,7 +1292,10 @@ export async function renderToHTMLOrFlight(
(polyfill) =>
polyfill.endsWith('.js') && !polyfill.endsWith('.module.js')
)
.map((polyfill) => `${renderOpts.assetPrefix || ''}/_next/${polyfill}`)
.map((polyfill) => ({
src: `${renderOpts.assetPrefix || ''}/_next/${polyfill}`,
integrity: subresourceIntegrityManifest?.[polyfill],
}))

try {
const renderStream = await renderToInitialStream({
Expand Down
9 changes: 7 additions & 2 deletions packages/next/server/node-web-streams-helper.ts
Expand Up @@ -272,7 +272,7 @@ export async function continueFromInitialStream(
generateStaticHTML: boolean
flushEffectHandler?: () => Promise<string>
flushEffectsToHead: boolean
polyfills?: string[]
polyfills?: { src: string; integrity: string | undefined }[]
}
): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>'
Expand All @@ -295,7 +295,12 @@ export async function continueFromInitialStream(
// blocking here and can't be `defer` because other scripts have `async`.
const polyfillScripts = polyfills
? polyfills
.map((src) => `<script src="${src}" nomodule=""></script>`)
.map(
({ src, integrity }) =>
`<script src="${src}" nomodule=""${
integrity ? ` integrity="${integrity}"` : ''
}></script>`
)
.join('')
: ''

Expand Down

0 comments on commit fe2c494

Please sign in to comment.