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: make refetch async #736

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions examples/24_nesting/src/components/Counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export const Counter = ({ enableInnerApp }: { enableInnerApp?: boolean }) => {
const refetch = useRefetch();
const handleClick = () => {
if (enableInnerApp) {
startTransition(() => {
startTransition(async () => {
const nextCount = count + 1;
setCount(nextCount);
refetch('InnerApp=' + nextCount);
await refetch('InnerApp=' + nextCount);
});
} else {
setCount((c) => c + 1);
Expand Down
9 changes: 6 additions & 3 deletions packages/waku/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const prefetchRSC = (
};

const RefetchContext = createContext<
(input: string, searchParams?: URLSearchParams) => void
(input: string, searchParams?: URLSearchParams) => Promise<void>
>(() => {
throw new Error('Missing Root component');
});
Expand All @@ -184,11 +184,14 @@ export const Root = ({
fetchCache[SET_ELEMENTS] = setElements;
}, [fetchCache, setElements]);
const refetch = useCallback(
(input: string, searchParams?: URLSearchParams) => {
async (input: string, searchParams?: URLSearchParams) => {
// clear cache entry before fetching
delete fetchCache[ENTRY];
const data = fetchRSC(input, searchParams?.toString() || '', fetchCache);
setElements((prev) => mergeElements(prev, data));
startTransition(() => {
setElements((prev) => mergeElements(prev, data));
});
await data;
},
[fetchCache],
);
Expand Down
46 changes: 23 additions & 23 deletions packages/waku/src/router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type ChangeRoute = (
checkCache?: boolean;
skipRefetch?: boolean;
},
) => void;
) => void | Promise<void>;

type PrefetchRoute = (route: RouteProps) => void;

Expand All @@ -81,7 +81,7 @@ export function useRouter_UNSTABLE() {
}
const { route, changeRoute, prefetchRoute } = router;
const push = useCallback(
(to: string) => {
async (to: string) => {
const url = new URL(to, window.location.href);
window.history.pushState(
{
Expand All @@ -91,21 +91,21 @@ export function useRouter_UNSTABLE() {
'',
url,
);
changeRoute(parseRoute(url));
await changeRoute(parseRoute(url));
},
[changeRoute],
);
const replace = useCallback(
(to: string) => {
async (to: string) => {
const url = new URL(to, window.location.href);
window.history.replaceState(window.history.state, '', url);
changeRoute(parseRoute(url));
await changeRoute(parseRoute(url));
},
[changeRoute],
);
const reload = useCallback(() => {
const reload = useCallback(async () => {
const url = new URL(window.location.href);
changeRoute(parseRoute(url));
await changeRoute(parseRoute(url));
}, [changeRoute]);
const back = useCallback(() => {
// FIXME is this correct?
Expand Down Expand Up @@ -201,7 +201,7 @@ export function Link({
if (url.href !== window.location.href) {
const route = parseRoute(url);
prefetchRoute(route);
startTransition(() => {
startTransition(async () => {
window.history.pushState(
{
...window.history.state,
Expand All @@ -210,7 +210,7 @@ export function Link({
'',
url,
);
changeRoute(route);
await changeRoute(route);
});
}
props.onClick?.(event);
Expand Down Expand Up @@ -331,7 +331,7 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
}, [cached]);

const changeRoute: ChangeRoute = useCallback(
(route, options) => {
async (route, options) => {
const { checkCache, skipRefetch } = options || {};
setRoute(route);
const componentIds = getComponentIds(route.path);
Expand All @@ -355,15 +355,6 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
return; // everything is skipped
}
const input = getInputString(route.path);
if (!skipRefetch) {
refetch(
input,
new URLSearchParams([
...Array.from(route.searchParams.entries()),
...skip.map((id) => [PARAM_KEY_SKIP, id]),
]),
);
}
setCached((prev) => ({
...prev,
...Object.fromEntries(
Expand All @@ -372,6 +363,15 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
),
),
}));
if (!skipRefetch) {
await refetch(
input,
new URLSearchParams([
...Array.from(route.searchParams.entries()),
...skip.map((id) => [PARAM_KEY_SKIP, id]),
]),
);
}
},
[refetch, routerData],
);
Expand Down Expand Up @@ -401,9 +401,9 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
);

useEffect(() => {
const callback = () => {
const callback = async () => {
const route = parseRoute(new URL(window.location.href));
changeRoute(route, { checkCache: true });
await changeRoute(route, { checkCache: true });
};
window.addEventListener('popstate', callback);
return () => {
Expand All @@ -412,7 +412,7 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
}, [changeRoute]);

useEffect(() => {
const callback = (pathname: string, searchParamsString: string) => {
const callback = async (pathname: string, searchParamsString: string) => {
const url = new URL(window.location.href);
url.pathname = pathname;
url.search = searchParamsString;
Expand All @@ -425,7 +425,7 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
'',
url,
);
changeRoute(parseRoute(url), { skipRefetch: true });
await changeRoute(parseRoute(url), { skipRefetch: true });
};
const listeners = (routerData[1] ||= new Set());
listeners.add(callback);
Expand Down
Loading