Skip to content

Commit 09cc75d

Browse files
committed
perf: lazy-load SuperDoc bundle on preview click
Move superdoc.umd.js (1.8MB) and style.css (24KB) from eager render-blocking tags to dynamic loading inside openPreview(). Both are only needed when a user clicks to preview a document.
1 parent 0a24af7 commit 09cc75d

1 file changed

Lines changed: 45 additions & 18 deletions

File tree

apps/web/index.html

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
<link rel="icon" type="image/png" sizes="32x32" href="/public/favicon-32x32.png">
7171
<link rel="icon" type="image/png" sizes="16x16" href="/public/favicon-16x16.png">
7272
<link rel="apple-touch-icon" sizes="180x180" href="/public/apple-touch-icon.png">
73-
<link href="https://cdn.jsdelivr.net/npm/superdoc/dist/style.css" rel="stylesheet">
7473
<link rel="preconnect" href="https://fonts.googleapis.com">
7574
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
7675
<link
@@ -1002,7 +1001,6 @@ <h1>The largest open corpus of classified Word documents</h1>
10021001
</div>
10031002
</div>
10041003

1005-
<script src="https://cdn.jsdelivr.net/npm/superdoc/dist/superdoc.umd.js"></script>
10061004
<script>
10071005
(function () {
10081006
// ---------- state (seed from URL params) ----------
@@ -1043,6 +1041,30 @@ <h1>The largest open corpus of classified Word documents</h1>
10431041
return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
10441042
}
10451043

1044+
// ---------- lazy-load SuperDoc ----------
1045+
let superdocLoaded = false;
1046+
let superdocLoading = null;
1047+
1048+
function loadSuperdoc() {
1049+
if (superdocLoaded) return Promise.resolve();
1050+
if (superdocLoading) return superdocLoading;
1051+
1052+
superdocLoading = new Promise(function (resolve, reject) {
1053+
var link = document.createElement('link');
1054+
link.rel = 'stylesheet';
1055+
link.href = 'https://cdn.jsdelivr.net/npm/superdoc/dist/style.css';
1056+
document.head.appendChild(link);
1057+
1058+
var script = document.createElement('script');
1059+
script.src = 'https://cdn.jsdelivr.net/npm/superdoc/dist/superdoc.umd.js';
1060+
script.onload = function () { superdocLoaded = true; resolve(); };
1061+
script.onerror = function () { reject(new Error('Failed to load SuperDoc')); };
1062+
document.body.appendChild(script);
1063+
});
1064+
1065+
return superdocLoading;
1066+
}
1067+
10461068
// ---------- preview ----------
10471069
let currentSuperdoc = null;
10481070

@@ -1053,6 +1075,7 @@ <h1>The largest open corpus of classified Word documents</h1>
10531075
$('preview-title').textContent = name;
10541076
$('preview-dl').href = url;
10551077
$('preview-loading').style.display = 'flex';
1078+
$('preview-loading').textContent = 'Loading preview…';
10561079
$('preview-container').innerHTML = '';
10571080
$('preview-overlay').classList.add('open');
10581081
document.body.style.overflow = 'hidden';
@@ -1063,23 +1086,27 @@ <h1>The largest open corpus of classified Word documents</h1>
10631086
currentSuperdoc = null;
10641087
}
10651088

1066-
try {
1067-
currentSuperdoc = new SuperDocLibrary.SuperDoc({
1068-
selector: '#preview-container',
1069-
document: url,
1070-
documentMode: 'viewing',
1071-
role: 'viewer',
1072-
hideToolbar: true,
1073-
onReady: function () {
1074-
$('preview-loading').style.display = 'none';
1075-
},
1076-
onContentError: function () {
1077-
$('preview-loading').textContent = 'Could not render this document';
1078-
},
1079-
});
1080-
} catch {
1089+
loadSuperdoc().then(function () {
1090+
try {
1091+
currentSuperdoc = new SuperDocLibrary.SuperDoc({
1092+
selector: '#preview-container',
1093+
document: url,
1094+
documentMode: 'viewing',
1095+
role: 'viewer',
1096+
hideToolbar: true,
1097+
onReady: function () {
1098+
$('preview-loading').style.display = 'none';
1099+
},
1100+
onContentError: function () {
1101+
$('preview-loading').textContent = 'Could not render this document';
1102+
},
1103+
});
1104+
} catch {
1105+
$('preview-loading').textContent = 'Could not load SuperDoc viewer';
1106+
}
1107+
}).catch(function () {
10811108
$('preview-loading').textContent = 'Could not load SuperDoc viewer';
1082-
}
1109+
});
10831110
}
10841111

10851112
function closePreview() {

0 commit comments

Comments
 (0)