From 0922ad55835432a2c304b7361e053865a1ebdf89 Mon Sep 17 00:00:00 2001 From: Abanoub Ghadban Date: Fri, 28 Jul 2023 11:38:07 +0300 Subject: [PATCH] add the ability to ignore caching server-side result --- packages/runtime/src/index.runtime.ts | 1 + packages/runtime/src/useSSRComputation_Server.tsx | 12 +++++++----- packages/runtime/src/utils.ts | 9 +++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/runtime/src/index.runtime.ts b/packages/runtime/src/index.runtime.ts index 0ebde42..1043117 100644 --- a/packages/runtime/src/index.runtime.ts +++ b/packages/runtime/src/index.runtime.ts @@ -1,2 +1,3 @@ export { SSRCacheProvider } from './SSRCacheProvider'; export { default as getSSRComputation } from './getSSRComputation'; +export { ignoreCache } from './utils'; diff --git a/packages/runtime/src/useSSRComputation_Server.tsx b/packages/runtime/src/useSSRComputation_Server.tsx index ef12e55..d3bd3e5 100644 --- a/packages/runtime/src/useSSRComputation_Server.tsx +++ b/packages/runtime/src/useSSRComputation_Server.tsx @@ -1,5 +1,6 @@ import { useSSRCache } from "./SSRCacheProvider"; import { calculateCacheKey } from "./utils"; +import { IgnoreCache } from './utils'; export default function useSSRComputation_Server(fn: (...dependencies: any[]) => any, dependencies: any[], relativePathToCwd: string) { const cache = useSSRCache(); @@ -9,12 +10,13 @@ export default function useSSRComputation_Server(fn: (...dependencies: any[]) => // relativePathToCwd is used to make sure that the cache key is unique for each module // and it's not affected by the file that calls it const cacheKey = calculateCacheKey(relativePathToCwd, dependencies); - // check if result is a promise - if (result && typeof result.then === 'function') { - cache[cacheKey] = result.then(asyncResult => cache[cacheKey] = asyncResult); - } else { - cache[cacheKey] = result; + + // check if it should ignore caching + if (result && result[IgnoreCache]) { + return result.result; } + + cache[cacheKey] = result; } return result; diff --git a/packages/runtime/src/utils.ts b/packages/runtime/src/utils.ts index df2113e..02529c9 100644 --- a/packages/runtime/src/utils.ts +++ b/packages/runtime/src/utils.ts @@ -30,3 +30,12 @@ export const calculateCacheKey = (modulePath: string, dependencies: Dependency[] return `${modulePath}::${dependenciesString}`; } + +export const IgnoreCache = Symbol('IgnoreCache'); + +export const ignoreCache = (result: any) => { + return { + [IgnoreCache]: true, + result, + }; +}