next
Version:
The React Framework
51 lines (50 loc) • 2.11 kB
JavaScript
import { stringifyFetchCacheStore, stringifyUseCacheCacheStore, parseUseCacheCacheStore, parseFetchCacheStore } from './cache-store';
/**
* Serializes a resume data cache into a JSON string for storage or transmission.
* Handles both 'use cache' values and fetch responses.
*
* @param resumeDataCache - The immutable cache to serialize
* @returns A Promise that resolves to the serialized cache as a JSON string, or 'null' if empty
*/ export async function stringifyResumeDataCache(resumeDataCache) {
if (resumeDataCache.fetch.size === 0 && resumeDataCache.cache.size === 0) {
return 'null';
}
const json = {
store: {
fetch: Object.fromEntries(stringifyFetchCacheStore(resumeDataCache.fetch.entries())),
cache: Object.fromEntries(await stringifyUseCacheCacheStore(resumeDataCache.cache.entries()))
}
};
return JSON.stringify(json);
}
/**
* Creates a new empty mutable resume data cache for pre-rendering.
* Initializes fresh Map instances for both the 'use cache' and fetch caches.
* Used at the start of pre-rendering to begin collecting cached values.
*
* @returns A new empty PrerenderResumeDataCache instance
*/ export function createPrerenderResumeDataCache() {
return {
cache: new Map(),
fetch: new Map()
};
}
export function createRenderResumeDataCache(prerenderResumeDataCacheOrPersistedCache) {
if (typeof prerenderResumeDataCacheOrPersistedCache !== 'string') {
// If the cache is already a prerender cache, we can return it directly,
// we're just performing a type change.
return prerenderResumeDataCacheOrPersistedCache;
}
if (prerenderResumeDataCacheOrPersistedCache === 'null') {
return {
cache: new Map(),
fetch: new Map()
};
}
const json = JSON.parse(prerenderResumeDataCacheOrPersistedCache);
return {
cache: parseUseCacheCacheStore(Object.entries(json.store.cache)),
fetch: parseFetchCacheStore(Object.entries(json.store.fetch))
};
}
//# sourceMappingURL=resume-data-cache.js.map