Skip to content

Commit

Permalink
Merge pull request #479 from jpudysz/fix/ssr
Browse files Browse the repository at this point in the history
fix: ssr issues
  • Loading branch information
jpudysz authored Jan 15, 2025
2 parents 526c75f + 5350990 commit 0f7e357
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/server/useServerUnistyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export const useServerUnistyles = () => {

if (typeof window !== 'undefined') {
UnistylesWeb.registry.css.hydrate(window.__UNISTYLES_STATE__)
document.querySelector('#unistyles-script')?.remove()
}
}
3 changes: 2 additions & 1 deletion src/web/convert/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const CSS_NUMBER_KEYS = new Set([
'strokeDashoffset',
'strokeMiterlimit',
'strokeOpacity',
'strokeWidth'
'strokeWidth',
'aspectRatio'
])

const convertMap = {
Expand Down
3 changes: 3 additions & 0 deletions src/web/css/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export class CSSState {
this.mainMap.forEach(styles => {
styles.delete(hash)
})
this.mqMap.forEach(styles => {
styles.delete(hash)
})

if (this.styleTag) {
this.styleTag.innerText = this.getStyles()
Expand Down
13 changes: 10 additions & 3 deletions src/web/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ export class UnistylesRegistry {

stylesCounter.delete(ref)

if (stylesCounter.size === 0 && !document.querySelector(`.${hash}`)) {
this.css.remove(hash)
this.stylesCache.delete(hash)
if (stylesCounter.size === 0) {
// Move this to the end of the event loop so the element is removed from the DOM
Promise.resolve().then(() => {
if (document.querySelector(`.${hash}`)) {
return
}

this.css.remove(hash)
this.stylesCache.delete(hash)
})

return true
}
Expand Down
6 changes: 6 additions & 0 deletions src/web/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { UnistylesServices } from './types'
type UnistylesSettings = Partial<UnionToIntersection<Required<UnistylesConfig>['settings']>>

export class UnistylesState {
isInitialized = false
themes = new Map<string, UnistylesTheme>()
cssThemes = new Map<string, UnistylesTheme>()
themeName?: AppThemeName
Expand All @@ -32,6 +33,11 @@ export class UnistylesState {
constructor(private services: UnistylesServices) {}

init = (config: UnistylesConfig) => {
if (this.isInitialized) {
return
}

this.isInitialized = true
this.initThemes(config.themes, config.settings?.CSSVars)
this.initBreakpoints(config.breakpoints)

Expand Down

0 comments on commit 0f7e357

Please sign in to comment.