@solidjs/router 0.5.1 → 0.6.0

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.
@@ -10,7 +10,7 @@ declare module "solid-js" {
10
10
  }
11
11
  }
12
12
  }
13
- export declare type RouterProps = {
13
+ export type RouterProps = {
14
14
  base?: string;
15
15
  data?: RouteDataFunc;
16
16
  children: JSX.Element;
@@ -29,7 +29,7 @@ export interface RoutesProps {
29
29
  }
30
30
  export declare const Routes: (props: RoutesProps) => JSX.Element;
31
31
  export declare const useRoutes: (routes: RouteDefinition | RouteDefinition[], base?: string) => () => JSX.Element;
32
- export declare type RouteProps = {
32
+ export type RouteProps = {
33
33
  path: string | string[];
34
34
  children?: JSX.Element;
35
35
  data?: RouteDataFunc;
package/dist/index.js CHANGED
@@ -185,9 +185,6 @@ function extractSearchParams(url) {
185
185
  });
186
186
  return params;
187
187
  }
188
- function urlDecode(str, isQuery) {
189
- return decodeURIComponent(isQuery ? str.replace(/\+/g, " ") : str);
190
- }
191
188
  function createMatcher(path, partial) {
192
189
  const [pattern, splat] = path.split("/*", 2);
193
190
  const segments = pattern.split("/").filter(Boolean);
@@ -299,8 +296,13 @@ const useLocation = () => useRouter().location;
299
296
  const useIsRouting = () => useRouter().isRouting;
300
297
  const useMatch = path => {
301
298
  const location = useLocation();
302
- const matcher = createMemo(() => createMatcher(path()));
303
- return createMemo(() => matcher()(location.pathname));
299
+ const matchers = createMemo(() => expandOptionals(path()).map(path => createMatcher(path)));
300
+ return createMemo(() => {
301
+ for (const matcher of matchers()) {
302
+ const match = matcher(location.pathname);
303
+ if (match) return match;
304
+ }
305
+ });
304
306
  };
305
307
  const useParams = () => useRoute().params;
306
308
  const useRouteData = () => useRoute().data;
@@ -426,9 +428,9 @@ function createLocation(path, state) {
426
428
  }, origin, {
427
429
  equals: (a, b) => a.href === b.href
428
430
  });
429
- const pathname = createMemo(() => urlDecode(url().pathname));
430
- const search = createMemo(() => urlDecode(url().search, true));
431
- const hash = createMemo(() => urlDecode(url().hash));
431
+ const pathname = createMemo(() => url().pathname);
432
+ const search = createMemo(() => url().search, true);
433
+ const hash = createMemo(() => url().hash);
432
434
  const key = createMemo(() => "");
433
435
  return {
434
436
  get pathname() {
@@ -612,8 +614,7 @@ function createRouterContext(integration, base = "", data, out) {
612
614
  const rel = (a.getAttribute("rel") || "").split(/\s+/);
613
615
  if (a.hasAttribute("download") || rel && rel.includes("external")) return;
614
616
  const url = new URL(href);
615
- const pathname = urlDecode(url.pathname);
616
- if (url.origin !== window.location.origin || basePath && pathname && !pathname.toLowerCase().startsWith(basePath.toLowerCase())) return;
617
+ if (url.origin !== window.location.origin || basePath && url.pathname && !url.pathname.toLowerCase().startsWith(basePath.toLowerCase())) return;
617
618
  const to = parsePath(url.pathname + url.search + url.hash);
618
619
  const state = a.getAttribute("state");
619
620
  evt.preventDefault();
package/dist/routing.d.ts CHANGED
@@ -9,9 +9,9 @@ export declare const useHref: (to: () => string | undefined) => Accessor<string
9
9
  export declare const useNavigate: () => Navigator;
10
10
  export declare const useLocation: <S = unknown>() => Location<S>;
11
11
  export declare const useIsRouting: () => () => boolean;
12
- export declare const useMatch: (path: () => string) => Accessor<import("./types").PathMatch | null>;
12
+ export declare const useMatch: (path: () => string) => Accessor<import("./types").PathMatch | undefined>;
13
13
  export declare const useParams: <T extends Params>() => T;
14
- declare type MaybeReturnType<T> = T extends (...args: any) => infer R ? R : T;
14
+ type MaybeReturnType<T> = T extends (...args: any) => infer R ? R : T;
15
15
  export declare const useRouteData: <T>() => MaybeReturnType<T>;
16
16
  export declare const useSearchParams: <T extends Params>() => [T, (params: SetParams, options?: Partial<NavigateOptions>) => void];
17
17
  export declare const useBeforeLeave: (listener: (e: BeforeLeaveEventArgs) => void) => void;
package/dist/routing.js CHANGED
@@ -2,7 +2,7 @@ import { createComponent, createContext, createMemo, createRenderEffect, createS
2
2
  import { isServer, delegateEvents } from "solid-js/web";
3
3
  import { normalizeIntegration } from "./integration";
4
4
  import { createBeforeLeave } from "./lifecycle";
5
- import { createMemoObject, extractSearchParams, invariant, resolvePath, createMatcher, joinPaths, scoreRoute, mergeSearchString, urlDecode, expandOptionals } from "./utils";
5
+ import { createMemoObject, extractSearchParams, invariant, resolvePath, createMatcher, joinPaths, scoreRoute, mergeSearchString, expandOptionals } from "./utils";
6
6
  const MAX_REDIRECTS = 100;
7
7
  export const RouterContextObj = createContext();
8
8
  export const RouteContextObj = createContext();
@@ -25,8 +25,14 @@ export const useLocation = () => useRouter().location;
25
25
  export const useIsRouting = () => useRouter().isRouting;
26
26
  export const useMatch = (path) => {
27
27
  const location = useLocation();
28
- const matcher = createMemo(() => createMatcher(path()));
29
- return createMemo(() => matcher()(location.pathname));
28
+ const matchers = createMemo(() => expandOptionals(path()).map((path) => createMatcher(path)));
29
+ return createMemo(() => {
30
+ for (const matcher of matchers()) {
31
+ const match = matcher(location.pathname);
32
+ if (match)
33
+ return match;
34
+ }
35
+ });
30
36
  };
31
37
  export const useParams = () => useRoute().params;
32
38
  export const useRouteData = () => useRoute().data;
@@ -145,9 +151,9 @@ export function createLocation(path, state) {
145
151
  }, origin, {
146
152
  equals: (a, b) => a.href === b.href
147
153
  });
148
- const pathname = createMemo(() => urlDecode(url().pathname));
149
- const search = createMemo(() => urlDecode(url().search, true));
150
- const hash = createMemo(() => urlDecode(url().hash));
154
+ const pathname = createMemo(() => url().pathname);
155
+ const search = createMemo(() => url().search, true);
156
+ const hash = createMemo(() => url().hash);
151
157
  const key = createMemo(() => "");
152
158
  return {
153
159
  get pathname() {
@@ -328,9 +334,8 @@ export function createRouterContext(integration, base = "", data, out) {
328
334
  if (a.hasAttribute("download") || (rel && rel.includes("external")))
329
335
  return;
330
336
  const url = new URL(href);
331
- const pathname = urlDecode(url.pathname);
332
337
  if (url.origin !== window.location.origin ||
333
- (basePath && pathname && !pathname.toLowerCase().startsWith(basePath.toLowerCase())))
338
+ (basePath && url.pathname && !url.pathname.toLowerCase().startsWith(basePath.toLowerCase())))
334
339
  return;
335
340
  const to = parsePath(url.pathname + url.search + url.hash);
336
341
  const state = a.getAttribute("state");
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Component, JSX } from "solid-js";
2
- export declare type Params = Record<string, string>;
3
- export declare type SetParams = Record<string, string | number | boolean | null | undefined>;
2
+ export type Params = Record<string, string>;
3
+ export type SetParams = Record<string, string | number | boolean | null | undefined>;
4
4
  export interface Path {
5
5
  pathname: string;
6
6
  search: string;
@@ -21,14 +21,14 @@ export interface Navigator {
21
21
  (to: string, options?: Partial<NavigateOptions>): void;
22
22
  (delta: number): void;
23
23
  }
24
- export declare type NavigatorFactory = (route?: RouteContext) => Navigator;
24
+ export type NavigatorFactory = (route?: RouteContext) => Navigator;
25
25
  export interface LocationChange<S = unknown> {
26
26
  value: string;
27
27
  replace?: boolean;
28
28
  scroll?: boolean;
29
29
  state?: S;
30
30
  }
31
- export declare type LocationChangeSignal = [() => LocationChange, (next: LocationChange) => void];
31
+ export type LocationChangeSignal = [() => LocationChange, (next: LocationChange) => void];
32
32
  export interface RouterIntegration {
33
33
  signal: LocationChangeSignal;
34
34
  utils?: Partial<RouterUtils>;
@@ -39,8 +39,8 @@ export interface RouteDataFuncArgs<T = unknown> {
39
39
  location: Location;
40
40
  navigate: Navigator;
41
41
  }
42
- export declare type RouteDataFunc<T = unknown, R = unknown> = (args: RouteDataFuncArgs<T>) => R;
43
- export declare type RouteDefinition = {
42
+ export type RouteDataFunc<T = unknown, R = unknown> = (args: RouteDataFuncArgs<T>) => R;
43
+ export type RouteDefinition = {
44
44
  path: string | string[];
45
45
  data?: RouteDataFunc;
46
46
  children?: RouteDefinition | RouteDefinition[];
package/dist/utils.d.ts CHANGED
@@ -4,7 +4,6 @@ export declare function resolvePath(base: string, path: string, from?: string):
4
4
  export declare function invariant<T>(value: T | null | undefined, message: string): T;
5
5
  export declare function joinPaths(from: string, to: string): string;
6
6
  export declare function extractSearchParams(url: URL): Params;
7
- export declare function urlDecode(str: string, isQuery?: boolean): string;
8
7
  export declare function createMatcher(path: string, partial?: boolean): (location: string) => PathMatch | null;
9
8
  export declare function scoreRoute(route: Route): number;
10
9
  export declare function createMemoObject<T extends Record<string | symbol, unknown>>(fn: () => T): T;
package/dist/utils.js CHANGED
@@ -39,9 +39,6 @@ export function extractSearchParams(url) {
39
39
  });
40
40
  return params;
41
41
  }
42
- export function urlDecode(str, isQuery) {
43
- return decodeURIComponent(isQuery ? str.replace(/\+/g, " ") : str);
44
- }
45
42
  export function createMatcher(path, partial) {
46
43
  const [pattern, splat] = path.split("/*", 2);
47
44
  const segments = pattern.split("/").filter(Boolean);
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "Ryan Turnquist"
7
7
  ],
8
8
  "license": "MIT",
9
- "version": "0.5.1",
9
+ "version": "0.6.0",
10
10
  "homepage": "https://github.com/solidjs/solid-router#readme",
11
11
  "repository": {
12
12
  "type": "git",
@@ -28,36 +28,36 @@
28
28
  "dist"
29
29
  ],
30
30
  "sideEffects": false,
31
- "scripts": {
32
- "build": "tsc && rollup -c",
33
- "prepublishOnly": "npm run build",
34
- "test": "jest && npm run test:types",
35
- "test:watch": "jest --watch",
36
- "test:coverage": "jest --coverage && npm run test:types",
37
- "test:types": "tsc --project tsconfig.test.json",
38
- "pretty": "prettier --write \"{src,test}/**/*.{ts,tsx}\""
39
- },
40
31
  "devDependencies": {
41
32
  "@babel/core": "^7.18.13",
42
33
  "@babel/preset-typescript": "^7.18.6",
43
- "@rollup/plugin-babel": "5.3.1",
44
- "@rollup/plugin-node-resolve": "13.3.0",
34
+ "@rollup/plugin-babel": "6.0.3",
35
+ "@rollup/plugin-node-resolve": "15.0.1",
36
+ "@rollup/plugin-terser": "0.2.0",
45
37
  "@types/jest": "^29.0.0",
46
38
  "@types/node": "^18.7.14",
47
- "babel-preset-solid": "^1.5.3",
39
+ "babel-jest": "^29.0.1",
40
+ "babel-preset-solid": "^1.6.6",
48
41
  "jest": "^29.0.1",
49
42
  "jest-environment-jsdom": "^29.2.1",
50
43
  "prettier": "^2.7.1",
51
- "rollup": "^2.79.0",
52
- "rollup-plugin-terser": "^7.0.2",
44
+ "rollup": "^3.7.5",
53
45
  "solid-jest": "^0.2.0",
54
- "solid-js": "^1.5.3",
55
- "typescript": "^4.8.2"
46
+ "solid-js": "^1.6.6",
47
+ "typescript": "^4.9.4"
56
48
  },
57
49
  "peerDependencies": {
58
50
  "solid-js": "^1.5.3"
59
51
  },
60
52
  "jest": {
61
53
  "preset": "solid-jest/preset/browser"
54
+ },
55
+ "scripts": {
56
+ "build": "tsc && rollup -c",
57
+ "test": "jest && npm run test:types",
58
+ "test:watch": "jest --watch",
59
+ "test:coverage": "jest --coverage && npm run test:types",
60
+ "test:types": "tsc --project tsconfig.test.json",
61
+ "pretty": "prettier --write \"{src,test}/**/*.{ts,tsx}\""
62
62
  }
63
- }
63
+ }