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 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/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
82 changes: 59 additions & 23 deletions packages/runtime/src/Suspense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import type { RequestContext } from './types.js';
const LOADER = '__ICE_SUSPENSE_LOADER__';
const isClient = typeof window !== 'undefined' && 'onload' in window;

declare global {
interface Window {
[LOADER]: Map<string, any>;
}
}

interface SuspenseState {
id: string;
data?: any;
Expand All @@ -20,16 +26,56 @@ type Request = (ctx: RequestContext) => Promise<any>;

const SuspenseContext = React.createContext<SuspenseState | undefined>(undefined);

const getHydrateData = (id: string) => {
let data = null;
const hasHydrateData = isClient && window[LOADER] && window[LOADER].has(id);
if (hasHydrateData) {
data = window[LOADER].get(id);
}
return {
hasHydrateData,
data,
};
};

export function useSuspenseData(request?: Request) {
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);

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)) {
return window[LOADER].get(id);
if (hasHydrateData) {
return hydrateData;
}

// 2. Check data request error, if error throw it to react.
Expand All @@ -56,27 +102,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) => {
if (!isClient) {
// 6. Create a promise for the request and throw it to react.
update({
done: true,
data: response,
promise: null,
promise: thenable,
});
}).catch(e => {
update({
done: true,
error: e,
promise: null,
});
});

update({
promise: thenable,
});

}
throw thenable;
}

Expand All @@ -100,8 +131,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