@solidjs/router 0.13.0 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,9 @@
1
1
  import { CacheEntry } from "../types.js";
2
2
  export declare function revalidate(key?: string | string[] | void, force?: boolean): Promise<void>;
3
3
  export declare function cacheKeyOp(key: string | string[] | void, fn: (cacheEntry: CacheEntry) => void): void;
4
- export type CachedFunction<T extends (...args: any) => any> = T extends (...args: infer A) => infer R ? ([] extends A ? (...args: never[]) => R : T) & {
4
+ export type CachedFunction<T extends (...args: any) => any> = T extends (...args: infer A) => infer R ? ([] extends {
5
+ [K in keyof A]-?: A[K];
6
+ } ? (...args: never[]) => R : T) & {
5
7
  keyFor: (...args: A) => string;
6
8
  key: string;
7
9
  } : never;
@@ -83,6 +83,8 @@ export function setupNativeEvents(preload = true, explicitLinks = false, actionB
83
83
  }
84
84
  }
85
85
  function handleFormSubmit(evt) {
86
+ if (evt.defaultPrevented)
87
+ return;
86
88
  let actionRef = evt.submitter && evt.submitter.hasAttribute("formaction")
87
89
  ? evt.submitter.getAttribute("formaction")
88
90
  : evt.target.getAttribute("action");
package/dist/index.js CHANGED
@@ -256,7 +256,7 @@ const useMatch = (path, matchFilters) => {
256
256
  }
257
257
  });
258
258
  };
259
- const useParams = () => useRoute().params;
259
+ const useParams = () => useRouter().params;
260
260
  const useSearchParams = () => {
261
261
  const location = useLocation();
262
262
  const navigate = useNavigate();
@@ -292,9 +292,9 @@ function createRoutes(routeDef, base = "") {
292
292
  load,
293
293
  info
294
294
  };
295
- return asArray(routeDef.path).reduce((acc, path) => {
296
- for (const originalPath of expandOptionals(path)) {
297
- const path = joinPaths(base, originalPath);
295
+ return asArray(routeDef.path).reduce((acc, originalPath) => {
296
+ for (const expandedPath of expandOptionals(originalPath)) {
297
+ const path = joinPaths(base, expandedPath);
298
298
  let pattern = isLeaf ? path : path.split("/*", 1)[0];
299
299
  pattern = pattern.split("/").map(s => {
300
300
  return s.startsWith(":") || s.startsWith("*") ? s : encodeURIComponent(s);
@@ -406,7 +406,7 @@ let intent;
406
406
  function getIntent() {
407
407
  return intent;
408
408
  }
409
- function createRouterContext(integration, getContext, getBranches, options = {}) {
409
+ function createRouterContext(integration, branches, getContext, options = {}) {
410
410
  const {
411
411
  signal: [source, setSource],
412
412
  utils = {}
@@ -438,9 +438,17 @@ function createRouterContext(integration, getContext, getBranches, options = {})
438
438
  const location = createLocation(reference, state);
439
439
  const referrers = [];
440
440
  const submissions = createSignal(isServer ? initFromFlash() : []);
441
+ const matches = createMemo(() => getRouteMatches(branches(), location.pathname));
442
+ const params = createMemoObject(() => {
443
+ const m = matches();
444
+ const params = {};
445
+ for (let i = 0; i < m.length; i++) {
446
+ Object.assign(params, m[i].params);
447
+ }
448
+ return params;
449
+ });
441
450
  const baseRoute = {
442
451
  pattern: basePath,
443
- params: {},
444
452
  path: () => basePath,
445
453
  outlet: () => null,
446
454
  resolvePath(to) {
@@ -470,10 +478,12 @@ function createRouterContext(integration, getContext, getBranches, options = {})
470
478
  return {
471
479
  base: baseRoute,
472
480
  location,
481
+ params,
473
482
  isRouting,
474
483
  renderPath,
475
484
  parsePath,
476
485
  navigatorFactory,
486
+ matches,
477
487
  beforeLeave,
478
488
  preloadRoute,
479
489
  singleFlight: options.singleFlight === undefined ? true : options.singleFlight,
@@ -568,7 +578,7 @@ function createRouterContext(integration, getContext, getBranches, options = {})
568
578
  }
569
579
  }
570
580
  function preloadRoute(url, preloadData) {
571
- const matches = getRouteMatches(getBranches(), url.pathname);
581
+ const matches = getRouteMatches(branches(), url.pathname);
572
582
  const prevIntent = intent;
573
583
  intent = "preload";
574
584
  for (let match in matches) {
@@ -600,10 +610,11 @@ function createRouterContext(integration, getContext, getBranches, options = {})
600
610
  return e && e.router && e.router.submission ? [e.router.submission] : [];
601
611
  }
602
612
  }
603
- function createRouteContext(router, parent, outlet, match, params) {
613
+ function createRouteContext(router, parent, outlet, match) {
604
614
  const {
605
615
  base,
606
- location
616
+ location,
617
+ params
607
618
  } = router;
608
619
  const {
609
620
  pattern,
@@ -621,7 +632,6 @@ function createRouteContext(router, parent, outlet, match, params) {
621
632
  parent,
622
633
  pattern,
623
634
  path,
624
- params,
625
635
  outlet: () => component ? createComponent(component, {
626
636
  params,
627
637
  location,
@@ -644,17 +654,16 @@ const createRouterComponent = router => props => {
644
654
  const routeDefs = children(() => props.children);
645
655
  const branches = createMemo(() => createBranches(routeDefs(), props.base || ""));
646
656
  let context;
647
- const routerState = createRouterContext(router, () => context, branches, {
657
+ const routerState = createRouterContext(router, branches, () => context, {
648
658
  base,
649
659
  singleFlight: props.singleFlight
650
660
  });
651
- const location = routerState.location;
652
661
  router.create && router.create(routerState);
653
662
  return createComponent$1(RouterContextObj.Provider, {
654
663
  value: routerState,
655
664
  get children() {
656
665
  return createComponent$1(Root, {
657
- location: location,
666
+ routerState: routerState,
658
667
  get root() {
659
668
  return props.root;
660
669
  },
@@ -674,9 +683,10 @@ const createRouterComponent = router => props => {
674
683
  });
675
684
  };
676
685
  function Root(props) {
677
- const location = props.location;
686
+ const location = props.routerState.location;
687
+ const params = props.routerState.params;
678
688
  const data = createMemo(() => props.load && untrack(() => props.load({
679
- params: {},
689
+ params,
680
690
  location,
681
691
  intent: "preload"
682
692
  })));
@@ -689,7 +699,7 @@ function Root(props) {
689
699
  return props.children;
690
700
  },
691
701
  children: Root => createComponent$1(Root, {
692
- params: {},
702
+ params: params,
693
703
  location: location,
694
704
  get data() {
695
705
  return data();
@@ -701,14 +711,13 @@ function Root(props) {
701
711
  });
702
712
  }
703
713
  function Routes(props) {
704
- const matches = createMemo(() => getRouteMatches(props.branches, props.routerState.location.pathname));
705
714
  if (isServer) {
706
715
  const e = getRequestEvent();
707
716
  if (e && e.router && e.router.dataOnly) {
708
717
  dataOnly(e, props.routerState, props.branches);
709
718
  return;
710
719
  }
711
- e && ((e.router || (e.router = {})).matches || (e.router.matches = matches().map(({
720
+ e && ((e.router || (e.router = {})).matches || (e.router.matches = props.routerState.matches().map(({
712
721
  route,
713
722
  path,
714
723
  params
@@ -720,17 +729,9 @@ function Routes(props) {
720
729
  info: route.info
721
730
  }))));
722
731
  }
723
- const params = createMemoObject(() => {
724
- const m = matches();
725
- const params = {};
726
- for (let i = 0; i < m.length; i++) {
727
- Object.assign(params, m[i].params);
728
- }
729
- return params;
730
- });
731
732
  const disposers = [];
732
733
  let root;
733
- const routeStates = createMemo(on(matches, (nextMatches, prevMatches, prev) => {
734
+ const routeStates = createMemo(on(props.routerState.matches, (nextMatches, prevMatches, prev) => {
734
735
  let equal = prevMatches && nextMatches.length === prevMatches.length;
735
736
  const next = [];
736
737
  for (let i = 0, len = nextMatches.length; i < len; i++) {
@@ -745,7 +746,7 @@ function Routes(props) {
745
746
  }
746
747
  createRoot(dispose => {
747
748
  disposers[i] = dispose;
748
- next[i] = createRouteContext(props.routerState, next[i - 1] || props.routerState.base, createOutlet(() => routeStates()[i + 1]), () => matches()[i], params);
749
+ next[i] = createRouteContext(props.routerState, next[i - 1] || props.routerState.base, createOutlet(() => routeStates()[i + 1]), () => props.routerState.matches()[i]);
749
750
  });
750
751
  }
751
752
  }
@@ -1230,6 +1231,7 @@ function setupNativeEvents(preload = true, explicitLinks = false, actionBase = "
1230
1231
  }
1231
1232
  }
1232
1233
  function handleFormSubmit(evt) {
1234
+ if (evt.defaultPrevented) return;
1233
1235
  let actionRef = evt.submitter && evt.submitter.hasAttribute("formaction") ? evt.submitter.getAttribute("formaction") : evt.target.getAttribute("action");
1234
1236
  if (!actionRef) return;
1235
1237
  if (!actionRef.startsWith("https://action/")) {
@@ -1381,9 +1383,12 @@ function createMemoryHistory() {
1381
1383
  entries.splice(index + 1, entries.length - index, value);
1382
1384
  index++;
1383
1385
  }
1384
- if (scroll) {
1385
- scrollToHash(value.split("#")[1] || "", true);
1386
- }
1386
+ listeners.forEach(listener => listener(value));
1387
+ setTimeout(() => {
1388
+ if (scroll) {
1389
+ scrollToHash(value.split("#")[1] || "", true);
1390
+ }
1391
+ }, 0);
1387
1392
  },
1388
1393
  back: () => {
1389
1394
  go(-1);
@@ -20,9 +20,12 @@ export function createMemoryHistory() {
20
20
  entries.splice(index + 1, entries.length - index, value);
21
21
  index++;
22
22
  }
23
- if (scroll) {
24
- scrollToHash(value.split("#")[1] || "", true);
25
- }
23
+ listeners.forEach(listener => listener(value));
24
+ setTimeout(() => {
25
+ if (scroll) {
26
+ scrollToHash(value.split("#")[1] || "", true);
27
+ }
28
+ }, 0);
26
29
  },
27
30
  back: () => {
28
31
  go(-1);
@@ -2,36 +2,34 @@
2
2
  import { getRequestEvent, isServer } from "solid-js/web";
3
3
  import { children, createMemo, createRoot, getOwner, mergeProps, on, Show, untrack } from "solid-js";
4
4
  import { createBranches, createRouteContext, createRouterContext, getRouteMatches, RouteContextObj, RouterContextObj } from "../routing.js";
5
- import { createMemoObject } from "../utils.js";
6
5
  export const createRouterComponent = (router) => (props) => {
7
6
  const { base } = props;
8
7
  const routeDefs = children(() => props.children);
9
8
  const branches = createMemo(() => createBranches(routeDefs(), props.base || ""));
10
9
  let context;
11
- const routerState = createRouterContext(router, () => context, branches, {
10
+ const routerState = createRouterContext(router, branches, () => context, {
12
11
  base,
13
12
  singleFlight: props.singleFlight
14
13
  });
15
- const location = routerState.location;
16
14
  router.create && router.create(routerState);
17
15
  return (<RouterContextObj.Provider value={routerState}>
18
- <Root location={location} root={props.root} load={props.rootLoad}>
16
+ <Root routerState={routerState} root={props.root} load={props.rootLoad}>
19
17
  {(context = getOwner()) && null}
20
18
  <Routes routerState={routerState} branches={branches()}/>
21
19
  </Root>
22
20
  </RouterContextObj.Provider>);
23
21
  };
24
22
  function Root(props) {
25
- const location = props.location;
26
- const data = createMemo(() => props.load && untrack(() => props.load({ params: {}, location, intent: "preload" })));
23
+ const location = props.routerState.location;
24
+ const params = props.routerState.params;
25
+ const data = createMemo(() => props.load && untrack(() => props.load({ params, location, intent: "preload" })));
27
26
  return (<Show when={props.root} keyed fallback={props.children}>
28
- {Root => (<Root params={{}} location={location} data={data()}>
27
+ {Root => (<Root params={params} location={location} data={data()}>
29
28
  {props.children}
30
29
  </Root>)}
31
30
  </Show>);
32
31
  }
33
32
  function Routes(props) {
34
- const matches = createMemo(() => getRouteMatches(props.branches, props.routerState.location.pathname));
35
33
  if (isServer) {
36
34
  const e = getRequestEvent();
37
35
  if (e && e.router && e.router.dataOnly) {
@@ -40,7 +38,7 @@ function Routes(props) {
40
38
  }
41
39
  e &&
42
40
  ((e.router || (e.router = {})).matches ||
43
- (e.router.matches = matches().map(({ route, path, params }) => ({
41
+ (e.router.matches = props.routerState.matches().map(({ route, path, params }) => ({
44
42
  path: route.originalPath,
45
43
  pattern: route.pattern,
46
44
  match: path,
@@ -48,17 +46,9 @@ function Routes(props) {
48
46
  info: route.info
49
47
  }))));
50
48
  }
51
- const params = createMemoObject(() => {
52
- const m = matches();
53
- const params = {};
54
- for (let i = 0; i < m.length; i++) {
55
- Object.assign(params, m[i].params);
56
- }
57
- return params;
58
- });
59
49
  const disposers = [];
60
50
  let root;
61
- const routeStates = createMemo(on(matches, (nextMatches, prevMatches, prev) => {
51
+ const routeStates = createMemo(on(props.routerState.matches, (nextMatches, prevMatches, prev) => {
62
52
  let equal = prevMatches && nextMatches.length === prevMatches.length;
63
53
  const next = [];
64
54
  for (let i = 0, len = nextMatches.length; i < len; i++) {
@@ -74,7 +64,7 @@ function Routes(props) {
74
64
  }
75
65
  createRoot(dispose => {
76
66
  disposers[i] = dispose;
77
- next[i] = createRouteContext(props.routerState, next[i - 1] || props.routerState.base, createOutlet(() => routeStates()[i + 1]), () => matches()[i], params);
67
+ next[i] = createRouteContext(props.routerState, next[i - 1] || props.routerState.base, createOutlet(() => routeStates()[i + 1]), () => props.routerState.matches()[i]);
78
68
  });
79
69
  }
80
70
  }
package/dist/routing.d.ts CHANGED
@@ -19,8 +19,8 @@ export declare function createBranches(routeDef: RouteDefinition | RouteDefiniti
19
19
  export declare function getRouteMatches(branches: Branch[], location: string): RouteMatch[];
20
20
  export declare function createLocation(path: Accessor<string>, state: Accessor<any>): Location;
21
21
  export declare function getIntent(): Intent | undefined;
22
- export declare function createRouterContext(integration: RouterIntegration, getContext?: () => any, getBranches?: () => Branch[], options?: {
22
+ export declare function createRouterContext(integration: RouterIntegration, branches: () => Branch[], getContext?: () => any, options?: {
23
23
  base?: string;
24
24
  singleFlight?: boolean;
25
25
  }): RouterContext;
26
- export declare function createRouteContext(router: RouterContext, parent: RouteContext, outlet: () => JSX.Element, match: () => RouteMatch, params: Params): RouteContext;
26
+ export declare function createRouteContext(router: RouterContext, parent: RouteContext, outlet: () => JSX.Element, match: () => RouteMatch): RouteContext;
package/dist/routing.js CHANGED
@@ -34,7 +34,7 @@ export const useMatch = (path, matchFilters) => {
34
34
  }
35
35
  });
36
36
  };
37
- export const useParams = () => useRoute().params;
37
+ export const useParams = () => useRouter().params;
38
38
  export const useSearchParams = () => {
39
39
  const location = useLocation();
40
40
  const navigate = useNavigate();
@@ -65,9 +65,9 @@ export function createRoutes(routeDef, base = "") {
65
65
  load,
66
66
  info
67
67
  };
68
- return asArray(routeDef.path).reduce((acc, path) => {
69
- for (const originalPath of expandOptionals(path)) {
70
- const path = joinPaths(base, originalPath);
68
+ return asArray(routeDef.path).reduce((acc, originalPath) => {
69
+ for (const expandedPath of expandOptionals(originalPath)) {
70
+ const path = joinPaths(base, expandedPath);
71
71
  let pattern = isLeaf ? path : path.split("/*", 1)[0];
72
72
  pattern = pattern
73
73
  .split("/")
@@ -184,7 +184,7 @@ let intent;
184
184
  export function getIntent() {
185
185
  return intent;
186
186
  }
187
- export function createRouterContext(integration, getContext, getBranches, options = {}) {
187
+ export function createRouterContext(integration, branches, getContext, options = {}) {
188
188
  const { signal: [source, setSource], utils = {} } = integration;
189
189
  const parsePath = utils.parsePath || (p => p);
190
190
  const renderPath = utils.renderPath || (p => p);
@@ -211,9 +211,17 @@ export function createRouterContext(integration, getContext, getBranches, option
211
211
  const location = createLocation(reference, state);
212
212
  const referrers = [];
213
213
  const submissions = createSignal(isServer ? initFromFlash() : []);
214
+ const matches = createMemo(() => getRouteMatches(branches(), location.pathname));
215
+ const params = createMemoObject(() => {
216
+ const m = matches();
217
+ const params = {};
218
+ for (let i = 0; i < m.length; i++) {
219
+ Object.assign(params, m[i].params);
220
+ }
221
+ return params;
222
+ });
214
223
  const baseRoute = {
215
224
  pattern: basePath,
216
- params: {},
217
225
  path: () => basePath,
218
226
  outlet: () => null,
219
227
  resolvePath(to) {
@@ -240,10 +248,12 @@ export function createRouterContext(integration, getContext, getBranches, option
240
248
  return {
241
249
  base: baseRoute,
242
250
  location,
251
+ params,
243
252
  isRouting,
244
253
  renderPath,
245
254
  parsePath,
246
255
  navigatorFactory,
256
+ matches,
247
257
  beforeLeave,
248
258
  preloadRoute,
249
259
  singleFlight: options.singleFlight === undefined ? true : options.singleFlight,
@@ -324,7 +334,7 @@ export function createRouterContext(integration, getContext, getBranches, option
324
334
  }
325
335
  }
326
336
  function preloadRoute(url, preloadData) {
327
- const matches = getRouteMatches(getBranches(), url.pathname);
337
+ const matches = getRouteMatches(branches(), url.pathname);
328
338
  const prevIntent = intent;
329
339
  intent = "preload";
330
340
  for (let match in matches) {
@@ -357,8 +367,8 @@ export function createRouterContext(integration, getContext, getBranches, option
357
367
  : []);
358
368
  }
359
369
  }
360
- export function createRouteContext(router, parent, outlet, match, params) {
361
- const { base, location } = router;
370
+ export function createRouteContext(router, parent, outlet, match) {
371
+ const { base, location, params } = router;
362
372
  const { pattern, component, load } = match().route;
363
373
  const path = createMemo(() => match().path);
364
374
  component &&
@@ -369,7 +379,6 @@ export function createRouteContext(router, parent, outlet, match, params) {
369
379
  parent,
370
380
  pattern,
371
381
  path,
372
- params,
373
382
  outlet: () => component
374
383
  ? createComponent(component, {
375
384
  params,
package/dist/types.d.ts CHANGED
@@ -114,7 +114,6 @@ export interface RouteContext {
114
114
  parent?: RouteContext;
115
115
  child?: RouteContext;
116
116
  pattern: string;
117
- params: Params;
118
117
  path: () => string;
119
118
  outlet: () => JSX.Element;
120
119
  resolvePath(to: string): string | undefined;
@@ -128,8 +127,10 @@ export interface RouterUtils {
128
127
  export interface RouterContext {
129
128
  base: RouteContext;
130
129
  location: Location;
130
+ params: Params;
131
131
  navigatorFactory: NavigatorFactory;
132
132
  isRouting: () => boolean;
133
+ matches: () => RouteMatch[];
133
134
  renderPath(path: string): string;
134
135
  parsePath(str: string): string;
135
136
  beforeLeave: BeforeLeaveLifecycle;
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "Ryan Turnquist"
7
7
  ],
8
8
  "license": "MIT",
9
- "version": "0.13.0",
9
+ "version": "0.13.2",
10
10
  "homepage": "https://github.com/solidjs/solid-router#readme",
11
11
  "repository": {
12
12
  "type": "git",