Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to ignore caching server-side result #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AbanoubGhadban
Copy link
Contributor

It adds the ability to ignore caching the value returned from the server-side. It can be used if the server-side result is not ready, so the client will need to recalculate it

Example

import { ignoreCache } from 'use-ssr-computation.runtime';
function fetchUsers() {

if (UsersStore.cached) return { loading: false, users: UsersStore.all };
return ignoreCache({ loading: true, users: UsersStore.fetchAll() });

}

@AbanoubGhadban AbanoubGhadban requested a review from Romex91 July 28, 2023 08:45
@@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this line before to deal with promises. But, I don't know if server-side renderer will support executing async functions. Should I remove it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the coding principles I follow is "Every single line of code should be tested". If you don't have proof that the code works, don't merge it.

So I'm for reverting (unless you tested it and found it useful).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Romex91 I don't add lines here, I delete them. I removed the ability to track async results on the server-side. This feature is already not been tested before. So, I think it's a mistake from me to add it before.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. So I approve the reversion.

Copy link
Collaborator

@Romex91 Romex91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's necessary (more on it in Slack)
If I'm wrong, there are issues:

cache[cacheKey] = result;

// check if it should ignore caching
if (result && result[IgnoreCache]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want this feature, this has to be duplicated client-side, as it will return different stuff client-side and server-side, resulting in crashes and hydration conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants