Skip to content

Commit b21fb4d

Browse files
author
“bc-yevhenii-buliuk”
committed
fix(storefront): BCTHEME-991 Cornerstone performance optimizations: blocking scripts delaying DomContentLoaded.
1 parent 0e132e8 commit b21fb4d

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Draft
8+
- Cornerstone performance optimizations: blocking scripts delaying DomContentLoaded. [#2158](https://github.com/bigcommerce/cornerstone/pull/2158)
89

910
## 6.2.0 (12-13-2021)
1011
- Fix tooltip on close button of modal is shifted. [#2148](https://github.com/bigcommerce/cornerstone/pull/2148)

assets/js/theme/common/font.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const WebFont = require('webfontloader');
2+
3+
const linkEl = document.querySelector('link[href*="https://fonts.googleapis.com/css"]');
4+
const fontUrl = linkEl.getAttribute('href');
5+
const regexFontsCollection = /family=([^&]*)/gm;
6+
const families = regexFontsCollection.exec(fontUrl)[1].replace(/\+/gm, ' ').split('|');
7+
8+
WebFont.load({
9+
custom: {
10+
families,
11+
},
12+
classes: false,
13+
});

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"slick-carousel": "^1.8.1",
2626
"svg-injector": "^1.1.3",
2727
"sweetalert2": "^9.17.2",
28+
"webfontloader": "^1.6.28",
2829
"whatwg-fetch": "^3.6.1"
2930
},
3031
"devDependencies": {

templates/layout/base.html

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,12 @@
2525
window.lazySizesConfig.loadMode = 1;
2626
</script>
2727
<script async src="{{cdn 'assets/dist/theme-bundle.head_async.js'}}"></script>
28+
29+
{{getFontsCollection font-display='block'}}
30+
31+
<link rel="preload" href="{{cdn 'assets/dist/theme-bundle.font.js'}}" as="script">
32+
<script async src="{{cdn 'assets/dist/theme-bundle.font.js'}}"></script>
2833

29-
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
30-
31-
<script>
32-
WebFont.load({
33-
custom: {
34-
families: ['Karla', 'Roboto', 'Source Sans Pro']
35-
},
36-
classes: false
37-
});
38-
</script>
39-
40-
{{ getFontsCollection }}
4134
{{{stylesheet '/assets/css/theme.css'}}}
4235

4336
{{{head.scripts}}}
@@ -58,6 +51,9 @@
5851
{{~inject 'carouselArrowAndDotAriaLabel' (lang 'carousel.arrow_and_dot_aria_label')}}
5952
{{~inject 'carouselActiveDotAriaLabel' (lang 'carousel.active_dot_aria_label')}}
6053
{{~inject 'carouselContentAnnounceMessage' (lang 'carousel.content_announce_message')}}
54+
55+
{{!-- Get this loading ASAP --}}
56+
<link rel="preload" href="{{cdn 'assets/dist/theme-bundle.main.js'}}" as="script">
6157
</head>
6258
<body>
6359
<svg data-src="{{cdn 'img/icon-sprite.svg'}}" class="icons-svg-sprite"></svg>
@@ -67,11 +63,13 @@
6763
{{> components/common/footer }}
6864

6965
<script>window.__webpack_public_path__ = "{{cdn 'assets/dist/'}}";</script>
70-
<script src="{{cdn 'assets/dist/theme-bundle.main.js'}}"></script>
7166
<script>
7267
{{!-- Exported in app.js --}}
73-
window.stencilBootstrap("{{page_type}}", {{jsContext}}).load();
68+
function onThemeBundleMain() {
69+
window.stencilBootstrap("{{page_type}}", {{jsContext}}).load();
70+
}
7471
</script>
72+
<script async defer src="{{cdn 'assets/dist/theme-bundle.main.js'}}" onload="onThemeBundleMain()"></script>
7573

7674
{{{footer.scripts}}}
7775
</body>

webpack.common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
entry: {
1212
main: './assets/js/app.js',
1313
head_async: ['lazysizes'],
14+
font: './assets/js/theme/common/font.js',
1415
polyfills: './assets/js/polyfills.js',
1516
},
1617
module: {

0 commit comments

Comments
 (0)