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(HMR):update client routes by HMR #472

Merged
merged 2 commits into from
May 17, 2022
Merged
Changes from 1 commit
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
30 changes: 29 additions & 1 deletion framework/react/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FetchError from "../core/fetch_error.ts";
import { redirect } from "../core/redirect.ts";
import type { Route, RouteMeta, RouteModule, RouteRecord } from "../core/route.ts";
import { matchRoutes } from "../core/route.ts";
import { URLPatternCompat } from "../core/url_pattern.ts";
import { URLPatternCompat, URLPatternInput } from "../core/url_pattern.ts";
import { ForwardPropsContext, RouterContext, type RouterContextProps } from "./context.ts";
import { DataProvider, type RouteData } from "./data.ts";
import { Err, ErrorBoundary } from "./error.ts";
Expand Down Expand Up @@ -200,11 +200,39 @@ export const Router: FC<RouterProps> = ({ ssrContext, suspense, createPortal })
events.emit("routerready", { type: "routerready" });

// todo: update routes by hmr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the todo comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you are already fixed it

const oncreate = (e: Record<string, unknown>) => {
const route: Route = [
new URLPatternCompat(e.routePattern as URLPatternInput),
{
filename: e.specifier as string,
pattern: e.routePattern as URLPatternInput,
},
];
const pathname = (e.routePattern as URLPatternInput).pathname.slice(1);
if (pathname == "_app" || pathname == "_404" || pathname == "_error") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better use "===" instead of "=="

routeRecord[pathname] = route;
}
routeRecord.routes.push(route);
onpopstate({ type: "popstate" });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont call the onpopstate in oncreate callback, call it in the onremove

};
events.on("hmr:create", oncreate);

const onremove = (e: Record<string, unknown>) => {
const route = routeRecord.routes.find((v) => v[1].filename == e.specifier);
const pathname = (route?.[1].pattern.pathname)?.slice(1);
if (pathname == "_app" || pathname == "_404" || pathname == "_error") {
routeRecord[pathname] = undefined;
}
routeRecord.routes = routeRecord.routes.filter((v) => v[1].filename != e.specifier);
};
events.on("hmr:remove", onremove);

return () => {
removeEventListener("popstate", onpopstate as unknown as EventListener);
events.off("popstate", onpopstate);
events.off("moduleprefetch", onmoduleprefetch);
events.off("hmr:create", oncreate);
events.off("hmr:remove", onremove);
};
}, []);

Expand Down