Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
84 changes: 73 additions & 11 deletions app/composables/useSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { RemovableRef } from '@vueuse/core'
import { useLocalStorage } from '@vueuse/core'
import { ACCENT_COLORS } from '#shared/utils/constants'
import type { LocaleObject } from '@nuxtjs/i18n'
import { BACKGROUND_THEMES } from '#shared/utils/constants'
Expand Down Expand Up @@ -73,22 +71,86 @@ const DEFAULT_SETTINGS: AppSettings = {

const STORAGE_KEY = 'npmx-settings'

// Shared settings instance (singleton per app)
let settingsRef: RemovableRef<AppSettings> | null = null
/**
* Read settings from localStorage and merge with defaults.
*/
function normaliseSettings(input: AppSettings): AppSettings {
return {
...input,
searchProvider: input.searchProvider === 'npm' ? 'npm' : 'algolia',
sidebar: {
...input.sidebar,
collapsed: Array.isArray(input.sidebar?.collapsed)
? input.sidebar.collapsed.filter((v): v is string => typeof v === 'string')
: [],
},
}
}

function readFromLocalStorage(): AppSettings {
try {
const raw = localStorage.getItem(STORAGE_KEY)
if (raw) {
const stored = JSON.parse(raw)
return normaliseSettings({
...DEFAULT_SETTINGS,
...stored,
connector: { ...DEFAULT_SETTINGS.connector, ...stored.connector },
sidebar: { ...DEFAULT_SETTINGS.sidebar, ...stored.sidebar },
chartFilter: { ...DEFAULT_SETTINGS.chartFilter, ...stored.chartFilter },
})
}
} catch {}
return { ...DEFAULT_SETTINGS }
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

let syncInitialized = false

/**
* Composable for managing application settings with localStorage persistence.
* Settings are shared across all components that use this composable.
* Composable for managing application settings.
*
* Uses useState for SSR-safe hydration (server and client agree on initial
* values during hydration) and syncs with localStorage on the client.
* The onPrehydrate script in prehydrate.ts handles DOM-level patches
* (accent color, bg theme, collapsed sections, etc.) to prevent visual
* flash before hydration.
*/
export function useSettings() {
if (!settingsRef) {
settingsRef = useLocalStorage<AppSettings>(STORAGE_KEY, DEFAULT_SETTINGS, {
mergeDefaults: true,
})
const settings = useState<AppSettings>(STORAGE_KEY, () => ({ ...DEFAULT_SETTINGS }))

if (import.meta.client && !syncInitialized) {
syncInitialized = true

// Read localStorage eagerly but apply after mount to prevent hydration
// mismatch. During hydration, useState provides server-matching defaults.
// After mount, we swap in the user's actual preferences from localStorage.
// Uses nuxtApp.hook('app:mounted') instead of onMounted so it works even
// when useSettings() is first called from a plugin (no component context).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right fix. right now, with onPrehydrate, in many cases we're able to reflect the user's preferences immediately without any flash after mounting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point. What approach do you think we could look toward here?

I tried a few iterations but was only able to get satisfactory results using the useState + localStorage pattern. Though I can see some flashes (related to client-side sorting, I believe)

The core issue is that useLocalStorage from VueUse causes hydration mismatches because the server renders with defaults while the client initializes with stored values immediately. Cos right now, when we load https://npmx.dev/~antfu on page load with npm registry as a setting.

The onPrehydrate script handles the visual/DOM side (accent color, background theme, collapsed sections), but the reactive state still needs to reconcile somehow.

Open to suggestions if you have a different pattern in mind, I might be missing something in the Nuxt hydration lifecycle that could help here.

const stored = readFromLocalStorage()
const nuxtApp = useNuxtApp()

if (nuxtApp.isHydrating) {
nuxtApp.hook('app:mounted', () => {
settings.value = stored
})
} else {
settings.value = stored
}

// Persist future changes back to localStorage
watch(
settings,
value => {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(value))
} catch {}
},
{ deep: true },
)
}

return {
settings: settingsRef,
settings,
}
}

Expand Down
5 changes: 5 additions & 0 deletions app/utils/prehydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ export function initPreferencesOnPrehydrate() {
if (settings.keyboardShortcuts === false) {
document.documentElement.dataset.kbdShortcuts = 'false'
}

// Search provider (default: algolia)
if (settings.searchProvider === 'npm') {
document.documentElement.dataset.searchProvider = 'npm'
}
})
}
Loading