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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/runtime/src/index.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { SSRCacheProvider } from './SSRCacheProvider';
export { default as getSSRComputation } from './getSSRComputation';
export { ignoreCache } from './utils';
12 changes: 7 additions & 5 deletions packages/runtime/src/useSSRComputation_Server.tsx
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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.

} else {
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.

return result.result;
}

cache[cacheKey] = result;
}

return result;
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}