alfonsusac / nextjs-better-unstable-cache

Wrapper function for unstable_cache() from next/cache (Next.js Caching)
https://www.npmjs.com/package/nextjs-better-unstable-cache
72 stars 5 forks source link

allow cross platform usages #7

Closed asp3 closed 7 months ago

alfonsusac commented 8 months ago

Interesting. I will look up to this next week

asp3 commented 7 months ago

Interesting. I will look up to this next week

Thanks @alfonsusac! added one more commit - the function was failing if it ever got called on client side (since no cache available), so added one more condition.

alfonsusac commented 7 months ago

Interesting implementation @asp3 . Though is it okay to let user fallback to default function even if the entire usage of memoize() itself was only in the server?

Using it in the client would be outside of this function's intended usage and therefore, imo, should return an error saying it can only be used in the server / can't be used in client/browser environment.

asp3 commented 7 months ago
export const callFetchX = memoize(
    async () => {
        return client
            .query({
                query: fetchX,
                variables: { input },
            })
            .then(({ data }) => data.X.items)
            .catch(async e => {
                const { report } = await platform.analytics.logging();
                report(e, "fetchX");
            });
    },
    {
        persist: true,
        duration: TIME_SECONDS.DAY,
        revalidateTags: () => ["fetch-X"],
        log: ["dedupe", "datacache"],
        logid: "fetch-X",
    }
);

@alfonsusac thanks for the reply! so for our implementation, we have a lot of code thats shared between server and client. so this function, for example, would get called from server so that we can statically/incrementally render certain page X, which will help for SEO. but this same function is also called from client side, which will need to bypass the cache. This function is also used in our react native app as well, which is the reason for those 3 conditions here, rather than throwing an error.