Skip to content
Merged
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
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,13 @@
},
"optionalDependencies": {
"@rollup/rollup-linux-x64-gnu": "4.62.2"
},
"allowScripts": {
"@playwright/browser-chromium@1.61.1": true,
"@sentry/cli@2.58.6": true,
"esbuild@0.27.0": true,
"esbuild@0.28.1": true,
"esbuild@0.27.7": true,
"puppeteer@25.3.0": true
}
}
19 changes: 6 additions & 13 deletions src/components/Head/ThemeInit.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
*/
import { themeData } from './server/themeData'

const { defaultThemeIdJson, darkThemeIdJson, metaColorsJson } = themeData()
const { defaultThemeId, darkThemeId, metaColors } = themeData()
---

<script
is:inline
define:vars={{
defaultThemeIdJson,
darkThemeIdJson,
metaColorsJson,
defaultThemeId,
darkThemeId,
metaColors,
}}
>
// @ts-nocheck
Expand Down Expand Up @@ -48,18 +48,14 @@ const { defaultThemeIdJson, darkThemeIdJson, metaColorsJson } = themeData()

const runThemeInit = () => {
try {
const defaultThemeId = JSON.parse(defaultThemeIdJson)
const darkThemeId = JSON.parse(darkThemeIdJson)
const themeMetaColors = JSON.parse(metaColorsJson)

const stored = localStorage.getItem('theme')
const resolvedThemeId = getResolvedThemeId({ defaultThemeId, darkThemeId })
const activeThemeId = stored && stored !== defaultThemeId ? stored : resolvedThemeId

applyThemeToDocument(document, activeThemeId)

window.metaColors = window.metaColors || {}
Object.assign(window.metaColors, themeMetaColors)
Object.assign(window.metaColors, metaColors)

applyThemeMetaColor(document, activeThemeId)

Expand All @@ -78,13 +74,10 @@ const { defaultThemeIdJson, darkThemeIdJson, metaColorsJson } = themeData()
*/
document.addEventListener('astro:before-swap', event => {
try {
const defaultThemeId = JSON.parse(defaultThemeIdJson)
const darkThemeId = JSON.parse(darkThemeIdJson)
const themeMetaColors = JSON.parse(metaColorsJson)
const resolvedThemeId = getResolvedThemeId({ defaultThemeId, darkThemeId })

window.metaColors = window.metaColors || {}
Object.assign(window.metaColors, themeMetaColors)
Object.assign(window.metaColors, metaColors)

applyThemeToDocument(event.newDocument, resolvedThemeId)
applyThemeMetaColor(event.newDocument, resolvedThemeId)
Expand Down
8 changes: 4 additions & 4 deletions src/components/Head/server/__tests__/themeData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ describe('themeData', () => {
vi.restoreAllMocks()
})

it('serializes configured theme ids and colors into JSON-safe strings', async () => {
it('returns configured theme ids and colors for the inline theme bootstrap', async () => {
const themeData = await loadThemeData()
const data = themeData()

const defaultThemeId = JSON.parse(data.defaultThemeIdJson)
const defaultThemeId = data.defaultThemeId
expect(typeof defaultThemeId).toBe('string')
expect(defaultThemeId.length).toBeGreaterThan(0)

const darkThemeId = JSON.parse(data.darkThemeIdJson)
const darkThemeId = data.darkThemeId
expect(typeof darkThemeId).toBe('string')
expect(darkThemeId.length).toBeGreaterThan(0)

const colors = JSON.parse(data.metaColorsJson) as Record<string, string>
const colors = data.metaColors
const configuredIds = themeConfig.themes.map(theme => theme.id)

configuredIds.forEach(id => {
Expand Down
12 changes: 6 additions & 6 deletions src/components/Head/server/themeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import themeConfig from '@content/themes.json'
import { BuildError } from '@lib/errors/BuildError'

interface ThemeInitScriptData {
defaultThemeIdJson: string
darkThemeIdJson: string
metaColorsJson: string
defaultThemeId: string
darkThemeId: string
metaColors: Record<string, string>
}

type ThemeDataFunction = () => ThemeInitScriptData
Expand Down Expand Up @@ -39,9 +39,9 @@ export const themeData: ThemeDataFunction = () => {
const darkThemeId = hasDarkTheme ? 'dark' : defaultThemeId

return {
defaultThemeIdJson: JSON.stringify(defaultThemeId),
darkThemeIdJson: JSON.stringify(darkThemeId),
metaColorsJson: JSON.stringify(metaColors),
defaultThemeId,
darkThemeId,
metaColors,
}
} catch (error) {
throw new BuildError('Failed to prepare theme initialization data.', {
Expand Down
Loading