Comparing version
@@ -604,9 +604,10 @@ import React, { useEffect, useLayoutEffect, createContext, useContext, useMemo, useRef, createElement } from 'react'; | ||
const [, , , PRELOAD] = SWRGlobalState.get(cache); | ||
let normalizedKey = key; | ||
if (key.startsWith(INFINITE_PREFIX)) { | ||
normalizedKey = key.slice(INFINITE_PREFIX.length); | ||
// we want the infinite fetcher to be called. | ||
// handling of the PRELOAD cache happens there. | ||
return fetcher_(...args); | ||
} | ||
const req = PRELOAD[normalizedKey]; | ||
const req = PRELOAD[key]; | ||
if (isUndefined(req)) return fetcher_(...args); | ||
delete PRELOAD[normalizedKey]; | ||
delete PRELOAD[key]; | ||
return req; | ||
@@ -613,0 +614,0 @@ }); |
@@ -610,9 +610,10 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
const [, , , PRELOAD] = SWRGlobalState.get(cache); | ||
let normalizedKey = key; | ||
if (key.startsWith(INFINITE_PREFIX)) { | ||
normalizedKey = key.slice(INFINITE_PREFIX.length); | ||
// we want the infinite fetcher to be called. | ||
// handling of the PRELOAD cache happens there. | ||
return fetcher_(...args); | ||
} | ||
const req = PRELOAD[normalizedKey]; | ||
const req = PRELOAD[key]; | ||
if (isUndefined(req)) return fetcher_(...args); | ||
delete PRELOAD[normalizedKey]; | ||
delete PRELOAD[key]; | ||
return req; | ||
@@ -619,0 +620,0 @@ }); |
import { useRef, useCallback } from 'react'; | ||
import useSWR from 'swr'; | ||
import { INFINITE_PREFIX, serialize, withMiddleware, createCacheHelper, isUndefined, useIsomorphicLayoutEffect, UNDEFINED, isFunction } from 'swr/_internal'; | ||
import { INFINITE_PREFIX, serialize, withMiddleware, SWRGlobalState, cache, createCacheHelper, isUndefined, useIsomorphicLayoutEffect, UNDEFINED, isFunction } from 'swr/_internal'; | ||
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'; | ||
@@ -21,3 +21,4 @@ | ||
const didMountRef = useRef(false); | ||
const { cache, initialSize = 1, revalidateAll = false, persistSize = false, revalidateFirstPage = true, revalidateOnMount = false, parallel = false } = config; | ||
const { cache: cache$1, initialSize = 1, revalidateAll = false, persistSize = false, revalidateFirstPage = true, revalidateOnMount = false, parallel = false } = config; | ||
const [, , , PRELOAD] = SWRGlobalState.get(cache); | ||
// The serialized key of the first page. This key will be used to store | ||
@@ -32,3 +33,3 @@ // metadata of this SWR infinite hook. | ||
} | ||
const [get, set, subscribeCache] = createCacheHelper(cache, infiniteKey); | ||
const [get, set, subscribeCache] = createCacheHelper(cache$1, infiniteKey); | ||
const getSnapshot = useCallback(()=>{ | ||
@@ -39,3 +40,3 @@ const size = isUndefined(get()._l) ? initialSize : get()._l; | ||
}, [ | ||
cache, | ||
cache$1, | ||
infiniteKey, | ||
@@ -51,3 +52,3 @@ initialSize | ||
[ | ||
cache, | ||
cache$1, | ||
infiniteKey | ||
@@ -83,3 +84,3 @@ ]), getSnapshot, getSnapshot); | ||
infiniteKey, | ||
cache | ||
cache$1 | ||
]); | ||
@@ -95,3 +96,3 @@ // Needs to check didMountRef during mounting, not in the fetcher | ||
const pageSize = resolvePageSize(); | ||
const [getCache] = createCacheHelper(cache, key); | ||
const [getCache] = createCacheHelper(cache$1, key); | ||
const cacheData = getCache().data; | ||
@@ -105,5 +106,20 @@ const revalidators = []; | ||
} | ||
const [getSWRCache, setSWRCache] = createCacheHelper(cache, pageKey); | ||
const [getSWRCache, setSWRCache] = createCacheHelper(cache$1, pageKey); | ||
const hasPreloadedRequest = pageKey in PRELOAD; | ||
// Get the cached page data. | ||
let pageData = getSWRCache().data; | ||
if (hasPreloadedRequest) { | ||
const req = PRELOAD[pageKey]; | ||
// delete the preload cache key before resolving it | ||
// in case there's an error | ||
delete PRELOAD[pageKey]; | ||
// get the page data from the preload cache | ||
pageData = await req; | ||
// set the SWR cache with the preloaded data | ||
setSWRCache({ | ||
data: pageData, | ||
_k: pageArg | ||
}); | ||
// remove the preload cache key to prevent memory leak | ||
} | ||
// should fetch (or revalidate) if: | ||
@@ -181,3 +197,3 @@ // - `revalidateAll` is enabled | ||
infiniteKey, | ||
cache | ||
cache$1 | ||
]); | ||
@@ -188,3 +204,3 @@ // Extend the SWR API | ||
if (!infiniteKey) return EMPTY_PROMISE; | ||
const [, changeSize] = createCacheHelper(cache, infiniteKey); | ||
const [, changeSize] = createCacheHelper(cache$1, infiniteKey); | ||
let size; | ||
@@ -203,7 +219,7 @@ if (isFunction(arg)) { | ||
const data = []; | ||
const [getInfiniteCache] = createCacheHelper(cache, infiniteKey); | ||
const [getInfiniteCache] = createCacheHelper(cache$1, infiniteKey); | ||
let previousPageData = null; | ||
for(let i = 0; i < size; ++i){ | ||
const [pageKey] = serialize(getKey(i, previousPageData)); | ||
const [getCache] = createCacheHelper(cache, pageKey); | ||
const [getCache] = createCacheHelper(cache$1, pageKey); | ||
// Get the cached page data. | ||
@@ -223,3 +239,3 @@ const pageData = pageKey ? getCache().data : UNDEFINED; | ||
infiniteKey, | ||
cache, | ||
cache$1, | ||
mutate, | ||
@@ -226,0 +242,0 @@ resolvePageSize |
@@ -28,2 +28,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
const { cache, initialSize = 1, revalidateAll = false, persistSize = false, revalidateFirstPage = true, revalidateOnMount = false, parallel = false } = config; | ||
const [, , , PRELOAD] = _internal.SWRGlobalState.get(_internal.cache); | ||
// The serialized key of the first page. This key will be used to store | ||
@@ -107,4 +108,19 @@ // metadata of this SWR infinite hook. | ||
const [getSWRCache, setSWRCache] = _internal.createCacheHelper(cache, pageKey); | ||
const hasPreloadedRequest = pageKey in PRELOAD; | ||
// Get the cached page data. | ||
let pageData = getSWRCache().data; | ||
if (hasPreloadedRequest) { | ||
const req = PRELOAD[pageKey]; | ||
// delete the preload cache key before resolving it | ||
// in case there's an error | ||
delete PRELOAD[pageKey]; | ||
// get the page data from the preload cache | ||
pageData = await req; | ||
// set the SWR cache with the preloaded data | ||
setSWRCache({ | ||
data: pageData, | ||
_k: pageArg | ||
}); | ||
// remove the preload cache key to prevent memory leak | ||
} | ||
// should fetch (or revalidate) if: | ||
@@ -111,0 +127,0 @@ // - `revalidateAll` is enabled |
{ | ||
"name": "swr", | ||
"version": "2.2.1-beta.1", | ||
"version": "2.2.1-beta.2", | ||
"description": "React Hooks library for remote data fetching", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
298767
0.87%5928
0.87%