Skip to content

fix: fixed theme flash on page load #5619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions next.constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ export const DEFAULT_LOCALE_CODE = nextLocales.defaultLocale.code;
*/
export const LEGACY_JAVASCRIPT_FILE = `${BASE_PATH}/static/js/legacyMain.js`;

/**
* This script source replaces the logic for the initial theme set in the Legacy Javascript File.
*
* @deprecated This theme script is a temporal fix until the Legacy Website is removed.
*/
export const INITIAL_THEME_SCRIPT = `!(function () {
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const theme = localStorage.getItem('theme') ?? systemTheme;
document.querySelector('html').setAttribute('data-theme', theme);
document.body.className = theme;
window.localStorage.setItem('theme', theme);
})();`;

/**
* This is a list of all static routes or pages from the Website that we do not
* want to allow to be statically built on our Static Export Build.
Expand Down
7 changes: 7 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ const Document = () => (
src={nextConstants.LEGACY_JAVASCRIPT_FILE}
/>

<script
id="initial-theme-script"
dangerouslySetInnerHTML={{
__html: nextConstants.INITIAL_THEME_SCRIPT,
}}
/>

<a rel="me" href="https://social.lfx.dev/@nodejs" />
</body>
</Html>
Expand Down
7 changes: 0 additions & 7 deletions public/static/js/legacyMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,12 @@ const listenScrollToTopButton = () => {
});
};

const setCurrentTheme = () =>
setTheme(getTheme() || (preferredColorScheme.matches ? 'dark' : 'light'));

const startLegacyApp = () => {
setCurrentTheme();

watchThemeChanges();

listenLanguagePickerButton();
listenThemeToggleButton();
listenScrollToTopButton();
};

setCurrentTheme();

window.startLegacyApp = startLegacyApp;