crelte 0.4.8 → 0.5.0-alpha.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.
- package/dist/Crelte.d.ts +7 -6
- package/dist/Crelte.d.ts.map +1 -1
- package/dist/Crelte.js +5 -13
- package/dist/CrelteRequest.d.ts +9 -0
- package/dist/CrelteRequest.d.ts.map +1 -1
- package/dist/CrelteRequest.js +16 -2
- package/dist/blocks/Blocks.svelte +2 -2
- package/dist/blocks/Blocks.svelte.d.ts +3 -19
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
- package/dist/cookies/ClientCookies.d.ts +0 -1
- package/dist/cookies/ClientCookies.d.ts.map +1 -1
- package/dist/cookies/ClientCookies.js +0 -1
- package/dist/cookies/ServerCookies.d.ts +1 -2
- package/dist/cookies/ServerCookies.d.ts.map +1 -1
- package/dist/cookies/ServerCookies.js +2 -6
- package/dist/cookies/index.d.ts +0 -2
- package/dist/cookies/index.d.ts.map +1 -1
- package/dist/graphql/GraphQl.d.ts +2 -2
- package/dist/graphql/GraphQl.d.ts.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -6
- package/dist/init/InternalApp.d.ts +30 -0
- package/dist/init/InternalApp.d.ts.map +1 -0
- package/dist/init/InternalApp.js +71 -0
- package/dist/init/client.d.ts +0 -5
- package/dist/init/client.d.ts.map +1 -1
- package/dist/init/client.js +88 -75
- package/dist/init/crelte-vite-plugin.d.ts +5 -0
- package/dist/init/server.d.ts +0 -5
- package/dist/init/server.d.ts.map +1 -1
- package/dist/init/server.js +49 -20
- package/dist/init/shared.d.ts +7 -18
- package/dist/init/shared.d.ts.map +1 -1
- package/dist/init/shared.js +97 -154
- package/dist/init/svelteComponents.d.ts +3 -0
- package/dist/init/svelteComponents.d.ts.map +1 -0
- package/dist/init/svelteComponents.js +7 -0
- package/dist/loadData/Globals.d.ts +40 -33
- package/dist/loadData/Globals.d.ts.map +1 -1
- package/dist/loadData/Globals.js +99 -88
- package/dist/loadData/index.d.ts +3 -2
- package/dist/loadData/index.d.ts.map +1 -1
- package/dist/loadData/index.js +2 -0
- package/dist/plugins/Events.d.ts +11 -13
- package/dist/plugins/Events.d.ts.map +1 -1
- package/dist/plugins/Events.js +10 -3
- package/dist/routing/BaseRoute.d.ts +255 -0
- package/dist/routing/BaseRoute.d.ts.map +1 -0
- package/dist/routing/BaseRoute.js +349 -0
- package/dist/routing/BaseRouter.d.ts +210 -0
- package/dist/routing/BaseRouter.d.ts.map +1 -0
- package/dist/routing/BaseRouter.js +444 -0
- package/dist/routing/ClientRouter.d.ts +32 -0
- package/dist/routing/ClientRouter.d.ts.map +1 -0
- package/dist/routing/ClientRouter.js +259 -0
- package/dist/routing/LoadRunner.d.ts +39 -0
- package/dist/routing/LoadRunner.d.ts.map +1 -0
- package/dist/routing/{PageLoader.js → LoadRunner.js} +32 -20
- package/dist/routing/Request.d.ts +35 -3
- package/dist/routing/Request.d.ts.map +1 -1
- package/dist/routing/Request.js +64 -5
- package/dist/routing/Route.d.ts +24 -223
- package/dist/routing/Route.d.ts.map +1 -1
- package/dist/routing/Route.js +26 -315
- package/dist/routing/Router.d.ts +49 -73
- package/dist/routing/Router.d.ts.map +1 -1
- package/dist/routing/Router.js +85 -251
- package/dist/routing/ServerRouter.d.ts +23 -0
- package/dist/routing/ServerRouter.d.ts.map +1 -0
- package/dist/routing/ServerRouter.js +57 -0
- package/dist/routing/utils.d.ts +5 -0
- package/dist/routing/utils.d.ts.map +1 -1
- package/dist/routing/utils.js +39 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -0
- package/package.json +7 -6
- package/src/Crelte.ts +12 -18
- package/src/CrelteRequest.ts +21 -2
- package/src/cookies/ClientCookies.ts +0 -2
- package/src/cookies/ServerCookies.ts +2 -7
- package/src/cookies/index.ts +0 -3
- package/src/graphql/GraphQl.ts +2 -1
- package/src/index.ts +17 -9
- package/src/init/InternalApp.ts +134 -0
- package/src/init/client.ts +104 -93
- package/src/init/crelte-vite-plugin.d.ts +5 -0
- package/src/init/server.ts +67 -35
- package/src/init/shared.ts +107 -227
- package/src/init/svelteComponents.ts +12 -0
- package/src/loadData/Globals.ts +121 -102
- package/src/loadData/index.ts +3 -2
- package/src/plugins/Events.ts +40 -42
- package/src/routing/BaseRoute.ts +422 -0
- package/src/routing/BaseRouter.ts +528 -0
- package/src/routing/ClientRouter.ts +329 -0
- package/src/routing/{PageLoader.ts → LoadRunner.ts} +43 -30
- package/src/routing/Request.ts +97 -12
- package/src/routing/Route.ts +56 -376
- package/src/routing/Router.ts +100 -359
- package/src/routing/ServerRouter.ts +78 -0
- package/src/routing/utils.ts +53 -0
- package/src/utils.ts +4 -0
- package/dist/routing/InnerRouter.d.ts +0 -113
- package/dist/routing/InnerRouter.d.ts.map +0 -1
- package/dist/routing/InnerRouter.js +0 -417
- package/dist/routing/PageLoader.d.ts +0 -36
- package/dist/routing/PageLoader.d.ts.map +0 -1
- package/src/routing/InnerRouter.ts +0 -498
package/dist/routing/Route.d.ts
CHANGED
|
@@ -1,252 +1,53 @@
|
|
|
1
1
|
import Site from './Site.js';
|
|
2
|
+
import { Entry, LoadData } from '../index.js';
|
|
3
|
+
import BaseRoute, { RouteOrigin } from './BaseRoute.js';
|
|
2
4
|
export type RouteOptions = {
|
|
5
|
+
entryChanged?: boolean;
|
|
3
6
|
scrollY?: number;
|
|
4
7
|
index?: number;
|
|
5
8
|
origin?: RouteOrigin;
|
|
6
9
|
state?: Record<string, any>;
|
|
7
10
|
context?: Record<string, any>;
|
|
8
11
|
};
|
|
12
|
+
export interface TemplateModule {
|
|
13
|
+
default: any;
|
|
14
|
+
loadData?: LoadData<Entry>;
|
|
15
|
+
}
|
|
9
16
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* - `'init'`: is set on the first page load
|
|
14
|
-
* - `'manual'`: is set when a route is triggered manually via `Router.open`
|
|
15
|
-
* - `'click'`: is set when a route is triggered by a click event
|
|
16
|
-
* - `'pop'`: is set when a route is triggered by a popstate event (back/forward)
|
|
17
|
-
* - `'replace'`: is set when a route is replaced via `Router.replaceState`
|
|
18
|
-
* - `'push'`: is set when a route is pushed via `Router.pushState`
|
|
19
|
-
*
|
|
20
|
-
* ## Note
|
|
21
|
-
* `replace` and `push` will not call loadData
|
|
22
|
-
*/
|
|
23
|
-
export type RouteOrigin = 'init' | 'manual' | 'click' | 'pop' | 'replace' | 'push';
|
|
24
|
-
/**
|
|
25
|
-
* A Route contains information about the current page for example the url and
|
|
26
|
-
* the site
|
|
17
|
+
* A Route contains information about the current page for example the url,
|
|
18
|
+
* the site and its entry.
|
|
27
19
|
*/
|
|
28
|
-
export default class Route {
|
|
29
|
-
/**
|
|
30
|
-
* The url of the route
|
|
31
|
-
*/
|
|
32
|
-
url: URL;
|
|
33
|
-
/**
|
|
34
|
-
* The site of the route
|
|
35
|
-
*
|
|
36
|
-
* ## Note
|
|
37
|
-
* The site might not always match with the current route
|
|
38
|
-
* but be the site default site or one that matches the
|
|
39
|
-
* users language.
|
|
40
|
-
*
|
|
41
|
-
* If that is important call `route.siteMatches()` to verify
|
|
42
|
-
*/
|
|
43
|
-
site: Site;
|
|
20
|
+
export default class Route extends BaseRoute {
|
|
44
21
|
/**
|
|
45
|
-
* The
|
|
46
|
-
*
|
|
47
|
-
* ## Note
|
|
48
|
-
* This does not have to represent the current scroll position
|
|
49
|
-
* should more be used internally.
|
|
50
|
-
*
|
|
51
|
-
* It might be useful for a new request to specify the wanted
|
|
52
|
-
* scroll position
|
|
22
|
+
* The entry of the route
|
|
53
23
|
*/
|
|
54
|
-
|
|
24
|
+
entry: Entry;
|
|
55
25
|
/**
|
|
56
|
-
*
|
|
57
|
-
* this allows to find out if we can go back
|
|
26
|
+
* The template module of the route
|
|
58
27
|
*/
|
|
59
|
-
|
|
28
|
+
template: TemplateModule;
|
|
60
29
|
/**
|
|
61
|
-
* The
|
|
30
|
+
* The loaded data of the route
|
|
62
31
|
*/
|
|
63
|
-
|
|
32
|
+
loadedData: Record<string, any>;
|
|
64
33
|
/**
|
|
65
|
-
*
|
|
66
|
-
* State data that can be used to store additional information
|
|
34
|
+
* Wether the entry changed since the last Route change
|
|
67
35
|
*/
|
|
68
|
-
|
|
36
|
+
entryChanged: boolean;
|
|
69
37
|
/**
|
|
70
|
-
*
|
|
71
|
-
* Any data that should be passed to onRoute and onRequest handlers
|
|
72
|
-
* or exchanged between loadData's
|
|
73
|
-
* This context is not persistant and should be considered "valid"
|
|
74
|
-
* only for the current request / route
|
|
38
|
+
* Create a new Route
|
|
75
39
|
*
|
|
76
40
|
* ## Note
|
|
77
|
-
*
|
|
78
|
-
* call so will always be the same object
|
|
79
|
-
*/
|
|
80
|
-
_context: Record<string, any>;
|
|
81
|
-
/**
|
|
82
|
-
* Creates a new Route
|
|
83
|
-
*/
|
|
84
|
-
constructor(url: string | URL, site: Site, opts?: RouteOptions);
|
|
85
|
-
/**
|
|
86
|
-
* Returns the uri of the route
|
|
87
|
-
*
|
|
88
|
-
* Never ends with a slash
|
|
89
|
-
*
|
|
90
|
-
* ## Example
|
|
91
|
-
* ```
|
|
92
|
-
* const site = _; // site with url https://example.com/fo
|
|
93
|
-
* const route = new Route('https://example.com/foo/bar/', site);
|
|
94
|
-
* console.log(route.uri); // '/bar'
|
|
95
|
-
*
|
|
96
|
-
* const site2 = _; // site with url https://example.com/other
|
|
97
|
-
* const route2 = new Route('https://example.com/foo/bar/?a=1', site2);
|
|
98
|
-
* console.log(route2.uri); // '/foo/bar'
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
get uri(): string;
|
|
102
|
-
/**
|
|
103
|
-
* Returns the base url of the route
|
|
104
|
-
*
|
|
105
|
-
* Never ends with a slash
|
|
106
|
-
*
|
|
107
|
-
* ## Example
|
|
108
|
-
* ```
|
|
109
|
-
* const site = _; // site with url https://example.com/foo
|
|
110
|
-
* const route = new Route('https://example.com/foo/bar/', null);
|
|
111
|
-
* console.log(route.baseUrl); // 'https://example.com/foo'
|
|
112
|
-
*
|
|
113
|
-
* const site2 = _; // site with url https://example.com/other
|
|
114
|
-
* const route2 = new Route('https://example.com/foo/bar/', site2);
|
|
115
|
-
* console.log(route2.baseUrl); // 'https://example.com'
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
get baseUrl(): string;
|
|
119
|
-
/**
|
|
120
|
-
* Returns the search params
|
|
121
|
-
*
|
|
122
|
-
* ## Note
|
|
123
|
-
* You might also have a look at `getSearchParam` and `setSearchParam`
|
|
124
|
-
*
|
|
125
|
-
* ## Example
|
|
126
|
-
* ```
|
|
127
|
-
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
128
|
-
* console.log(route.search.get('a')); // '1'
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
get search(): URLSearchParams;
|
|
132
|
-
/**
|
|
133
|
-
* Returns the hash of the route
|
|
134
|
-
*
|
|
135
|
-
* ## Example
|
|
136
|
-
* ```
|
|
137
|
-
* const route = new Route('https://example.com/foo/bar/#hash', null);
|
|
138
|
-
* console.log(route.hash); // '#hash'
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
get hash(): string;
|
|
142
|
-
/**
|
|
143
|
-
* Set the hash of the route
|
|
144
|
-
*
|
|
145
|
-
* ## Example
|
|
146
|
-
* ```
|
|
147
|
-
* const route = new Route('https://example.com/foo/bar/', null);
|
|
148
|
-
* route.hash = '#hash';
|
|
149
|
-
* console.log(route.url.href); // 'https://example.com/foo/bar/#hash'
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
152
|
-
set hash(hash: string);
|
|
153
|
-
/**
|
|
154
|
-
* Checks if there are previous routes which would allow it to go back
|
|
155
|
-
*/
|
|
156
|
-
canGoBack(): boolean;
|
|
157
|
-
/**
|
|
158
|
-
* Gets the search param
|
|
159
|
-
*
|
|
160
|
-
* ## Example
|
|
161
|
-
* ```
|
|
162
|
-
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
163
|
-
* console.log(route.getSearchParam('a')); // '1'
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
getSearchParam(key: string): string | null;
|
|
167
|
-
/**
|
|
168
|
-
* Sets the search param or removes it if the value is null, undefined or an
|
|
169
|
-
* empty string
|
|
170
|
-
*
|
|
171
|
-
* ## Example
|
|
172
|
-
* ```
|
|
173
|
-
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
174
|
-
* route.setSearchParam('a', '3');
|
|
175
|
-
* console.log(route.url.href); // 'https://example.com/foo/bar/?a=3&b=2'
|
|
176
|
-
*
|
|
177
|
-
* route.setSearchParam('a', null);
|
|
178
|
-
* console.log(route.url.href); // 'https://example.com/foo/bar/?b=2'
|
|
179
|
-
* ```
|
|
180
|
-
*/
|
|
181
|
-
setSearchParam(key: string, value?: string | number | null): void;
|
|
182
|
-
/**
|
|
183
|
-
* Returns a state value if it exists.
|
|
184
|
-
*/
|
|
185
|
-
getState<T = any>(key: string): T | null;
|
|
186
|
-
/**
|
|
187
|
-
* Sets a state value.
|
|
188
|
-
* If the value is null or undefined, the key will be removed.
|
|
189
|
-
*
|
|
190
|
-
* ## When to use state
|
|
191
|
-
* State is used to store additional information that persists across route changes.
|
|
192
|
-
* The State is only available in the client code since it is stored using window.history.
|
|
193
|
-
*
|
|
194
|
-
* Consider using setSearchParam instead to enable server side rendering.
|
|
195
|
-
*/
|
|
196
|
-
setState<T>(key: string, value: T | null | undefined): void;
|
|
197
|
-
/**
|
|
198
|
-
* Returns a context value if it exists.
|
|
41
|
+
* This should only be created by crelte
|
|
199
42
|
*/
|
|
200
|
-
|
|
43
|
+
constructor(url: string | URL, site: Site, entry: Entry, template: TemplateModule, loadedData: Record<string, any>, opts?: RouteOptions);
|
|
201
44
|
/**
|
|
202
|
-
*
|
|
203
|
-
* If the value is null or undefined, the key will be removed.
|
|
204
|
-
*
|
|
205
|
-
* ## When to use context
|
|
206
|
-
* Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
|
|
207
|
-
* This context is not persistent and should be considered valid only for the current request/route.
|
|
208
|
-
* The context is not cloned in the clone call and will be the same object.
|
|
209
|
-
*/
|
|
210
|
-
setContext<T>(key: string, value: T | null | undefined): void;
|
|
211
|
-
/**
|
|
212
|
-
* Returns true if the route is in live preview mode
|
|
213
|
-
*/
|
|
214
|
-
inLivePreview(): boolean;
|
|
215
|
-
/**
|
|
216
|
-
* Returns if the site matches the url
|
|
217
|
-
*/
|
|
218
|
-
siteMatches(): boolean;
|
|
219
|
-
/**
|
|
220
|
-
* Checks if the route is equal to another route
|
|
221
|
-
*
|
|
222
|
-
* This checks all properties of the url but search params do not have to be
|
|
223
|
-
* in the same order
|
|
45
|
+
* Create a copy of the EntryRoute
|
|
224
46
|
*
|
|
225
47
|
* ## Note
|
|
226
|
-
* This does not
|
|
227
|
-
*/
|
|
228
|
-
eq(route: Route | null): boolean | null;
|
|
229
|
-
/**
|
|
230
|
-
* Checks if the route is equal to another route
|
|
231
|
-
*
|
|
232
|
-
* This does not check the search params or hash
|
|
233
|
-
*/
|
|
234
|
-
eqUrl(route: Route | null): boolean | null;
|
|
235
|
-
/**
|
|
236
|
-
* Checks if the search params are equal to another route
|
|
237
|
-
*/
|
|
238
|
-
eqSearch(route: Route | null): boolean | null;
|
|
239
|
-
/**
|
|
240
|
-
* Checks if the hash is equal to another route
|
|
241
|
-
*/
|
|
242
|
-
eqHash(route: Route | null): boolean | null;
|
|
243
|
-
/**
|
|
244
|
-
* Create a copy of the request
|
|
48
|
+
* This does not make a copy of the entry, template or loadedData.
|
|
245
49
|
*/
|
|
246
50
|
clone(): Route;
|
|
247
|
-
/** @hidden */
|
|
248
|
-
_fillFromState(state: any): void;
|
|
249
|
-
/** @hidden */
|
|
250
|
-
_toState(): any;
|
|
251
51
|
}
|
|
52
|
+
export declare function isRoute(route: any): route is Route;
|
|
252
53
|
//# sourceMappingURL=Route.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Route.d.ts","sourceRoot":"","sources":["../../src/routing/Route.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Route.d.ts","sourceRoot":"","sources":["../../src/routing/Route.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,SAAS,EAAE,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAExD,MAAM,MAAM,YAAY,GAAG;IAC1B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B,CAAC;AAEF,MAAM,WAAW,cAAc;IAE9B,OAAO,EAAE,GAAG,CAAC;IAEb,QAAQ,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;CAC3B;AAED;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,KAAM,SAAQ,SAAS;IAC3C;;OAEG;IACH,KAAK,EAAE,KAAK,CAAC;IAEb;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC;IAEzB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAEhC;;OAEG;IACH,YAAY,EAAE,OAAO,CAAC;IAEtB;;;;;OAKG;gBAEF,GAAG,EAAE,MAAM,GAAG,GAAG,EACjB,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,cAAc,EACxB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC/B,IAAI,GAAE,YAAiB;IAUxB;;;;;OAKG;IACH,KAAK;CAgBL;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,KAAK,CAIlD"}
|
package/dist/routing/Route.js
CHANGED
|
@@ -1,319 +1,47 @@
|
|
|
1
1
|
import { objClone } from '../utils.js';
|
|
2
|
-
import
|
|
2
|
+
import BaseRoute from './BaseRoute.js';
|
|
3
3
|
/**
|
|
4
|
-
* A Route contains information about the current page for example the url
|
|
5
|
-
* the site
|
|
4
|
+
* A Route contains information about the current page for example the url,
|
|
5
|
+
* the site and its entry.
|
|
6
6
|
*/
|
|
7
|
-
export default class Route {
|
|
7
|
+
export default class Route extends BaseRoute {
|
|
8
8
|
/**
|
|
9
|
-
* The
|
|
9
|
+
* The entry of the route
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
entry;
|
|
12
12
|
/**
|
|
13
|
-
* The
|
|
14
|
-
*
|
|
15
|
-
* ## Note
|
|
16
|
-
* The site might not always match with the current route
|
|
17
|
-
* but be the site default site or one that matches the
|
|
18
|
-
* users language.
|
|
19
|
-
*
|
|
20
|
-
* If that is important call `route.siteMatches()` to verify
|
|
21
|
-
*/
|
|
22
|
-
site;
|
|
23
|
-
/**
|
|
24
|
-
* The scroll position of the current route
|
|
25
|
-
*
|
|
26
|
-
* ## Note
|
|
27
|
-
* This does not have to represent the current scroll position
|
|
28
|
-
* should more be used internally.
|
|
29
|
-
*
|
|
30
|
-
* It might be useful for a new request to specify the wanted
|
|
31
|
-
* scroll position
|
|
32
|
-
*/
|
|
33
|
-
scrollY;
|
|
34
|
-
/**
|
|
35
|
-
* the position in the browser history of this route
|
|
36
|
-
* this allows to find out if we can go back
|
|
37
|
-
*/
|
|
38
|
-
index;
|
|
39
|
-
/**
|
|
40
|
-
* The origin of this route, See [[RouteOrigin]]
|
|
41
|
-
*/
|
|
42
|
-
origin;
|
|
43
|
-
/**
|
|
44
|
-
* @hidden
|
|
45
|
-
* State data that can be used to store additional information
|
|
13
|
+
* The template module of the route
|
|
46
14
|
*/
|
|
47
|
-
|
|
15
|
+
template;
|
|
48
16
|
/**
|
|
49
|
-
*
|
|
50
|
-
* Any data that should be passed to onRoute and onRequest handlers
|
|
51
|
-
* or exchanged between loadData's
|
|
52
|
-
* This context is not persistant and should be considered "valid"
|
|
53
|
-
* only for the current request / route
|
|
54
|
-
*
|
|
55
|
-
* ## Note
|
|
56
|
-
* Consider using state instead. This will not be cloned in the clone
|
|
57
|
-
* call so will always be the same object
|
|
58
|
-
*/
|
|
59
|
-
_context;
|
|
60
|
-
/**
|
|
61
|
-
* Creates a new Route
|
|
62
|
-
*/
|
|
63
|
-
constructor(url, site, opts = {}) {
|
|
64
|
-
this.url = new URL(url);
|
|
65
|
-
this.site = site;
|
|
66
|
-
this.scrollY = opts.scrollY ?? null;
|
|
67
|
-
this.index = opts.index ?? 0;
|
|
68
|
-
this.origin = opts.origin ?? 'manual';
|
|
69
|
-
this._state = opts.state ?? {};
|
|
70
|
-
this._context = opts.context ?? {};
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Returns the uri of the route
|
|
74
|
-
*
|
|
75
|
-
* Never ends with a slash
|
|
76
|
-
*
|
|
77
|
-
* ## Example
|
|
78
|
-
* ```
|
|
79
|
-
* const site = _; // site with url https://example.com/fo
|
|
80
|
-
* const route = new Route('https://example.com/foo/bar/', site);
|
|
81
|
-
* console.log(route.uri); // '/bar'
|
|
82
|
-
*
|
|
83
|
-
* const site2 = _; // site with url https://example.com/other
|
|
84
|
-
* const route2 = new Route('https://example.com/foo/bar/?a=1', site2);
|
|
85
|
-
* console.log(route2.uri); // '/foo/bar'
|
|
86
|
-
* ```
|
|
17
|
+
* The loaded data of the route
|
|
87
18
|
*/
|
|
88
|
-
|
|
89
|
-
if (this.siteMatches()) {
|
|
90
|
-
return trimSlashEnd(this.url.pathname.substring(this.site.uri.length));
|
|
91
|
-
}
|
|
92
|
-
return trimSlashEnd(this.url.pathname);
|
|
93
|
-
}
|
|
19
|
+
loadedData;
|
|
94
20
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* Never ends with a slash
|
|
98
|
-
*
|
|
99
|
-
* ## Example
|
|
100
|
-
* ```
|
|
101
|
-
* const site = _; // site with url https://example.com/foo
|
|
102
|
-
* const route = new Route('https://example.com/foo/bar/', null);
|
|
103
|
-
* console.log(route.baseUrl); // 'https://example.com/foo'
|
|
104
|
-
*
|
|
105
|
-
* const site2 = _; // site with url https://example.com/other
|
|
106
|
-
* const route2 = new Route('https://example.com/foo/bar/', site2);
|
|
107
|
-
* console.log(route2.baseUrl); // 'https://example.com'
|
|
108
|
-
* ```
|
|
21
|
+
* Wether the entry changed since the last Route change
|
|
109
22
|
*/
|
|
110
|
-
|
|
111
|
-
if (this.siteMatches())
|
|
112
|
-
return trimSlashEnd(this.site.url.href);
|
|
113
|
-
return this.url.origin;
|
|
114
|
-
}
|
|
23
|
+
entryChanged;
|
|
115
24
|
/**
|
|
116
|
-
*
|
|
25
|
+
* Create a new Route
|
|
117
26
|
*
|
|
118
27
|
* ## Note
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* ## Example
|
|
122
|
-
* ```
|
|
123
|
-
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
124
|
-
* console.log(route.search.get('a')); // '1'
|
|
125
|
-
* ```
|
|
126
|
-
*/
|
|
127
|
-
get search() {
|
|
128
|
-
return this.url.searchParams;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Returns the hash of the route
|
|
132
|
-
*
|
|
133
|
-
* ## Example
|
|
134
|
-
* ```
|
|
135
|
-
* const route = new Route('https://example.com/foo/bar/#hash', null);
|
|
136
|
-
* console.log(route.hash); // '#hash'
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
get hash() {
|
|
140
|
-
return this.url.hash;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Set the hash of the route
|
|
144
|
-
*
|
|
145
|
-
* ## Example
|
|
146
|
-
* ```
|
|
147
|
-
* const route = new Route('https://example.com/foo/bar/', null);
|
|
148
|
-
* route.hash = '#hash';
|
|
149
|
-
* console.log(route.url.href); // 'https://example.com/foo/bar/#hash'
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
152
|
-
set hash(hash) {
|
|
153
|
-
this.url.hash = hash;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Checks if there are previous routes which would allow it to go back
|
|
157
|
-
*/
|
|
158
|
-
canGoBack() {
|
|
159
|
-
return !!this.index;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Gets the search param
|
|
163
|
-
*
|
|
164
|
-
* ## Example
|
|
165
|
-
* ```
|
|
166
|
-
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
167
|
-
* console.log(route.getSearchParam('a')); // '1'
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
getSearchParam(key) {
|
|
171
|
-
return this.search.get(key);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Sets the search param or removes it if the value is null, undefined or an
|
|
175
|
-
* empty string
|
|
176
|
-
*
|
|
177
|
-
* ## Example
|
|
178
|
-
* ```
|
|
179
|
-
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
180
|
-
* route.setSearchParam('a', '3');
|
|
181
|
-
* console.log(route.url.href); // 'https://example.com/foo/bar/?a=3&b=2'
|
|
182
|
-
*
|
|
183
|
-
* route.setSearchParam('a', null);
|
|
184
|
-
* console.log(route.url.href); // 'https://example.com/foo/bar/?b=2'
|
|
185
|
-
* ```
|
|
186
|
-
*/
|
|
187
|
-
setSearchParam(key, value) {
|
|
188
|
-
const deleteValue = typeof value === 'undefined' ||
|
|
189
|
-
value === null ||
|
|
190
|
-
(typeof value === 'string' && value === '');
|
|
191
|
-
if (!deleteValue) {
|
|
192
|
-
this.search.set(key, value);
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
this.search.delete(key);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Returns a state value if it exists.
|
|
200
|
-
*/
|
|
201
|
-
getState(key) {
|
|
202
|
-
return this._state[key] ?? null;
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Sets a state value.
|
|
206
|
-
* If the value is null or undefined, the key will be removed.
|
|
207
|
-
*
|
|
208
|
-
* ## When to use state
|
|
209
|
-
* State is used to store additional information that persists across route changes.
|
|
210
|
-
* The State is only available in the client code since it is stored using window.history.
|
|
211
|
-
*
|
|
212
|
-
* Consider using setSearchParam instead to enable server side rendering.
|
|
213
|
-
*/
|
|
214
|
-
setState(key, value) {
|
|
215
|
-
if (typeof value === 'undefined' || value === null) {
|
|
216
|
-
delete this._state[key];
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
this._state[key] = value;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Returns a context value if it exists.
|
|
224
|
-
*/
|
|
225
|
-
getContext(key) {
|
|
226
|
-
return this._context[key] ?? null;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Sets a context value.
|
|
230
|
-
* If the value is null or undefined, the key will be removed.
|
|
231
|
-
*
|
|
232
|
-
* ## When to use context
|
|
233
|
-
* Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
|
|
234
|
-
* This context is not persistent and should be considered valid only for the current request/route.
|
|
235
|
-
* The context is not cloned in the clone call and will be the same object.
|
|
236
|
-
*/
|
|
237
|
-
setContext(key, value) {
|
|
238
|
-
if (typeof value === 'undefined' || value === null) {
|
|
239
|
-
delete this._context[key];
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
this._context[key] = value;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Returns true if the route is in live preview mode
|
|
247
|
-
*/
|
|
248
|
-
inLivePreview() {
|
|
249
|
-
return !!this.search.get('x-craft-live-preview');
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Returns if the site matches the url
|
|
28
|
+
* This should only be created by crelte
|
|
253
29
|
*/
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
return (this.url.pathname + '/').startsWith(
|
|
261
|
-
// uri never returns a slash at the end
|
|
262
|
-
this.site.uri + '/');
|
|
30
|
+
constructor(url, site, entry, template, loadedData, opts = {}) {
|
|
31
|
+
super(url, site, opts);
|
|
32
|
+
this.entry = entry;
|
|
33
|
+
this.template = template;
|
|
34
|
+
this.loadedData = loadedData;
|
|
35
|
+
this.entryChanged = opts?.entryChanged ?? true;
|
|
263
36
|
}
|
|
264
37
|
/**
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
* This checks all properties of the url but search params do not have to be
|
|
268
|
-
* in the same order
|
|
38
|
+
* Create a copy of the EntryRoute
|
|
269
39
|
*
|
|
270
40
|
* ## Note
|
|
271
|
-
* This does not
|
|
272
|
-
*/
|
|
273
|
-
eq(route) {
|
|
274
|
-
return (route &&
|
|
275
|
-
this.eqUrl(route) &&
|
|
276
|
-
this.eqSearch(route) &&
|
|
277
|
-
this.eqHash(route));
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Checks if the route is equal to another route
|
|
281
|
-
*
|
|
282
|
-
* This does not check the search params or hash
|
|
283
|
-
*/
|
|
284
|
-
eqUrl(route) {
|
|
285
|
-
return (route &&
|
|
286
|
-
this.url.pathname === route.url.pathname &&
|
|
287
|
-
this.url.origin === route.url.origin);
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Checks if the search params are equal to another route
|
|
291
|
-
*/
|
|
292
|
-
eqSearch(route) {
|
|
293
|
-
const searchEq = (a, b) => {
|
|
294
|
-
if (a.size !== b.size)
|
|
295
|
-
return false;
|
|
296
|
-
a.sort();
|
|
297
|
-
b.sort();
|
|
298
|
-
const aEntries = Array.from(a.entries());
|
|
299
|
-
const bEntries = Array.from(b.entries());
|
|
300
|
-
return aEntries
|
|
301
|
-
.map((a, i) => [a, bEntries[i]])
|
|
302
|
-
.every(([[ak, av], [bk, bv]]) => ak === bk && av === bv);
|
|
303
|
-
};
|
|
304
|
-
return route && searchEq(this.search, route.search);
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Checks if the hash is equal to another route
|
|
308
|
-
*/
|
|
309
|
-
eqHash(route) {
|
|
310
|
-
return route && this.hash === route.hash;
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Create a copy of the request
|
|
41
|
+
* This does not make a copy of the entry, template or loadedData.
|
|
314
42
|
*/
|
|
315
43
|
clone() {
|
|
316
|
-
return new Route(this.url.href, this.site, {
|
|
44
|
+
return new Route(this.url.href, this.site, this.entry, this.template, this.loadedData, {
|
|
317
45
|
scrollY: this.scrollY ?? undefined,
|
|
318
46
|
index: this.index,
|
|
319
47
|
origin: this.origin,
|
|
@@ -321,24 +49,7 @@ export default class Route {
|
|
|
321
49
|
context: this._context,
|
|
322
50
|
});
|
|
323
51
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
this.scrollY = state.route.scrollY;
|
|
328
|
-
if (typeof state?.route?.index === 'number')
|
|
329
|
-
this.index = state.route.index;
|
|
330
|
-
if (typeof state?.state === 'object' && state.state !== null) {
|
|
331
|
-
this._state = state.state;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
/** @hidden */
|
|
335
|
-
_toState() {
|
|
336
|
-
return {
|
|
337
|
-
route: {
|
|
338
|
-
scrollY: this.scrollY,
|
|
339
|
-
index: this.index,
|
|
340
|
-
},
|
|
341
|
-
state: this._state,
|
|
342
|
-
};
|
|
343
|
-
}
|
|
52
|
+
}
|
|
53
|
+
export function isRoute(route) {
|
|
54
|
+
return (typeof route === 'object' && route !== null && route instanceof Route);
|
|
344
55
|
}
|