Skip to content

Commit

Permalink
Render the app after the stylesheet has loaded
Browse files Browse the repository at this point in the history
In order to avoif the flash of unstyled content.
  • Loading branch information
mthh committed Jul 5, 2024
1 parent cdecfc8 commit f734620
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 26 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,29 @@ loadLocale('en');
// to update the mode accordingly)
initializeLightDarkMode();

render(
() => (
<TypesafeI18n locale={'en'}>
<AppPage />
</TypesafeI18n>
) as JSX.Element,
root,
);
const firstRender = () => {
render(
() => (
<TypesafeI18n locale={'en'}>
<AppPage />
</TypesafeI18n>
) as JSX.Element,
root,
);
};

const s = document.querySelector('#stylesheet');

if (s instanceof HTMLLinkElement) {
// In production, we only want to render the app after the stylesheet has loaded
// (to avoid the flash of unstyled content)
if (s.sheet) firstRender();
else {
s.addEventListener('load', () => {
firstRender();
});
}
} else {
// In dev we can just render the app right away
firstRender();
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const jsToBottomCssNoneMedia = () => ({
// eslint-disable-next-line no-param-reassign
html = html.replace('<!-- # INSERT SCRIPT HERE -->', scriptTag);
// eslint-disable-next-line no-param-reassign
html = html.replace('rel="stylesheet"', 'rel="stylesheet" media="none" onload="if(media!=\'all\')media=\'all\'"');
html = html.replace('rel="stylesheet"', 'id="stylesheet" rel="stylesheet" media="none" onload="if(media!=\'all\')media=\'all\'"');
return html;
},
});
Expand Down

0 comments on commit f734620

Please sign in to comment.