Skip to content

Commit f3fb74d

Browse files
authored
fix: Refactor inspector.js into ES modules (#161)
1 parent 5639b87 commit f3fb74d

10 files changed

Lines changed: 2270 additions & 2204 deletions

File tree

src/view/frontend/templates/inspector.phtml

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,47 @@ $alpineBootstrap = <<<JS
2020
(function() {
2121
'use strict';
2222
23-
// Check if Alpine.js is already loaded (Hyvä themes) to avoid conflicts
24-
if (typeof Alpine !== 'undefined') {
25-
console.log('[MageForge Inspector] Alpine.js already loaded');
26-
return;
23+
function loadAlpineFromCDN() {
24+
// Check if Alpine.js is already loaded (Hyvä themes) to avoid conflicts
25+
if (typeof Alpine !== 'undefined') {
26+
console.log('[MageForge Inspector] Alpine.js already loaded');
27+
return;
28+
}
29+
30+
console.log('[MageForge Inspector] Loading Alpine.js from CDN');
31+
32+
var alpineScript = document.createElement('script');
33+
alpineScript.src = 'https://cdn.jsdelivr.net/npm/alpinejs@3.15.11/dist/cdn.min.js';
34+
alpineScript.integrity = 'sha256-vuumPQiVb2T6Bg9rainIejQb8Gn7lslFnCIsb9QuWK4=';
35+
alpineScript.crossOrigin = 'anonymous';
36+
alpineScript.onload = function() {
37+
console.log('[MageForge Inspector] Alpine.js loaded successfully');
38+
};
39+
alpineScript.onerror = function() {
40+
console.error('[MageForge Inspector] Failed to load Alpine.js');
41+
};
42+
43+
document.head.appendChild(alpineScript);
2744
}
2845
29-
console.log('[MageForge Inspector] Loading Alpine.js from CDN');
30-
31-
var alpineScript = document.createElement('script');
32-
alpineScript.src = 'https://cdn.jsdelivr.net/npm/alpinejs@3.15.11/dist/cdn.min.js';
33-
alpineScript.integrity = 'sha256-vuumPQiVb2T6Bg9rainIejQb8Gn7lslFnCIsb9QuWK4=';
34-
alpineScript.crossOrigin = 'anonymous';
35-
alpineScript.onload = function() {
36-
console.log('[MageForge Inspector] Alpine.js loaded successfully');
37-
};
38-
alpineScript.onerror = function() {
39-
console.error('[MageForge Inspector] Failed to load Alpine.js');
40-
};
41-
42-
// Defer injection until DOMContentLoaded so that all <script defer> tags
43-
// (including inspector.js) have already executed and registered their
44-
// alpine:init listener before Alpine starts.
46+
// Use a short microtask delay after DOMContentLoaded so that all deferred
47+
// and module scripts (including Hyvä's Alpine bundle) have had a chance to
48+
// run before we decide to load Alpine from CDN.
4549
if (document.readyState === 'loading') {
4650
document.addEventListener('DOMContentLoaded', function() {
47-
document.head.appendChild(alpineScript);
51+
// setTimeout(0) yields to the script queue, giving defer/module
52+
// scripts priority over this CDN fallback.
53+
setTimeout(loadAlpineFromCDN, 0);
4854
});
4955
} else {
50-
// DOMContentLoaded already fired (e.g. script injected late)
51-
document.head.appendChild(alpineScript);
56+
setTimeout(loadAlpineFromCDN, 0);
5257
}
5358
})();
5459
JS;
5560
?>
5661
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $alpineBootstrap, false) ?>
5762

58-
<script defer src="<?= $escaper->escapeUrl($block->getJsUrl()) ?>"></script>
63+
<script type="module" src="<?= $escaper->escapeUrl($block->getJsUrl()) ?>"></script>
5964

6065
<!-- MageForge Inspector Component Wrapper -->
6166
<div class="mageforge-inspector"

0 commit comments

Comments
 (0)