Skip to content

Commit a293eee

Browse files
natemoo-reJonasBa
andauthored
feat(theme): avoid flash of inaccurate theme in SPA mode (#102045)
This PR makes `pnpm dev-ui` and Vercel preview deployments reflect user theme preferences By inlining a small script to the [`index.ejs`](https://github.com/getsentry/sentry/blob/master/static/index.ejs) template, we can avoid [Flash of inAccurate coloR Theme (aka `FART`)](https://css-tricks.com/flash-of-inaccurate-color-theme-fart/). Unfortunately, the theme is asynchronously fetched, but by defaulting to `body.theme-system`, the likelihood of an unstyled flash should be greatly reduced. --------- Co-authored-by: Jonas <[email protected]>
1 parent 3c7aa06 commit a293eee

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

static/index.ejs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,27 @@
1212
}
1313
__sentryMark('head-start');
1414
</script>
15+
<link rel="preload" href="/api/client-config/" as="fetch" crossorigin />
16+
<script>
17+
// Django inlines a class for the user theme, but SPA mode requires manual init.
18+
// Default to `.theme-system` and replace with user theme as soon as possible.
19+
const BOOTSTRAP_URL = '/api/client-config/';
20+
async function applySpaTheme() {
21+
const res = await fetch(BOOTSTRAP_URL);
22+
if (res.ok) {
23+
const data = await res.then(res => res.json());
24+
const theme = data.user?.options?.theme;
25+
if (theme) {
26+
document.body.classList.replace('theme-system', `theme-${theme}`);
27+
return;
28+
}
29+
}
30+
document.body.classList.remove('theme-system');
31+
}
32+
applySpaTheme();
33+
</script>
1534
</head>
16-
17-
<body>
35+
<body class="theme-system">
1836
<% if (htmlWebpackPlugin.options.unsupportedBrowser) { %>
1937
<style>.unsupported-browser { display: none; }</style>
2038
<div class="unsupported-browser">

0 commit comments

Comments
 (0)