Fix turbo nav layout flash and stats page not loading.
Wait for page CSS before swapping content, hoist inline styles to head, and boot page scripts immediately when DOM markers exist. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+41
-23
@@ -71,9 +71,19 @@
|
||||
if (el.tagName === 'LINK' && isPermanentStylesheet(el)) return;
|
||||
out.push(el);
|
||||
});
|
||||
doc.querySelectorAll('.main style').forEach(function (el) {
|
||||
out.push(el);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
function prepareMainHtml(doc) {
|
||||
var fetchedMain = doc.querySelector('.main');
|
||||
if (!fetchedMain) return '';
|
||||
fetchedMain.querySelectorAll('style').forEach(function (el) { el.remove(); });
|
||||
return fetchedMain.innerHTML;
|
||||
}
|
||||
|
||||
function collectPageScripts(doc) {
|
||||
var out = [];
|
||||
var pastCore = false;
|
||||
@@ -83,29 +93,34 @@
|
||||
return;
|
||||
}
|
||||
if (!pastCore) return;
|
||||
if (isCoreScript(el)) return;
|
||||
out.push(el);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
function removePageAssets() {
|
||||
document.querySelectorAll('head link[rel="stylesheet"]').forEach(function (el) {
|
||||
if (!isPermanentStylesheet(el)) el.remove();
|
||||
});
|
||||
document.querySelectorAll('head style').forEach(function (el) { el.remove(); });
|
||||
document.querySelectorAll('[data-page-css]').forEach(function (el) { el.remove(); });
|
||||
document.querySelectorAll('body script[data-page-js]').forEach(function (el) { el.remove(); });
|
||||
}
|
||||
|
||||
function applyPageCss(items) {
|
||||
items.forEach(function (srcEl) {
|
||||
var el = srcEl.cloneNode(true);
|
||||
if (el.tagName === 'LINK') {
|
||||
el.setAttribute('data-page-css', '');
|
||||
} else {
|
||||
el.setAttribute('data-page-css', '');
|
||||
}
|
||||
document.head.appendChild(el);
|
||||
});
|
||||
return items.reduce(function (chain, srcEl) {
|
||||
return chain.then(function () {
|
||||
return new Promise(function (resolve) {
|
||||
var el = srcEl.cloneNode(true);
|
||||
el.setAttribute('data-page-css', '');
|
||||
if (el.tagName === 'LINK') {
|
||||
el.onload = function () { resolve(); };
|
||||
el.onerror = function () { resolve(); };
|
||||
document.head.appendChild(el);
|
||||
} else {
|
||||
document.head.appendChild(el);
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}, Promise.resolve());
|
||||
}
|
||||
|
||||
function runPageScripts(items) {
|
||||
@@ -132,7 +147,7 @@
|
||||
}, Promise.resolve());
|
||||
}
|
||||
|
||||
function syncNavActive(doc, url) {
|
||||
function syncNavActive(doc) {
|
||||
var nav = document.getElementById('site-nav');
|
||||
var fetchedNav = doc.getElementById('site-nav');
|
||||
if (!nav || !fetchedNav) return;
|
||||
@@ -153,20 +168,23 @@
|
||||
|
||||
function applyDocument(doc, url, fromPopstate) {
|
||||
var main = document.querySelector('.main');
|
||||
var fetchedMain = doc.querySelector('.main');
|
||||
if (!main || !fetchedMain) {
|
||||
if (!main || !doc.querySelector('.main')) {
|
||||
window.location.href = url;
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
window.dispatchEvent(new Event('qihuo:page-leave'));
|
||||
removePageAssets();
|
||||
applyPageCss(collectPageCss(doc));
|
||||
main.innerHTML = fetchedMain.innerHTML;
|
||||
document.title = doc.title || document.title;
|
||||
syncNavActive(doc, url);
|
||||
|
||||
return runPageScripts(collectPageScripts(doc)).then(function () {
|
||||
var cssItems = collectPageCss(doc);
|
||||
var mainHtml = prepareMainHtml(doc);
|
||||
|
||||
return applyPageCss(cssItems).then(function () {
|
||||
main.innerHTML = mainHtml;
|
||||
document.title = doc.title || document.title;
|
||||
syncNavActive(doc, url);
|
||||
return runPageScripts(collectPageScripts(doc));
|
||||
}).then(function () {
|
||||
currentUrl = normalizeUrl(url);
|
||||
if (!fromPopstate) {
|
||||
history.pushState({ turbo: true }, '', currentUrl);
|
||||
@@ -190,8 +208,8 @@
|
||||
return fetchPage(target, ctrl.signal).then(function (doc) {
|
||||
if (ctrl.signal.aborted) return;
|
||||
inflight = null;
|
||||
applyDocument(doc, target, !!opts.fromPopstate);
|
||||
}).catch(function (err) {
|
||||
return applyDocument(doc, target, !!opts.fromPopstate);
|
||||
}).catch(function () {
|
||||
if (ctrl.signal.aborted) return;
|
||||
inflight = null;
|
||||
setLoading(false);
|
||||
|
||||
Reference in New Issue
Block a user