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

Feat: avoid duplicate request when use suspense data in CSR #6934

Open
wants to merge 5 commits into
base: master
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
5 changes: 5 additions & 0 deletions .changeset/warm-bears-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/runtime': patch
---

feat: support options for API useSuspenseData
43 changes: 40 additions & 3 deletions packages/runtime/src/Suspense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,62 @@ const getHydrateData = (id: string) => {
};
};

export function useSuspenseData(request?: Request) {
class QueryClient {
queryCache: Map<string, Promise<any>>;

constructor() {
this.queryCache = new Map();
}

setQueryData(key: string, promise: Promise<any>) {
if (key) {
this.queryCache.set(key, promise);
}
}

getQueryData(key: string) {
return this.queryCache.get(key);
}

removeQueryData(key: string) {
this.queryCache.delete(key);
}
}

const queryClient = new QueryClient();

interface SuspenseDataProps {
queryKey?: string;
}

export function useSuspenseData(request?: Request, options?: SuspenseDataProps) {
const appContext = useAppContext();
const { requestContext } = appContext;
const suspenseState = React.useContext(SuspenseContext);

const { data, done, promise, update, error, id } = suspenseState;
const { hasHydrateData, data: hydrateData } = getHydrateData(id);

const queryInProcess = options?.queryKey && queryClient.getQueryData(options.queryKey);
let thenable: Promise<any> = null;
if (!hasHydrateData && !error && !done && !promise && request) {
if (!hasHydrateData && !error && !done && !promise && request && !queryInProcess) {
thenable = request(requestContext);
thenable.then((response) => {
queryClient.removeQueryData(options?.queryKey);
update({
done: true,
data: response,
promise: null,
});
}).catch(e => {
queryClient.removeQueryData(options?.queryKey);
update({
done: true,
error: e,
promise: null,
});
});
queryClient.setQueryData(options?.queryKey, thenable);
}

React.useEffect(() => {
Expand Down Expand Up @@ -117,7 +149,12 @@ export function useSuspenseData(request?: Request) {
promise: thenable,
});
}
throw thenable;
if (thenable) {
throw thenable;
} else {
// Throw a pending promise to Suspense, otherwise the component will be error because of undefined data.
throw (queryClient.getQueryData(options?.queryKey) || new Promise(() => {}));
}
}

interface SuspenseProps {
Expand Down
Loading