* Appends module preload hints to an HTML string for specified JavaScript resources. * This function enhances the HTML by injecting ` ` elements * for each provided resource, allowing browsers to preload the specified JavaScript * modules for better performance. * * @pa
(html: string, preload: readonly string[])
| 534 | * If `</body>` is not found, the links are not added. |
| 535 | */ |
| 536 | function appendPreloadHintsToHtml(html: string, preload: readonly string[]): string { |
| 537 | const bodyCloseIdx = html.lastIndexOf('</body>'); |
| 538 | if (bodyCloseIdx === -1) { |
| 539 | return html; |
| 540 | } |
| 541 | |
| 542 | // Note: Module preloads should be placed at the end before the closing body tag to avoid a performance penalty. |
| 543 | // Placing them earlier can cause the browser to prioritize downloading these modules |
| 544 | // over other critical page resources like images, CSS, and fonts. |
| 545 | return [ |
| 546 | html.slice(0, bodyCloseIdx), |
| 547 | ...preload.map((val) => `<link rel="modulepreload" href="${val}">`), |
| 548 | html.slice(bodyCloseIdx), |
| 549 | ].join('\n'); |
| 550 | } |