Skip to content

Commit

Permalink
perf: defer application main JS chunks (#3619)
Browse files Browse the repository at this point in the history
* perf: defer main application JS chunks

* docs: changeset

---------

Co-authored-by: Nicola Molinari <nicola.molinari@commercetools.com>
  • Loading branch information
ragafus and emmenko authored Oct 8, 2024
1 parent e27a447 commit 5d55a50
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/serious-trainers-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@commercetools-frontend/mc-html-template": patch
"@commercetools-frontend/mc-scripts": patch
---

Use `defer` when loading main application JavaScript chunks.
6 changes: 3 additions & 3 deletions packages/mc-html-template/html-docs/application.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
href="__CDN_URL__favicon_144x144px.png"
/>

<!-- Main application chunks -->
__APPLICATION_SCRIPT_IMPORTS__

<!-- Fonts -->
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Open+Sans:ital,wght@0,300;0,400;0,700;1,400;1,700&display=swap"
Expand Down Expand Up @@ -167,8 +170,5 @@

<!-- Application globals -->
__APPLICATION_ENVIRONMENT__

<!-- Main application chunks -->
__APPLICATION_SCRIPT_IMPORTS__
</body>
</html>
2 changes: 1 addition & 1 deletion packages/mc-html-template/src/webpack-html-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function webpackHtmlTemplate(templateParams: TemplateParameter) {
`<link href="__CDN_URL__${chunkPath}" rel='stylesheet' type='text/css'>`
);
const scriptImports = scriptChunks.map(
(chunkPath) => `<script src="__CDN_URL__${chunkPath}"></script>`
(chunkPath) => `<script src="__CDN_URL__${chunkPath}" defer></script>`
);

return generateTemplate({ cssImports, scriptImports });
Expand Down
4 changes: 3 additions & 1 deletion packages/mc-scripts/src/commands/build-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ async function run() {
const html = generateTemplate({
// Define the module entry point (path relative from the `/public` folder).
// NOTE: that this is different from the development configuration.
scriptImports: [`<script type="module" src="/${appEntryPoint}"></script>`],
scriptImports: [
`<script type="module" src="/${appEntryPoint}" defer></script>`,
],
});
// Write `index.html` (template) into the `/public` folder.
fs.writeFileSync(paths.appIndexHtml, html, { encoding: 'utf8' });
Expand Down
2 changes: 1 addition & 1 deletion packages/mc-scripts/src/commands/start-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function run() {
// Define the module entry point (path relative to the `/public` folder).
// NOTE: that this is different from the production configuration.
scriptImports: [
`<script type="module" src="/../${appEntryPoint}"></script>`,
`<script type="module" src="/../${appEntryPoint}" defer></script>`,
],
});
// Write `index.html` (template) into the `/public` folder.
Expand Down

0 comments on commit 5d55a50

Please sign in to comment.