Skip to content

Commit

Permalink
fix preloadRoute to take string path
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Oct 14, 2024
1 parent 6dd0473 commit a22d7d2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-seahorses-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

fix preloadRoute to take string path
4 changes: 2 additions & 2 deletions src/data/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function setupNativeEvents(
if (!res) return;
const [a, url] = res;
transformUrl && (url.pathname = transformUrl(url.pathname));
router.preloadRoute(url, { preloadData: a.getAttribute("preload") !== "false" });
router.preloadRoute(url, a.getAttribute("preload") !== "false");
}

function handleAnchorMove(evt: Event) {
Expand All @@ -89,7 +89,7 @@ export function setupNativeEvents(
if (lastElement === a) return;
transformUrl && (url.pathname = transformUrl(url.pathname));
preloadTimeout = setTimeout(() => {
router.preloadRoute(url, { preloadData: a.getAttribute("preload") !== "false" });
router.preloadRoute(url, a.getAttribute("preload") !== "false");
lastElement = a;
}, 20) as any;
}
Expand Down
10 changes: 7 additions & 3 deletions src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export const useHref = (to: () => string | undefined) => {
export const useNavigate = () => useRouter().navigatorFactory();
export const useLocation = <S = unknown>() => useRouter().location as Location<S>;
export const useIsRouting = () => useRouter().isRouting;
export const usePreloadRoute = () => useRouter().preloadRoute;
export const usePreloadRoute = () => {
const pre = useRouter().preloadRoute
return (url: string | URL, options: { preloadData?: boolean } = {} ) =>
pre(url instanceof URL ? url : new URL(url, mockBase), options.preloadData)
}

export const useMatch = <S extends string>(path: () => S, matchFilters?: MatchFilters<S>) => {
const location = useLocation();
Expand Down Expand Up @@ -470,7 +474,7 @@ export function createRouterContext(
}
}

function preloadRoute(url: URL, options: { preloadData?: boolean } = {}) {
function preloadRoute(url: URL, preloadData?: boolean) {
const matches = getRouteMatches(branches(), url.pathname);
const prevIntent = intent;
intent = "preload";
Expand All @@ -481,7 +485,7 @@ export function createRouterContext(
(route.component as MaybePreloadableComponent).preload!();
const { preload } = route;
inPreloadFn = true;
options.preloadData &&
preloadData &&
preload &&
runWithOwner(getContext!(), () =>
preload({
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export interface RouterContext {
renderPath(path: string): string;
parsePath(str: string): string;
beforeLeave: BeforeLeaveLifecycle;
preloadRoute: (url: URL, options: { preloadData?: boolean }) => void;
preloadRoute: (url: URL, preloadData?: boolean) => void;
singleFlight: boolean;
submissions: Signal<Submission<any, any>[]>;
}
Expand Down

0 comments on commit a22d7d2

Please sign in to comment.