Skip to content

Make persistent cache work with Raycast environment #3415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/createAsyncStoragePersister/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PersistedClient, Persister } from '../persistQueryClient'
import { asyncThrottle } from './asyncThrottle'

interface AsyncStorage {
export interface AsyncStorage {
getItem: (key: string) => Promise<string | null>
setItem: (key: string, value: string) => Promise<void>
removeItem: (key: string) => Promise<void>
}

interface CreateAsyncStoragePersisterOptions {
export interface CreateAsyncStoragePersisterOptions {
/** The storage client used for setting an retrieving items from cache */
storage: AsyncStorage
/** The key to use when storing the cache */
Expand Down
8 changes: 5 additions & 3 deletions src/createWebStoragePersister/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { noop } from '../core/utils'
import { PersistedClient, Persister } from '../persistQueryClient'

interface CreateWebStoragePersisterOptions {
/** The storage client used for setting an retrieving items from cache */
storage: Storage
/** The storage client used for setting an retrieving items from cache.
* For SSR pass in `undefined`.
*/
storage?: Storage
/** The key to use when storing the cache */
key?: string
/** To avoid spamming,
Expand Down Expand Up @@ -31,7 +33,7 @@ export function createWebStoragePersister({
//try to save data to storage
function trySave(persistedClient: PersistedClient) {
try {
storage.setItem(key, serialize(persistedClient))
storage?.setItem(key, serialize(persistedClient))
} catch {
return false
}
Expand Down
78 changes: 35 additions & 43 deletions src/persistQueryClient/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,32 @@ export async function persistQueryClientRestore({
buster = '',
hydrateOptions,
}: PersistedQueryClientRestoreOptions) {
if (typeof window !== 'undefined') {
try {
const persistedClient = await persister.restoreClient()

if (persistedClient) {
if (persistedClient.timestamp) {
const expired = Date.now() - persistedClient.timestamp > maxAge
const busted = persistedClient.buster !== buster
if (expired || busted) {
persister.removeClient()
} else {
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
}
} else {
try {
const persistedClient = await persister.restoreClient()

if (persistedClient) {
if (persistedClient.timestamp) {
const expired = Date.now() - persistedClient.timestamp > maxAge
const busted = persistedClient.buster !== buster
if (expired || busted) {
persister.removeClient()
} else {
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
}
} else {
persister.removeClient()
}
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
queryClient.getLogger().error(err)
queryClient
.getLogger()
.warn(
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'
)
}
persister.removeClient()
}
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
queryClient.getLogger().error(err)
queryClient
.getLogger()
.warn(
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'
)
}
persister.removeClient()
}
}

Expand All @@ -107,15 +105,13 @@ export async function persistQueryClientSave({
buster = '',
dehydrateOptions,
}: PersistedQueryClientSaveOptions) {
if (typeof window !== 'undefined') {
const persistClient: PersistedClient = {
buster,
timestamp: Date.now(),
clientState: dehydrate(queryClient, dehydrateOptions),
}

await persister.persistClient(persistClient)
const persistClient: PersistedClient = {
buster,
timestamp: Date.now(),
clientState: dehydrate(queryClient, dehydrateOptions),
}

await persister.persistClient(persistClient)
}

/**
Expand Down Expand Up @@ -156,17 +152,13 @@ export function persistQueryClient(
persistQueryClientUnsubscribe?.()
}

let restorePromise = Promise.resolve()

if (typeof window !== 'undefined') {
// Attempt restore
restorePromise = persistQueryClientRestore(props).then(() => {
if (!hasUnsubscribed) {
// Subscribe to changes in the query cache to trigger the save
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)
}
})
}
// Attempt restore
const restorePromise = persistQueryClientRestore(props).then(() => {
if (!hasUnsubscribed) {
// Subscribe to changes in the query cache to trigger the save
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)
}
})

return [unsubscribe, restorePromise]
}