Skip to content

Commit e331d86

Browse files
authored
feat: prefill package manager from query param (#2520)
1 parent c8b6087 commit e331d86

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

app/composables/useSelectedPackageManager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const useSelectedPackageManager = createSharedComposable(
1212

1313
// Sync to data-pm attribute on the client
1414
if (import.meta.client) {
15+
const queryPM = new URLSearchParams(window.location.search).get('pm')
16+
if (queryPM && packageManagers.some(pm => pm.id === queryPM)) {
17+
pm.value = queryPM as PackageManagerId
18+
}
1519
// Watch for changes and update the attribute
1620
watch(
1721
pm,

app/utils/prehydrate.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,28 @@ export function initPreferencesOnPrehydrate() {
3939
document.documentElement.dataset.bgTheme = preferredBackgroundTheme
4040
}
4141

42-
// Read and apply package manager preference
43-
const storedPM = localStorage.getItem('npmx-pm')
44-
// Parse the stored value (it's stored as a JSON string by useLocalStorage)
4542
let pm = 'npm'
46-
if (storedPM) {
47-
try {
48-
const parsed = JSON.parse(storedPM)
49-
if (validPMs.has(parsed)) {
50-
pm = parsed
51-
}
52-
} catch {
53-
// If parsing fails, check if it's a plain string (legacy format)
54-
if (validPMs.has(storedPM)) {
55-
pm = storedPM
43+
44+
// Support package manager preference in query string (for example, ?pm=pnpm)
45+
const queryPM = new URLSearchParams(window.location.search).get('pm')
46+
if (queryPM && validPMs.has(queryPM)) {
47+
pm = queryPM
48+
localStorage.setItem('npmx-pm', pm)
49+
} else {
50+
// Read and apply package manager preference
51+
const storedPM = localStorage.getItem('npmx-pm')
52+
// Parse the stored value (it's stored as a JSON string by useLocalStorage)
53+
if (storedPM) {
54+
try {
55+
const parsed = JSON.parse(storedPM)
56+
if (validPMs.has(parsed)) {
57+
pm = parsed
58+
}
59+
} catch {
60+
// If parsing fails, check if it's a plain string (legacy format)
61+
if (validPMs.has(storedPM)) {
62+
pm = storedPM
63+
}
5664
}
5765
}
5866
}

0 commit comments

Comments
 (0)