@solidjs/router 0.15.2 → 0.15.3

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.
@@ -100,9 +100,14 @@ export function query(fn, name) {
100
100
  inPreloadFn && "then" in res && res.catch(() => { });
101
101
  return res;
102
102
  }
103
- let res = !isServer && sharedConfig.context && sharedConfig.has(key)
104
- ? sharedConfig.load(key) // hydrating
105
- : fn(...args);
103
+ let res;
104
+ if (!isServer && sharedConfig.has && sharedConfig.has(key)) {
105
+ res = sharedConfig.load(key); // hydrating
106
+ // @ts-ignore at least until we add a delete method to sharedConfig
107
+ delete globalThis._$HY.r[key];
108
+ }
109
+ else
110
+ res = fn(...args);
106
111
  if (cached) {
107
112
  cached[0] = now;
108
113
  cached[1] = res;
package/dist/index.js CHANGED
@@ -1123,8 +1123,12 @@ function query(fn, name) {
1123
1123
  inPreloadFn && "then" in res && res.catch(() => {});
1124
1124
  return res;
1125
1125
  }
1126
- let res = !isServer && sharedConfig.context && sharedConfig.has(key) ? sharedConfig.load(key) // hydrating
1127
- : fn(...args);
1126
+ let res;
1127
+ if (!isServer && sharedConfig.has && sharedConfig.has(key)) {
1128
+ res = sharedConfig.load(key); // hydrating
1129
+ // @ts-ignore at least until we add a delete method to sharedConfig
1130
+ delete globalThis._$HY.r[key];
1131
+ } else res = fn(...args);
1128
1132
  if (cached) {
1129
1133
  cached[0] = now;
1130
1134
  cached[1] = res;
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "Ryan Turnquist"
7
7
  ],
8
8
  "license": "MIT",
9
- "version": "0.15.2",
9
+ "version": "0.15.3",
10
10
  "homepage": "https://github.com/solidjs/solid-router#readme",
11
11
  "repository": {
12
12
  "type": "git",
@@ -1,15 +0,0 @@
1
- import type { CacheEntry, NarrowResponse } from "../types.js";
2
- export declare function revalidate(key?: string | string[] | void, force?: boolean): Promise<void>;
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 {
5
- [K in keyof A]-?: A[K];
6
- } ? (...args: never[]) => R extends Promise<infer P> ? Promise<NarrowResponse<P>> : NarrowResponse<R> : (...args: A) => R extends Promise<infer P> ? Promise<NarrowResponse<P>> : NarrowResponse<R>) & {
7
- keyFor: (...args: A) => string;
8
- key: string;
9
- } : never;
10
- export declare function cache<T extends (...args: any) => any>(fn: T, name: string): CachedFunction<T>;
11
- export declare namespace cache {
12
- var set: (key: string, value: any) => void;
13
- var clear: () => void;
14
- }
15
- export declare function hashKey<T extends Array<any>>(args: T): string;
@@ -1,209 +0,0 @@
1
- import { createSignal, getListener, getOwner, onCleanup, sharedConfig, startTransition } from "solid-js";
2
- import { getRequestEvent, isServer } from "solid-js/web";
3
- import { useNavigate, getIntent, getInPreloadFn } from "../routing.js";
4
- const LocationHeader = "Location";
5
- const PRELOAD_TIMEOUT = 5000;
6
- const CACHE_TIMEOUT = 180000;
7
- let cacheMap = new Map();
8
- // cleanup forward/back cache
9
- if (!isServer) {
10
- setInterval(() => {
11
- const now = Date.now();
12
- for (let [k, v] of cacheMap.entries()) {
13
- if (!v[3].count && now - v[0] > CACHE_TIMEOUT) {
14
- cacheMap.delete(k);
15
- }
16
- }
17
- }, 300000);
18
- }
19
- function getCache() {
20
- if (!isServer)
21
- return cacheMap;
22
- const req = getRequestEvent();
23
- if (!req)
24
- throw new Error("Cannot find cache context");
25
- return (req.router || (req.router = {})).cache || (req.router.cache = new Map());
26
- }
27
- export function revalidate(key, force = true) {
28
- return startTransition(() => {
29
- const now = Date.now();
30
- cacheKeyOp(key, entry => {
31
- force && (entry[0] = 0); //force cache miss
32
- entry[3][1](now); // retrigger live signals
33
- });
34
- });
35
- }
36
- export function cacheKeyOp(key, fn) {
37
- key && !Array.isArray(key) && (key = [key]);
38
- for (let k of cacheMap.keys()) {
39
- if (key === undefined || matchKey(k, key))
40
- fn(cacheMap.get(k));
41
- }
42
- }
43
- export function cache(fn, name) {
44
- // prioritize GET for server functions
45
- if (fn.GET)
46
- fn = fn.GET;
47
- const cachedFn = ((...args) => {
48
- const cache = getCache();
49
- const intent = getIntent();
50
- const inPreloadFn = getInPreloadFn();
51
- const owner = getOwner();
52
- const navigate = owner ? useNavigate() : undefined;
53
- const now = Date.now();
54
- const key = name + hashKey(args);
55
- let cached = cache.get(key);
56
- let tracking;
57
- if (isServer) {
58
- const e = getRequestEvent();
59
- if (e) {
60
- const dataOnly = (e.router || (e.router = {})).dataOnly;
61
- if (dataOnly) {
62
- const data = e && (e.router.data || (e.router.data = {}));
63
- if (data && key in data)
64
- return data[key];
65
- if (Array.isArray(dataOnly) && !matchKey(key, dataOnly)) {
66
- data[key] = undefined;
67
- return Promise.resolve();
68
- }
69
- }
70
- }
71
- }
72
- if (getListener() && !isServer) {
73
- tracking = true;
74
- onCleanup(() => cached[3].count--);
75
- }
76
- if (cached &&
77
- cached[0] &&
78
- (isServer ||
79
- intent === "native" ||
80
- cached[3].count ||
81
- Date.now() - cached[0] < PRELOAD_TIMEOUT)) {
82
- if (tracking) {
83
- cached[3].count++;
84
- cached[3][0](); // track
85
- }
86
- if (cached[2] === "preload" && intent !== "preload") {
87
- cached[0] = now;
88
- }
89
- let res = cached[1];
90
- if (intent !== "preload") {
91
- res =
92
- "then" in cached[1]
93
- ? cached[1].then(handleResponse(false), handleResponse(true))
94
- : handleResponse(false)(cached[1]);
95
- !isServer && intent === "navigate" && startTransition(() => cached[3][1](cached[0])); // update version
96
- }
97
- inPreloadFn && "then" in res && res.catch(() => { });
98
- return res;
99
- }
100
- let res = !isServer && sharedConfig.context && sharedConfig.has(key)
101
- ? sharedConfig.load(key) // hydrating
102
- : fn(...args);
103
- if (cached) {
104
- cached[0] = now;
105
- cached[1] = res;
106
- cached[2] = intent;
107
- !isServer && intent === "navigate" && startTransition(() => cached[3][1](cached[0])); // update version
108
- }
109
- else {
110
- cache.set(key, (cached = [now, res, intent, createSignal(now)]));
111
- cached[3].count = 0;
112
- }
113
- if (tracking) {
114
- cached[3].count++;
115
- cached[3][0](); // track
116
- }
117
- if (isServer) {
118
- const e = getRequestEvent();
119
- if (e && e.router.dataOnly)
120
- return (e.router.data[key] = res);
121
- }
122
- if (intent !== "preload") {
123
- res =
124
- "then" in res
125
- ? res.then(handleResponse(false), handleResponse(true))
126
- : handleResponse(false)(res);
127
- }
128
- inPreloadFn && "then" in res && res.catch(() => { });
129
- // serialize on server
130
- if (isServer &&
131
- sharedConfig.context &&
132
- sharedConfig.context.async &&
133
- !sharedConfig.context.noHydrate) {
134
- const e = getRequestEvent();
135
- (!e || !e.serverOnly) && sharedConfig.context.serialize(key, res);
136
- }
137
- return res;
138
- function handleResponse(error) {
139
- return async (v) => {
140
- if (v instanceof Response) {
141
- const url = v.headers.get(LocationHeader);
142
- if (url !== null) {
143
- // client + server relative redirect
144
- if (navigate && url.startsWith("/"))
145
- startTransition(() => {
146
- navigate(url, { replace: true });
147
- });
148
- else if (!isServer)
149
- window.location.href = url;
150
- else if (isServer) {
151
- const e = getRequestEvent();
152
- if (e)
153
- e.response = { status: 302, headers: new Headers({ Location: url }) };
154
- }
155
- return;
156
- }
157
- if (v.customBody)
158
- v = await v.customBody();
159
- }
160
- if (error)
161
- throw v;
162
- return v;
163
- };
164
- }
165
- });
166
- cachedFn.keyFor = (...args) => name + hashKey(args);
167
- cachedFn.key = name;
168
- return cachedFn;
169
- }
170
- cache.set = (key, value) => {
171
- const cache = getCache();
172
- const now = Date.now();
173
- let cached = cache.get(key);
174
- if (cached) {
175
- cached[0] = now;
176
- cached[1] = value;
177
- cached[2] = "preload";
178
- }
179
- else {
180
- cache.set(key, (cached = [now, value, , createSignal(now)]));
181
- cached[3].count = 0;
182
- }
183
- };
184
- cache.clear = () => getCache().clear();
185
- function matchKey(key, keys) {
186
- for (let k of keys) {
187
- if (k && key.startsWith(k))
188
- return true;
189
- }
190
- return false;
191
- }
192
- // Modified from the amazing Tanstack Query library (MIT)
193
- // https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L168
194
- export function hashKey(args) {
195
- return JSON.stringify(args, (_, val) => isPlainObject(val)
196
- ? Object.keys(val)
197
- .sort()
198
- .reduce((result, key) => {
199
- result[key] = val[key];
200
- return result;
201
- }, {})
202
- : val);
203
- }
204
- function isPlainObject(obj) {
205
- let proto;
206
- return (obj != null &&
207
- typeof obj === "object" &&
208
- (!(proto = Object.getPrototypeOf(obj)) || proto === Object.prototype));
209
- }
@@ -1,4 +0,0 @@
1
- import type { LocationChange, RouterContext, RouterUtils } from "../types";
2
- export declare function createIntegration(get: () => string | LocationChange, set: (next: LocationChange) => void, init?: (notify: (value?: string | LocationChange) => void) => () => void, create?: (router: RouterContext) => void, utils?: Partial<RouterUtils>): (props: import("./components").RouterProps) => import("solid-js").JSX.Element;
3
- export declare function bindEvent(target: EventTarget, type: string, handler: EventListener): () => void;
4
- export declare function scrollToHash(hash: string, fallbackTop?: boolean): void;
@@ -1,49 +0,0 @@
1
- import { createSignal, onCleanup } from "solid-js";
2
- import { createRouter } from "./components";
3
- function intercept([value, setValue], get, set) {
4
- return [get ? () => get(value()) : value, set ? (v) => setValue(set(v)) : setValue];
5
- }
6
- function querySelector(selector) {
7
- if (selector === "#") {
8
- return null;
9
- }
10
- // Guard against selector being an invalid CSS selector
11
- try {
12
- return document.querySelector(selector);
13
- }
14
- catch (e) {
15
- return null;
16
- }
17
- }
18
- export function createIntegration(get, set, init, create, utils) {
19
- let ignore = false;
20
- const wrap = (value) => (typeof value === "string" ? { value } : value);
21
- const signal = intercept(createSignal(wrap(get()), { equals: (a, b) => a.value === b.value }), undefined, next => {
22
- !ignore && set(next);
23
- return next;
24
- });
25
- init &&
26
- onCleanup(init((value = get()) => {
27
- ignore = true;
28
- signal[1](wrap(value));
29
- ignore = false;
30
- }));
31
- return createRouter({
32
- signal,
33
- create,
34
- utils
35
- });
36
- }
37
- export function bindEvent(target, type, handler) {
38
- target.addEventListener(type, handler);
39
- return () => target.removeEventListener(type, handler);
40
- }
41
- export function scrollToHash(hash, fallbackTop) {
42
- const el = querySelector(`#${hash}`);
43
- if (el) {
44
- el.scrollIntoView();
45
- }
46
- else if (fallbackTop) {
47
- window.scrollTo(0, 0);
48
- }
49
- }
@@ -1,48 +0,0 @@
1
- import { createSignal, onCleanup } from "solid-js";
2
- function intercept([value, setValue], get, set) {
3
- return [get ? () => get(value()) : value, set ? (v) => setValue(set(v)) : setValue];
4
- }
5
- function querySelector(selector) {
6
- if (selector === "#") {
7
- return null;
8
- }
9
- // Guard against selector being an invalid CSS selector
10
- try {
11
- return document.querySelector(selector);
12
- }
13
- catch (e) {
14
- return null;
15
- }
16
- }
17
- export function createIntegration(get, set, init, create, utils) {
18
- let ignore = false;
19
- const wrap = (value) => (typeof value === "string" ? { value } : value);
20
- const signal = intercept(createSignal(wrap(get()), { equals: (a, b) => a.value === b.value }), undefined, next => {
21
- !ignore && set(next);
22
- return next;
23
- });
24
- init &&
25
- onCleanup(init((value = get()) => {
26
- ignore = true;
27
- signal[1](wrap(value));
28
- ignore = false;
29
- }));
30
- return {
31
- signal,
32
- create,
33
- utils
34
- };
35
- }
36
- export function bindEvent(target, type, handler) {
37
- target.addEventListener(type, handler);
38
- return () => target.removeEventListener(type, handler);
39
- }
40
- export function scrollToHash(hash, fallbackTop) {
41
- const el = querySelector(`#${hash}`);
42
- if (el) {
43
- el.scrollIntoView();
44
- }
45
- else if (fallbackTop) {
46
- window.scrollTo(0, 0);
47
- }
48
- }