Skip to main content

buildThemeBootstrapScript

Builds the inline script that sets the theme before first paint, so a dark theme never flashes light. It also marks a stored globe map shape on the root element (MAP_SHAPE_ATTRIBUTE), so the map placeholder starts in the colour of the space around a globe rather than the flat map's. When given the languages the host offers, it also sets lang and dir on the root element to the stored language if (and only if) it is one of them and the current page does not declare a fixed language.

This is a function exported by @mapcairn/core, checked against version 0.2.0.

Signature​

import { buildThemeBootstrapScript } from "@mapcairn/core";

function buildThemeBootstrapScript(offeredLanguages?: readonly (string | LanguageOption)[]): string;

Parameters​

  • offeredLanguages? (readonly (string | LanguageOption)[]): The host's tags or language options. Without them the script leaves lang and dir as the server rendered them.

Returns​

string

Remarks​

The script embeds the real parseThemeChoice and resolveTheme through Function.prototype.toString, so it cannot drift from them, and interpolates the shared constants. Storage, matchMedia and the document are each guarded on their own, so a blocked one never stops the others and the script never throws. It rewrites every theme-color tag, because a page may carry one per prefers-color-scheme media query and the browser uses the tag whose media matches the OS, not the chosen theme. data-fixed-language on <html> tells it to leave lang and dir alone. The output is deterministic: the host hashes it for its content security policy. Whitespace is collapsed, nothing else.

See also​

Source: packages/core/src/theme/themeBootstrap.ts:39