mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-04 13:12:10 +00:00
fix: template query param stripped during login views (#6677)
Fixes issue where query params from https://github.com/Comfy-Org/ComfyUI_frontend/pull/6593 are stripped during the login/signup views/flow by storing initial params in session storage via router plugin. https://github.com/user-attachments/assets/51642e8c-af5c-43ef-ab7d-133bc7e511aa ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6677-fix-template-query-param-stripped-during-login-views-2aa6d73d365081a1bdc7d22b35f72a77) by [Unito](https://www.unito.io)
This commit is contained in:
109
src/platform/navigation/preservedQueryManager.ts
Normal file
109
src/platform/navigation/preservedQueryManager.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import type { LocationQuery, LocationQueryRaw } from 'vue-router'
|
||||
|
||||
const STORAGE_PREFIX = 'Comfy.PreservedQuery.'
|
||||
const preservedQueries = new Map<string, Record<string, string>>()
|
||||
|
||||
const readQueryParam = (value: unknown): string | undefined => {
|
||||
return typeof value === 'string' ? value : undefined
|
||||
}
|
||||
|
||||
const getStorageKey = (namespace: string) => `${STORAGE_PREFIX}${namespace}`
|
||||
|
||||
const isValidQueryRecord = (
|
||||
value: unknown
|
||||
): value is Record<string, string> => {
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
||||
return false
|
||||
}
|
||||
return Object.values(value).every((v) => typeof v === 'string')
|
||||
}
|
||||
|
||||
const readFromStorage = (namespace: string): Record<string, string> | null => {
|
||||
try {
|
||||
const raw = sessionStorage.getItem(getStorageKey(namespace))
|
||||
if (!raw) return null
|
||||
|
||||
const parsed = JSON.parse(raw)
|
||||
if (!isValidQueryRecord(parsed)) {
|
||||
console.warn('[preservedQuery] invalid storage format')
|
||||
sessionStorage.removeItem(getStorageKey(namespace))
|
||||
return null
|
||||
}
|
||||
return parsed
|
||||
} catch (error) {
|
||||
console.warn('[preservedQuery] storage operation failed')
|
||||
sessionStorage.removeItem(getStorageKey(namespace))
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const writeToStorage = (
|
||||
namespace: string,
|
||||
payload: Record<string, string> | null
|
||||
) => {
|
||||
try {
|
||||
if (!payload || Object.keys(payload).length === 0) {
|
||||
sessionStorage.removeItem(getStorageKey(namespace))
|
||||
return
|
||||
}
|
||||
sessionStorage.setItem(getStorageKey(namespace), JSON.stringify(payload))
|
||||
} catch (error) {
|
||||
console.warn('[preservedQuery] failed to write storage', {
|
||||
namespace,
|
||||
error
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const hydratePreservedQuery = (namespace: string) => {
|
||||
if (preservedQueries.has(namespace)) return
|
||||
const payload = readFromStorage(namespace)
|
||||
if (payload) {
|
||||
preservedQueries.set(namespace, payload)
|
||||
}
|
||||
}
|
||||
|
||||
export const capturePreservedQuery = (
|
||||
namespace: string,
|
||||
query: LocationQuery,
|
||||
keys: string[]
|
||||
) => {
|
||||
const payload: Record<string, string> = {}
|
||||
|
||||
keys.forEach((key) => {
|
||||
const value = readQueryParam(query[key])
|
||||
if (value) {
|
||||
payload[key] = value
|
||||
}
|
||||
})
|
||||
|
||||
if (Object.keys(payload).length === 0) return
|
||||
|
||||
preservedQueries.set(namespace, payload)
|
||||
writeToStorage(namespace, payload)
|
||||
}
|
||||
|
||||
export const mergePreservedQueryIntoQuery = (
|
||||
namespace: string,
|
||||
query?: LocationQueryRaw
|
||||
): LocationQueryRaw | undefined => {
|
||||
const payload = preservedQueries.get(namespace)
|
||||
if (!payload) return undefined
|
||||
|
||||
const nextQuery: LocationQueryRaw = { ...(query || {}) }
|
||||
let changed = false
|
||||
|
||||
for (const [key, value] of Object.entries(payload)) {
|
||||
if (typeof nextQuery[key] === 'string') continue
|
||||
nextQuery[key] = value
|
||||
changed = true
|
||||
}
|
||||
|
||||
return changed ? nextQuery : undefined
|
||||
}
|
||||
|
||||
export const clearPreservedQuery = (namespace: string) => {
|
||||
if (!preservedQueries.has(namespace)) return
|
||||
preservedQueries.delete(namespace)
|
||||
writeToStorage(namespace, null)
|
||||
}
|
||||
Reference in New Issue
Block a user