Skip to content

Commit 263a337

Browse files
committed
fix(Build): Load modernizr early and non-asynchronously.
Include the modernizr bundle by injecting a script tag. This ensures modernizr is loaded synchronously and executing early and sets it's feature detection classes before the layout is done by the browser. This reverts the breaking change from the previous Patternslib 9.8.0-beta.2 release. The separate modernizr.min.js build file is still kept, but modernizr is included by the main Patternslib bundle. There is no need to add another script tag to include modernizr. You can disable loading of modernizr by setting the following before the Patternslib bundle.min.js is included: <script>window.__patternslib_disable_modernizr = true;</script> Since Patternslib 9.0.0-alpha.0 where we introduced webpack module federation for our bundles, Modernizr is loaded asynchronously and applying it's CSS classes a tick too late. For example, the change from the "no-js" to the "js" class was done while the tiles have already been drawn and visible on the screen, resulting in screen flickering. There are a number of projects which depend on Modernizr being applied early.
1 parent 836c912 commit 263a337

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<title>Patterns demo pages</title>
55
<meta charset="utf-8">
66
<link rel="stylesheet" href="/style/common.css" />
7-
<script src="/modernizr.min.js"></script>
87
<script src="/bundle.min.js"></script>
98
</head>
109
<body class="component-index">

src/feature-detection.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(function () {
2+
// Do not load modernizr if disabled. It's enabled by default.
3+
// You might want to disable it for your project by setting:
4+
// window.__patternslib_disable_modernizr = true;
5+
if (window.__patternslib_disable_modernizr) {
6+
return;
7+
}
8+
9+
// Add JavaScript feature as class to the <html> element, just like Modernizr.
10+
// This is needed for accessibility reasons, to support browsers and situations
11+
// where JavaScript is not available or disabled.
12+
var html = document.getElementsByTagName("html")[0];
13+
if (html.classList.contains("no-js")) {
14+
html.classList.remove("no-js");
15+
html.classList.add("js");
16+
}
17+
18+
// Get this one's script src.
19+
// See: https://stackoverflow.com/a/984656/1337474
20+
var scripts = document.getElementsByTagName("script");
21+
var script = scripts[scripts.length - 1];
22+
var script_url = script.src;
23+
// Get the base URL of this script.
24+
script_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/";
25+
26+
// Inject a new one with the modernizr bundle.
27+
var script_tag = document.createElement("script");
28+
script_tag.src = script_url + "modernizr.min.js";
29+
document.getElementsByTagName("head")[0].appendChild(script_tag);
30+
})();

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// Load modernizr and the `html.js` feature class.
2+
import "./feature-detection";
3+
14
// Webpack entry point for module federation.
25
import "@patternslib/dev/webpack/module_federation";
6+
37
// The next import needs to be kept with parentheses, otherwise we get this error:
48
// "Shared module is not available for eager consumption."
59
import("./patterns");

src/pat/clone-code/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<title>pat-clone demo page</title>
55
<meta charset="utf-8">
66
<link rel="stylesheet" href="/style/common.css" />
7+
<script>window.__patternslib_disable_modernizr = true;</script>
78
<script src="/bundle.min.js"></script>
89
</head>
910
<body>

0 commit comments

Comments
 (0)