mirror of
https://github.com/cotes2020/jekyll-theme-chirpy.git
synced 2026-06-21 23:38:39 +00:00
fix(giscus): synchronize theme state during lazy loading (#2742)
Previously, theme switch events were lost if triggered before the Giscus iframe was fully loaded (e.g., when deferred by lazy loading or before the client script executed). This resulted in Giscus rendering with the outdated initial theme once it finally appeared in the viewport. This commit handles these edge cases by: 1. Updating the `data-theme` attribute on the Giscus script node if the iframe hasn't been created yet. 2. Modifying the `theme` query parameter in the iframe's `src` URL if it is currently in a pending/lazy-loading state.
This commit is contained in:
@@ -39,16 +39,25 @@
|
||||
addEventListener('message', (event) => {
|
||||
if (event.source === window && event.data && event.data.id === Theme.ID) {
|
||||
const newTheme = themeMapper[Theme.visualState];
|
||||
|
||||
const message = {
|
||||
setConfig: {
|
||||
theme: newTheme
|
||||
}
|
||||
};
|
||||
|
||||
const giscus =
|
||||
document.getElementsByClassName('giscus-frame')[0].contentWindow;
|
||||
giscus.postMessage({ giscus: message }, 'https://giscus.app');
|
||||
const iframe = document.querySelector('.giscus-frame');
|
||||
|
||||
if (!iframe) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (iframe.classList.contains('giscus-frame--loading')) {
|
||||
let url = new URL(iframe.src);
|
||||
url.searchParams.set('theme', newTheme);
|
||||
iframe.src = url.toString();
|
||||
}
|
||||
|
||||
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user