snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
autofix.ci / autofix (push) Failing after 4s
Release / Release (push) Has been skipped
GitHub Actions Security Analysis / Run zizmor (push) Failing after 2s

This commit is contained in:
upstream-mirror
2026-09-16 01:13:08 +05:00
commit 3a013ccdfe
3570 changed files with 490194 additions and 0 deletions

View File

@@ -0,0 +1,539 @@
# @tanstack/query-sync-storage-persister
## 5.102.8
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.102.8
- @tanstack/query-persist-client-core@5.102.8
## 5.102.7
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.102.7
- @tanstack/query-persist-client-core@5.102.7
## 5.102.6
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.102.6
- @tanstack/query-persist-client-core@5.102.6
## 5.102.5
### Patch Changes
- Updated dependencies [[`578e5c2`](https://github.com/TanStack/query/commit/578e5c26e8ebd0d7351b4b8e2bafba695e672b8d)]:
- @tanstack/query-core@5.102.5
- @tanstack/query-persist-client-core@5.102.5
## 5.102.4
### Patch Changes
- Updated dependencies [[`a05df6a`](https://github.com/TanStack/query/commit/a05df6aefb0e2489ec2c879ae16e2ee7cb3123ec)]:
- @tanstack/query-core@5.102.4
- @tanstack/query-persist-client-core@5.102.4
## 5.102.3
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.102.3
- @tanstack/query-persist-client-core@5.102.3
## 5.102.2
### Patch Changes
- Updated dependencies [[`80fbf73`](https://github.com/TanStack/query/commit/80fbf73e77892d702c107e14a84c219a8ed825dc)]:
- @tanstack/query-core@5.102.2
- @tanstack/query-persist-client-core@5.102.2
## 5.102.1
### Patch Changes
- Updated dependencies [[`134890d`](https://github.com/TanStack/query/commit/134890dbdc60e4fb0313e44b512d29284ca82f96)]:
- @tanstack/query-core@5.102.1
- @tanstack/query-persist-client-core@5.102.1
## 5.102.0
### Patch Changes
- Updated dependencies [[`34f7cee`](https://github.com/TanStack/query/commit/34f7ceed09c10e4a3aa2df31a106ddf02ec4e787), [`b4368c4`](https://github.com/TanStack/query/commit/b4368c43792349f6c29d1fb41f7ee1ef3a8bdd2c), [`5bb089d`](https://github.com/TanStack/query/commit/5bb089d153d33933983b7ea80f8d69b51d423698), [`ba4650c`](https://github.com/TanStack/query/commit/ba4650c05e61e33c609e051b948c9f7d31ce70d1), [`294d4e6`](https://github.com/TanStack/query/commit/294d4e62c4f7d674574a4903eef2a1bc3dd1413b), [`1f631b3`](https://github.com/TanStack/query/commit/1f631b3604aab6567fb3f6c90646a5a304641546), [`01a02bf`](https://github.com/TanStack/query/commit/01a02bfad32f9efbc17796be31d6399f1197a655), [`18c1c1e`](https://github.com/TanStack/query/commit/18c1c1ef94883e781ad36d36f9ea4a043ce4260b), [`5448063`](https://github.com/TanStack/query/commit/5448063f828d2270dccd41ae375e1afde35e91f2), [`2215bb0`](https://github.com/TanStack/query/commit/2215bb031139cdc8a84751b37a485c38ca9d2b6e), [`1ef4208`](https://github.com/TanStack/query/commit/1ef42087c9a266c2137d2ef645cbcc662f60ac93), [`5981771`](https://github.com/TanStack/query/commit/5981771abec9330b344a2617543eb05ba7c99e24), [`4a9bef6`](https://github.com/TanStack/query/commit/4a9bef6cf19b1cd6b014032d696129f12a848185), [`bef4bc7`](https://github.com/TanStack/query/commit/bef4bc780ce7cca32b7e3dea85f77d92f82a62a2), [`9656dc4`](https://github.com/TanStack/query/commit/9656dc4e5fef5f8c502579b52459f9e8c72e787c), [`326aaf1`](https://github.com/TanStack/query/commit/326aaf1333e5d9cbc46569c53f218d31684162d2), [`3e83601`](https://github.com/TanStack/query/commit/3e836010032c2586e9f1c66a271fa9114a6401f9), [`c6fc17c`](https://github.com/TanStack/query/commit/c6fc17cfad6e452261c585fabfb8c210e60531ed)]:
- @tanstack/query-core@5.102.0
- @tanstack/query-persist-client-core@5.102.0
## 5.101.4
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.101.4
- @tanstack/query-persist-client-core@5.101.4
## 5.101.3
### Patch Changes
- Updated dependencies [[`7e3c822`](https://github.com/TanStack/query/commit/7e3c822a10896f41a8f1031c16b85096277af677)]:
- @tanstack/query-core@5.101.3
- @tanstack/query-persist-client-core@5.101.3
## 5.101.2
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.101.2
- @tanstack/query-persist-client-core@5.101.2
## 5.101.1
### Patch Changes
- Updated dependencies [[`9eff92e`](https://github.com/TanStack/query/commit/9eff92ed86e284ec0125b3a3539d028688235bd1)]:
- @tanstack/query-core@5.101.1
- @tanstack/query-persist-client-core@5.101.1
## 5.101.0
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.101.0
- @tanstack/query-persist-client-core@5.101.0
## 5.100.14
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.14
- @tanstack/query-persist-client-core@5.100.14
## 5.100.13
### Patch Changes
- Updated dependencies [[`d423168`](https://github.com/TanStack/query/commit/d423168f6261a5cb3d353e53b27c8150cc271151)]:
- @tanstack/query-core@5.100.13
- @tanstack/query-persist-client-core@5.100.13
## 5.100.12
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.12
- @tanstack/query-persist-client-core@5.100.12
## 5.100.11
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.11
- @tanstack/query-persist-client-core@5.100.11
## 5.100.10
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.10
- @tanstack/query-persist-client-core@5.100.10
## 5.100.9
### Patch Changes
- Updated dependencies [[`fcee7bd`](https://github.com/TanStack/query/commit/fcee7bdc429385ae8ffa224fa8a7a9ec7b8ee380)]:
- @tanstack/query-core@5.100.9
- @tanstack/query-persist-client-core@5.100.9
## 5.100.8
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.8
- @tanstack/query-persist-client-core@5.100.8
## 5.100.7
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.7
- @tanstack/query-persist-client-core@5.100.7
## 5.100.6
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.6
- @tanstack/query-persist-client-core@5.100.6
## 5.100.5
### Patch Changes
- Updated dependencies [[`a53ef97`](https://github.com/TanStack/query/commit/a53ef97f87decb8ea2431710f5199431d3c94c8d)]:
- @tanstack/query-core@5.100.5
- @tanstack/query-persist-client-core@5.100.5
## 5.100.4
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.100.4
- @tanstack/query-persist-client-core@5.100.4
## 5.100.3
### Patch Changes
- Updated dependencies [[`f85d825`](https://github.com/TanStack/query/commit/f85d825e02efbbff02e2081528ed28f5e5382f7a)]:
- @tanstack/query-core@5.100.3
- @tanstack/query-persist-client-core@5.100.3
## 5.100.2
### Patch Changes
- Updated dependencies [[`ea4497e`](https://github.com/TanStack/query/commit/ea4497e8aa00d8c1c3a36fb1e17563a889d6ab31), [`d6a7bf3`](https://github.com/TanStack/query/commit/d6a7bf3e3e024c1a77d0536813238cc8007a5fa7), [`645d5d1`](https://github.com/TanStack/query/commit/645d5d130f5e8017cb1bf1a37987f7b980aed705)]:
- @tanstack/query-core@5.100.2
- @tanstack/query-persist-client-core@5.100.2
## 5.100.1
### Patch Changes
- Updated dependencies [[`1bb0d23`](https://github.com/TanStack/query/commit/1bb0d234280fd4ae1725c439088426a20593a8df)]:
- @tanstack/query-core@5.100.1
- @tanstack/query-persist-client-core@5.100.1
## 5.100.0
### Patch Changes
- Updated dependencies [[`6540a41`](https://github.com/TanStack/query/commit/6540a4126b1c087d86d64525e78f32d9920dcd31)]:
- @tanstack/query-core@5.100.0
- @tanstack/query-persist-client-core@5.100.0
## 5.99.2
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.99.2
- @tanstack/query-persist-client-core@5.99.2
## 5.99.1
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.99.1
- @tanstack/query-persist-client-core@5.99.1
## 5.99.0
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.99.0
- @tanstack/query-persist-client-core@5.99.0
## 5.98.0
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.98.0
- @tanstack/query-persist-client-core@5.98.0
## 5.97.0
### Patch Changes
- Updated dependencies [[`2bfb12c`](https://github.com/TanStack/query/commit/2bfb12cc44f1d8495106136e4ddacb817135f8f9)]:
- @tanstack/query-core@5.97.0
- @tanstack/query-persist-client-core@5.97.0
## 5.96.2
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.96.2
- @tanstack/query-persist-client-core@5.96.2
## 5.96.1
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.96.1
- @tanstack/query-persist-client-core@5.96.1
## 5.96.0
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.96.0
- @tanstack/query-persist-client-core@5.96.0
## 5.95.2
### Patch Changes
- Updated dependencies [[`cd5a35b`](https://github.com/TanStack/query/commit/cd5a35b328837781aa4f9305bb2bd7877ca934e9)]:
- @tanstack/query-core@5.95.2
- @tanstack/query-persist-client-core@5.95.2
## 5.95.1
### Patch Changes
- Updated dependencies [[`1f1775c`](https://github.com/TanStack/query/commit/1f1775ca92f2b6c035682947ff3b3424804ff31a)]:
- @tanstack/query-core@5.95.1
- @tanstack/query-persist-client-core@5.95.1
## 5.95.0
### Patch Changes
- Updated dependencies []:
- @tanstack/query-core@5.95.0
- @tanstack/query-persist-client-core@5.95.0
## 5.94.5
### Patch Changes
- fix(\*): resolve issue about excluded build directory ([#10312](https://github.com/TanStack/query/pull/10312))
- Updated dependencies [[`4b6536d`](https://github.com/TanStack/query/commit/4b6536dfce99036f4e37f52943c6fed3ad0e0a18)]:
- @tanstack/query-core@5.94.5
- @tanstack/query-persist-client-core@5.94.5
## 5.94.4
### Patch Changes
- chore: fixed version ([#10064](https://github.com/TanStack/query/pull/10064))
- Updated dependencies [[`4c75210`](https://github.com/TanStack/query/commit/4c75210ce8235fe3d39b67e1029eff11278927cc)]:
- @tanstack/query-core@5.94.4
- @tanstack/query-persist-client-core@5.94.4
## 5.90.27
### Patch Changes
- fix(streamedQuery): maintain error state on reset refetch with initialData defined ([#10287](https://github.com/TanStack/query/pull/10287))
- Updated dependencies [[`248975e`](https://github.com/TanStack/query/commit/248975e896f585f6eaa505c796e73fcf7bfd1eec)]:
- @tanstack/query-persist-client-core@5.92.4
- @tanstack/query-core@5.91.2
## 5.90.26
### Patch Changes
- Updated dependencies [[`a89aab9`](https://github.com/TanStack/query/commit/a89aab975581c25c113a26c8af486b4cafad272a)]:
- @tanstack/query-core@5.91.1
- @tanstack/query-persist-client-core@5.92.3
## 5.90.25
### Patch Changes
- Updated dependencies [[`6fa901b`](https://github.com/TanStack/query/commit/6fa901b97a22a80d0fca3f6ed86237ff0cbdd13b)]:
- @tanstack/query-core@5.91.0
- @tanstack/query-persist-client-core@5.92.2
## 5.90.24
### Patch Changes
- Updated dependencies [[`e505568`](https://github.com/TanStack/query/commit/e505568f4d51c8281d38e9687091094b7d32a405)]:
- @tanstack/query-persist-client-core@5.92.1
## 5.90.23
### Patch Changes
- Updated dependencies [[`978fc52`](https://github.com/TanStack/query/commit/978fc52728a8b9eb33f0a82f4ddf42a95815bd7f)]:
- @tanstack/query-persist-client-core@5.92.0
## 5.90.22
### Patch Changes
- Updated dependencies [[`e7258c5`](https://github.com/TanStack/query/commit/e7258c5cb30cafa456cdb4e6bc75b43bf619954d)]:
- @tanstack/query-core@5.90.20
- @tanstack/query-persist-client-core@5.91.19
## 5.90.21
### Patch Changes
- Updated dependencies [[`53fc74e`](https://github.com/TanStack/query/commit/53fc74ebb16730bd3317f039a69c6821386bae93)]:
- @tanstack/query-core@5.90.19
- @tanstack/query-persist-client-core@5.91.18
## 5.90.20
### Patch Changes
- Updated dependencies [[`dea1614`](https://github.com/TanStack/query/commit/dea1614aaad5c572cf43cea54b64ac09dc4d5b41)]:
- @tanstack/query-core@5.90.18
- @tanstack/query-persist-client-core@5.91.17
## 5.90.19
### Patch Changes
- Updated dependencies [[`269351b`](https://github.com/TanStack/query/commit/269351b8ce4b4846da3d320ac5b850ee6aada0d6)]:
- @tanstack/query-core@5.90.17
- @tanstack/query-persist-client-core@5.91.16
## 5.90.18
### Patch Changes
- Updated dependencies [[`7f47906`](https://github.com/TanStack/query/commit/7f47906eaccc3f3aa5ce24b77a83bd7a620a237b)]:
- @tanstack/query-core@5.90.16
- @tanstack/query-persist-client-core@5.91.15
## 5.90.17
### Patch Changes
- Updated dependencies [[`fccef79`](https://github.com/TanStack/query/commit/fccef797d57d4a9566517bba87c8377f363920f2)]:
- @tanstack/query-core@5.90.15
- @tanstack/query-persist-client-core@5.91.14
## 5.90.16
### Patch Changes
- Updated dependencies [[`d576092`](https://github.com/TanStack/query/commit/d576092e2ece4ca3936add3eb0da5234c1d82ed4)]:
- @tanstack/query-core@5.90.14
- @tanstack/query-persist-client-core@5.91.13
## 5.90.15
### Patch Changes
- Updated dependencies [[`4a0a78a`](https://github.com/TanStack/query/commit/4a0a78afbc2432f8cb6828035965853fa98c86a0)]:
- @tanstack/query-core@5.90.13
- @tanstack/query-persist-client-core@5.91.12
## 5.90.14
### Patch Changes
- Updated dependencies [[`72d8ac5`](https://github.com/TanStack/query/commit/72d8ac5c592004b8f9c3ee086fcb9c3cd615ca05)]:
- @tanstack/query-core@5.90.12
- @tanstack/query-persist-client-core@5.91.11
## 5.90.13
### Patch Changes
- Updated dependencies [[`c01b150`](https://github.com/TanStack/query/commit/c01b150e3673e11d6533768529a5e6fe3ebee68c)]:
- @tanstack/query-core@5.90.11
- @tanstack/query-persist-client-core@5.91.10
## 5.90.12
### Patch Changes
- Updated dependencies [[`8e2e174`](https://github.com/TanStack/query/commit/8e2e174e9fd2e7b94cd232041e49c9d014d74e26), [`eb559a6`](https://github.com/TanStack/query/commit/eb559a66dc0d77dd46435f624fa64fc068bef9ae)]:
- @tanstack/query-core@5.90.10
- @tanstack/query-persist-client-core@5.91.9
## 5.90.11
### Patch Changes
- Updated dependencies [[`08b211f`](https://github.com/TanStack/query/commit/08b211f8aa475e05d2f13a36517fc556861ef962)]:
- @tanstack/query-core@5.90.9
- @tanstack/query-persist-client-core@5.91.8
## 5.90.10
### Patch Changes
- Updated dependencies [[`c0ec9fe`](https://github.com/TanStack/query/commit/c0ec9fe0d1426fe3f233adda3ebf23989ffaa110)]:
- @tanstack/query-core@5.90.8
- @tanstack/query-persist-client-core@5.91.7
## 5.90.9
### Patch Changes
- Updated dependencies [[`b4cd121`](https://github.com/TanStack/query/commit/b4cd121a39d07cefaa3a3411136d342cc54ce8fb)]:
- @tanstack/query-core@5.90.7
- @tanstack/query-persist-client-core@5.91.6
## 5.90.8
### Patch Changes
- Updated dependencies [[`1638c02`](https://github.com/TanStack/query/commit/1638c028df55648995d04431179904371a189772)]:
- @tanstack/query-core@5.90.6
- @tanstack/query-persist-client-core@5.91.5
## 5.90.7
### Patch Changes
- Updated dependencies [[`e42ddfe`](https://github.com/TanStack/query/commit/e42ddfe919f34f847ca101aeef162c69845f9a1e)]:
- @tanstack/query-core@5.90.5
- @tanstack/query-persist-client-core@5.91.4
## 5.90.6
### Patch Changes
- Updated dependencies [[`20ef922`](https://github.com/TanStack/query/commit/20ef922a0a7c3aee00150bf69123c338b0922922)]:
- @tanstack/query-core@5.90.4
- @tanstack/query-persist-client-core@5.91.3
## 5.90.5
### Patch Changes
- Updated dependencies [[`4e1c433`](https://github.com/TanStack/query/commit/4e1c4338a72f7384600bbda99e44bc1891695df4)]:
- @tanstack/query-core@5.90.3
- @tanstack/query-persist-client-core@5.91.2
## 5.90.4
### Patch Changes
- Updated dependencies [[`846d53d`](https://github.com/TanStack/query/commit/846d53d98992d50606c40634efa43dea9965b787)]:
- @tanstack/query-persist-client-core@5.91.1
## 5.90.3
### Patch Changes
- Updated dependencies [[`5cd86c6`](https://github.com/TanStack/query/commit/5cd86c6ef1720b87b13e1ab70ee823616f1f029a)]:
- @tanstack/query-persist-client-core@5.91.0

View File

@@ -0,0 +1,5 @@
// @ts-check
import rootConfig from './root.eslint.config.js'
export default [...rootConfig]

View File

@@ -0,0 +1,66 @@
{
"name": "@tanstack/query-sync-storage-persister",
"version": "5.102.8",
"description": "A persister for synchronous storages, to be used with TanStack/Query",
"author": "tannerlinsley",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/TanStack/query.git",
"directory": "packages/query-sync-storage-persister"
},
"homepage": "https://tanstack.com/query",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"scripts": {
"clean": "premove ./build ./coverage ./dist-ts ./.cache/test-types",
"compile": "tsc --project tsconfig.json",
"test:eslint": "eslint --concurrency=auto ./src",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js --project tsconfig.json --outDir .cache/test-types/ts56 --tsBuildInfoFile .cache/test-types/ts56.tsbuildinfo",
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --project tsconfig.json --outDir .cache/test-types/ts57 --tsBuildInfoFile .cache/test-types/ts57.tsbuildinfo",
"test:types:ts58": "node ../../node_modules/typescript58/lib/tsc.js --project tsconfig.json --outDir .cache/test-types/ts58 --tsBuildInfoFile .cache/test-types/ts58.tsbuildinfo",
"test:types:ts59": "node ../../node_modules/typescript59/lib/tsc.js --project tsconfig.json --outDir .cache/test-types/ts59 --tsBuildInfoFile .cache/test-types/ts59.tsbuildinfo",
"test:types:tscurrent": "tsc --project tsconfig.json --outDir .cache/test-types/tscurrent --tsBuildInfoFile .cache/test-types/tscurrent.tsbuildinfo",
"test:types:ts70": "node ../../node_modules/typescript70/lib/tsc.js --project tsconfig.json --outDir .cache/test-types/ts70 --tsBuildInfoFile .cache/test-types/ts70.tsbuildinfo",
"test:lib": "vitest",
"test:lib:dev": "pnpm run test:lib --watch",
"test:build": "publint --strict && attw --pack",
"build": "tsdown --tsconfig tsconfig.prod.json"
},
"type": "module",
"types": "build/legacy/index.d.ts",
"main": "build/legacy/index.cjs",
"module": "build/legacy/index.js",
"react-native": "src/index.ts",
"exports": {
".": {
"@tanstack/custom-condition": "./src/index.ts",
"import": {
"types": "./build/modern/index.d.ts",
"default": "./build/modern/index.js"
},
"require": {
"types": "./build/modern/index.d.cts",
"default": "./build/modern/index.cjs"
}
},
"./package.json": "./package.json"
},
"sideEffects": false,
"files": [
"build",
"src",
"!src/__tests__"
],
"dependencies": {
"@tanstack/query-core": "workspace:*",
"@tanstack/query-persist-client-core": "workspace:*"
},
"devDependencies": {
"@tanstack/query-test-utils": "workspace:*",
"npm-run-all2": "^5.0.0"
}
}

View File

@@ -0,0 +1 @@
../../eslint.config.js

View File

@@ -0,0 +1 @@
../../scripts/getTsdownConfig.js

View File

@@ -0,0 +1,217 @@
import { describe, expect, it } from 'vitest'
import {
MutationCache,
QueryCache,
QueryClient,
dehydrate,
noop,
} from '@tanstack/query-core'
import { removeOldestQuery } from '@tanstack/query-persist-client-core'
import { sleep } from '@tanstack/query-test-utils'
import { createSyncStoragePersister } from '../index'
function getMockStorage(limitSize?: number) {
const dataSet = new Map<string, string>()
return {
getItem(key: string): string | null {
const value = dataSet.get(key)
return value === undefined ? null : value
},
setItem(key: string, value: string) {
if (limitSize !== undefined) {
const currentSize = Array.from(dataSet.entries()).reduce(
(n, d) => d[0].length + d[1].length + n,
0,
)
if (
currentSize - (dataSet.get(key)?.length || 0) + value.length >
limitSize
) {
throw Error(
`Failed to execute 'setItem' on 'Storage': Setting the value of '${key}' exceeded the quota.`,
)
}
}
return dataSet.set(key, value)
},
removeItem(key: string) {
return dataSet.delete(key)
},
} as any as Storage
}
describe('create persister', () => {
it('basic store and recover', async () => {
const queryCache = new QueryCache()
const mutationCache = new MutationCache()
const queryClient = new QueryClient({ queryCache, mutationCache })
const storage = getMockStorage()
const persister = createSyncStoragePersister({
throttleTime: 0,
storage,
})
await queryClient
.query({
queryKey: ['string'],
queryFn: () => Promise.resolve('string'),
})
.catch(noop)
await queryClient
.query({
queryKey: ['number'],
queryFn: () => Promise.resolve(1),
})
.catch(noop)
await queryClient
.query({
queryKey: ['boolean'],
queryFn: () => Promise.resolve(true),
})
.catch(noop)
await queryClient
.query({
queryKey: ['null'],
queryFn: () => Promise.resolve(null),
})
.catch(noop)
await queryClient
.query({
queryKey: ['array'],
queryFn: () => Promise.resolve(['string', 0]),
})
.catch(noop)
const persistClient = {
buster: 'test-buster',
timestamp: Date.now(),
clientState: dehydrate(queryClient),
}
persister.persistClient(persistClient)
await sleep(1)
const restoredClient = await persister.restoreClient()
expect(restoredClient).toEqual(persistClient)
})
it('should clean the old queries when storage full', async () => {
const queryCache = new QueryCache()
const mutationCache = new MutationCache()
const queryClient = new QueryClient({ queryCache, mutationCache })
const N = 2000
const storage = getMockStorage(N * 5) // can save 4 items;
const persister = createSyncStoragePersister({
throttleTime: 0,
storage,
retry: removeOldestQuery,
})
await queryClient
.query({
queryKey: ['A'],
queryFn: () => Promise.resolve('A'.repeat(N)),
})
.catch(noop)
await sleep(1)
await queryClient
.query({
queryKey: ['B'],
queryFn: () => Promise.resolve('B'.repeat(N)),
})
.catch(noop)
await sleep(1)
await queryClient
.query({
queryKey: ['C'],
queryFn: () => Promise.resolve('C'.repeat(N)),
})
.catch(noop)
await sleep(1)
await queryClient
.query({
queryKey: ['D'],
queryFn: () => Promise.resolve('D'.repeat(N)),
})
.catch(noop)
await sleep(1)
await queryClient
.query({
queryKey: ['E'],
queryFn: () => Promise.resolve('E'.repeat(N)),
})
.catch(noop)
const persistClient = {
buster: 'test-limit',
timestamp: Date.now(),
clientState: dehydrate(queryClient),
}
persister.persistClient(persistClient)
await sleep(10)
const restoredClient = await persister.restoreClient()
expect(restoredClient?.clientState.queries.length).toEqual(4)
expect(
restoredClient?.clientState.queries.find((q) => q.queryKey[0] === 'A'),
).toBeUndefined()
expect(
restoredClient?.clientState.queries.find((q) => q.queryKey[0] === 'B'),
).not.toBeUndefined()
// update query Data
await queryClient
.query({
queryKey: ['A'],
queryFn: () => Promise.resolve('a'.repeat(N)),
})
.catch(noop)
const updatedPersistClient = {
buster: 'test-limit',
timestamp: Date.now(),
clientState: dehydrate(queryClient),
}
persister.persistClient(updatedPersistClient)
await sleep(10)
const restoredClient2 = await persister.restoreClient()
expect(restoredClient2?.clientState.queries.length).toEqual(4)
expect(
restoredClient2?.clientState.queries.find((q) => q.queryKey[0] === 'A'),
).toHaveProperty('state.data', 'a'.repeat(N))
expect(
restoredClient2?.clientState.queries.find((q) => q.queryKey[0] === 'B'),
).toBeUndefined()
})
it('should clear storage as default error handling', async () => {
const queryCache = new QueryCache()
const mutationCache = new MutationCache()
const queryClient = new QueryClient({ queryCache, mutationCache })
const N = 2000
const storage = getMockStorage(0)
const persister = createSyncStoragePersister({
throttleTime: 0,
storage,
retry: removeOldestQuery,
})
await queryClient
.query({
queryKey: ['A'],
queryFn: () => Promise.resolve('A'.repeat(N)),
})
.catch(noop)
await sleep(1)
const persistClient = {
buster: 'test-limit',
timestamp: Date.now(),
clientState: dehydrate(queryClient),
}
persister.persistClient(persistClient)
await sleep(10)
const restoredClient = await persister.restoreClient()
expect(restoredClient).toEqual(undefined)
})
})

View File

@@ -0,0 +1,116 @@
import { timeoutManager } from '@tanstack/query-core'
import { noop } from './utils'
import type { ManagedTimerId } from '@tanstack/query-core'
import type {
PersistRetryer,
PersistedClient,
Persister,
} from '@tanstack/query-persist-client-core'
interface Storage {
getItem: (key: string) => string | null
setItem: (key: string, value: string) => void
removeItem: (key: string) => void
}
interface CreateSyncStoragePersisterOptions {
/** The storage client used for setting and retrieving items from cache.
* For SSR pass in `undefined`. Note that window.localStorage can be
* `null` in Android WebViews depending on how they are configured.
*/
storage: Storage | undefined | null
/** The key to use when storing the cache */
key?: string
/** To avoid spamming,
* pass a time in ms to throttle saving the cache to disk */
throttleTime?: number
/**
* How to serialize the data to storage.
* @default `JSON.stringify`
*/
serialize?: (client: PersistedClient) => string
/**
* How to deserialize the data from storage.
* @default `JSON.parse`
*/
deserialize?: (cachedString: string) => PersistedClient
retry?: PersistRetryer
}
/**
* @deprecated use `createAsyncStoragePersister` from `@tanstack/query-async-storage-persister` instead.
*/
export function createSyncStoragePersister({
storage,
key = `REACT_QUERY_OFFLINE_CACHE`,
throttleTime = 1000,
serialize = JSON.stringify,
deserialize = JSON.parse,
retry,
}: CreateSyncStoragePersisterOptions): Persister {
if (storage) {
const trySave = (persistedClient: PersistedClient): Error | undefined => {
try {
storage.setItem(key, serialize(persistedClient))
return
} catch (error) {
return error as Error
}
}
return {
persistClient: throttle((persistedClient) => {
let client: PersistedClient | undefined = persistedClient
let error = trySave(client)
let errorCount = 0
while (error && client) {
errorCount++
client = retry?.({
persistedClient: client,
error,
errorCount,
})
if (client) {
error = trySave(client)
}
}
}, throttleTime),
restoreClient: () => {
const cacheString = storage.getItem(key)
if (!cacheString) {
return
}
return deserialize(cacheString)
},
removeClient: () => {
storage.removeItem(key)
},
}
}
return {
persistClient: noop,
restoreClient: noop,
removeClient: noop,
}
}
function throttle<TArgs extends Array<any>>(
func: (...args: TArgs) => any,
wait = 100,
) {
let timer: ManagedTimerId | null = null
let params: TArgs
return function (...args: TArgs) {
params = args
if (timer === null) {
timer = timeoutManager.setTimeout(() => {
func(...params)
timer = null
}, wait)
}
}
}

View File

@@ -0,0 +1,3 @@
export function noop(): void
export function noop(): undefined
export function noop() {}

View File

@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist-ts",
"rootDir": "."
},
"include": ["src", "*.config.ts", "*.config.js", "package.json"],
"references": [
{ "path": "../query-core" },
{ "path": "../query-persist-client-core" }
]
}

View File

@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"incremental": false,
"composite": false,
"rootDir": "../../"
},
"include": ["src"],
"exclude": ["src/__tests__"]
}

View File

@@ -0,0 +1,7 @@
import { defineConfig } from 'tsdown'
import { legacyConfig, modernConfig } from './root.tsdown.config.js'
export default defineConfig([
modernConfig({ entry: ['src/*.ts'] }),
legacyConfig({ entry: ['src/*.ts'] }),
])

View File

@@ -0,0 +1,30 @@
import { defineConfig } from 'vitest/config'
import packageJson from './package.json'
export default defineConfig({
// fix from https://github.com/vitest-dev/vitest/issues/6992#issuecomment-2509408660
resolve: {
conditions: ['@tanstack/custom-condition'],
},
environments: {
ssr: {
resolve: {
conditions: ['@tanstack/custom-condition'],
},
},
},
test: {
name: packageJson.name,
dir: './src',
watch: false,
coverage: {
enabled: !!process.env.CI,
provider: 'istanbul',
include: ['src/**/*'],
exclude: ['src/__tests__/**'],
},
typecheck: { enabled: true },
restoreMocks: true,
},
})