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

fix: do not mutate data return by useSuspenseData when csr #6434

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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/kind-singers-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/runtime': patch
---

fix: side effect of update suspense data may cause mulit time call
62 changes: 40 additions & 22 deletions packages/runtime/src/Suspense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,37 @@ export function useSuspenseData(request?: Request) {
const suspenseState = React.useContext(SuspenseContext);

const { data, done, promise, update, error, id } = suspenseState;
const hasHydrateData = isClient && (window[LOADER] as Map<string, any>) && window[LOADER].has(id);
ClarkXia marked this conversation as resolved.
Show resolved Hide resolved

let thenable: Promise<any> = null;
if (!hasHydrateData && !error && !done && !promise && request) {
thenable = request(requestContext);
thenable.then((response) => {
update({
done: true,
data: response,
promise: null,
});
}).catch(e => {
update({
done: true,
error: e,
promise: null,
});
});
}

React.useEffect(() => {
// Update state in useEffect, otherwise it will cause bad setState warning.
if (thenable) {
update({
promise: thenable,
});
}
}, [thenable, update]);

// 1. Use data from server side directly when hydrate.
if (isClient && (window[LOADER] as Map<string, any>) && window[LOADER].has(id)) {
if (hasHydrateData) {
return window[LOADER].get(id);
}

Expand Down Expand Up @@ -56,27 +84,12 @@ export function useSuspenseData(request?: Request) {
return null;
}

// 6. Create a promise for the request and throw it to react.
const thenable = request(requestContext);

thenable.then((response) => {
update({
done: true,
data: response,
promise: null,
});
}).catch(e => {
if (!isClient) {
// 6. Create a promise for the request and throw it to react.
update({
done: true,
error: e,
promise: null,
promise: thenable,
});
});

update({
promise: thenable,
});

}
throw thenable;
}

Expand All @@ -100,8 +113,13 @@ export function withSuspense(Component) {
});

function update(value) {
// For SSR, setState is not working, so here we need to update the state manually.
const newState = Object.assign(suspenseState, value);
let newState: any;
if (isClient) {
newState = Object.assign({}, suspenseState, value);
} else {
// For SSR, setState is not working, so here we need to update the state manually.
newState = Object.assign(suspenseState, value);
}
// For CSR.
updateSuspenseData(newState);
}
Expand Down
Loading