crelte 0.1.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.
- package/LICENSE.md +41 -0
- package/dist/Crelte.d.ts +55 -0
- package/dist/Crelte.d.ts.map +1 -0
- package/dist/Crelte.js +106 -0
- package/dist/CrelteBase.d.ts +16 -0
- package/dist/CrelteBase.d.ts.map +1 -0
- package/dist/CrelteBase.js +1 -0
- package/dist/CrelteRouted.d.ts +50 -0
- package/dist/CrelteRouted.d.ts.map +1 -0
- package/dist/CrelteRouted.js +88 -0
- package/dist/blocks/Blocks.d.ts +35 -0
- package/dist/blocks/Blocks.d.ts.map +1 -0
- package/dist/blocks/Blocks.js +100 -0
- package/dist/blocks/Blocks.svelte +21 -0
- package/dist/blocks/Blocks.svelte.d.ts +24 -0
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -0
- package/dist/blocks/index.d.ts +5 -0
- package/dist/blocks/index.d.ts.map +1 -0
- package/dist/blocks/index.js +3 -0
- package/dist/cookies/ClientCookies.d.ts +9 -0
- package/dist/cookies/ClientCookies.d.ts.map +1 -0
- package/dist/cookies/ClientCookies.js +22 -0
- package/dist/cookies/ServerCookies.d.ts +13 -0
- package/dist/cookies/ServerCookies.d.ts.map +1 -0
- package/dist/cookies/ServerCookies.js +31 -0
- package/dist/cookies/index.d.ts +20 -0
- package/dist/cookies/index.d.ts.map +1 -0
- package/dist/cookies/index.js +1 -0
- package/dist/cookies/utils.d.ts +12 -0
- package/dist/cookies/utils.d.ts.map +1 -0
- package/dist/cookies/utils.js +32 -0
- package/dist/graphql/GraphQl.d.ts +60 -0
- package/dist/graphql/GraphQl.d.ts.map +1 -0
- package/dist/graphql/GraphQl.js +197 -0
- package/dist/graphql/gql.test.d.ts +2 -0
- package/dist/graphql/gql.test.d.ts.map +1 -0
- package/dist/graphql/gql.test.js +80 -0
- package/dist/graphql/index.d.ts +3 -0
- package/dist/graphql/index.d.ts.map +1 -0
- package/dist/graphql/index.js +2 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +83 -0
- package/dist/init/client.d.ts +13 -0
- package/dist/init/client.d.ts.map +1 -0
- package/dist/init/client.js +129 -0
- package/dist/init/server.d.ts +38 -0
- package/dist/init/server.d.ts.map +1 -0
- package/dist/init/server.js +95 -0
- package/dist/init/shared.d.ts +29 -0
- package/dist/init/shared.d.ts.map +1 -0
- package/dist/init/shared.js +154 -0
- package/dist/loadData/Globals.d.ts +33 -0
- package/dist/loadData/Globals.d.ts.map +1 -0
- package/dist/loadData/Globals.js +119 -0
- package/dist/loadData/index.d.ts +25 -0
- package/dist/loadData/index.d.ts.map +1 -0
- package/dist/loadData/index.js +39 -0
- package/dist/plugins/Events.d.ts +11 -0
- package/dist/plugins/Events.d.ts.map +1 -0
- package/dist/plugins/Events.js +29 -0
- package/dist/plugins/Plugins.d.ts +12 -0
- package/dist/plugins/Plugins.d.ts.map +1 -0
- package/dist/plugins/Plugins.js +12 -0
- package/dist/plugins/index.d.ts +5 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +2 -0
- package/dist/routing/History.d.ts +22 -0
- package/dist/routing/History.d.ts.map +1 -0
- package/dist/routing/History.js +36 -0
- package/dist/routing/InnerRouter.d.ts +111 -0
- package/dist/routing/InnerRouter.d.ts.map +1 -0
- package/dist/routing/InnerRouter.js +397 -0
- package/dist/routing/PageLoader.d.ts +37 -0
- package/dist/routing/PageLoader.d.ts.map +1 -0
- package/dist/routing/PageLoader.js +72 -0
- package/dist/routing/Route.d.ts +82 -0
- package/dist/routing/Route.d.ts.map +1 -0
- package/dist/routing/Route.js +134 -0
- package/dist/routing/Router.d.ts +162 -0
- package/dist/routing/Router.d.ts.map +1 -0
- package/dist/routing/Router.js +333 -0
- package/dist/routing/Site.d.ts +47 -0
- package/dist/routing/Site.d.ts.map +1 -0
- package/dist/routing/Site.js +48 -0
- package/dist/routing/index.d.ts +5 -0
- package/dist/routing/index.d.ts.map +1 -0
- package/dist/routing/index.js +4 -0
- package/dist/ssr/SsrCache.d.ts +12 -0
- package/dist/ssr/SsrCache.d.ts.map +1 -0
- package/dist/ssr/SsrCache.js +50 -0
- package/dist/ssr/SsrComponents.d.ts +7 -0
- package/dist/ssr/SsrComponents.d.ts.map +1 -0
- package/dist/ssr/SsrComponents.js +30 -0
- package/dist/ssr/index.d.ts +4 -0
- package/dist/ssr/index.d.ts.map +1 -0
- package/dist/ssr/index.js +3 -0
- package/package.json +79 -0
- package/src/Crelte.ts +135 -0
- package/src/CrelteBase.ts +24 -0
- package/src/CrelteRouted.ts +128 -0
- package/src/blocks/Blocks.svelte +68 -0
- package/src/blocks/Blocks.ts +155 -0
- package/src/blocks/index.ts +14 -0
- package/src/cookies/ClientCookies.ts +30 -0
- package/src/cookies/ServerCookies.ts +42 -0
- package/src/cookies/index.ts +24 -0
- package/src/cookies/utils.ts +53 -0
- package/src/graphql/GraphQl.ts +281 -0
- package/src/graphql/gql.test.ts +123 -0
- package/src/graphql/index.ts +8 -0
- package/src/index.ts +109 -0
- package/src/init/client.ts +190 -0
- package/src/init/server.ts +177 -0
- package/src/init/shared.ts +221 -0
- package/src/loadData/Globals.ts +150 -0
- package/src/loadData/index.ts +67 -0
- package/src/plugins/Events.ts +50 -0
- package/src/plugins/Plugins.ts +23 -0
- package/src/plugins/index.ts +5 -0
- package/src/routing/History.ts +52 -0
- package/src/routing/InnerRouter.ts +469 -0
- package/src/routing/PageLoader.ts +112 -0
- package/src/routing/Route.ts +184 -0
- package/src/routing/Router.ts +476 -0
- package/src/routing/Site.ts +65 -0
- package/src/routing/index.ts +5 -0
- package/src/ssr/SsrCache.ts +61 -0
- package/src/ssr/SsrComponents.ts +34 -0
- package/src/ssr/index.ts +4 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import Route from './Route.js';
|
|
2
|
+
import InnerRouter from './InnerRouter.js';
|
|
3
|
+
import PageLoader from './PageLoader.js';
|
|
4
|
+
import { Writable } from 'crelte-std/stores';
|
|
5
|
+
import { Barrier, Listeners } from 'crelte-std/sync';
|
|
6
|
+
const defaultRouterOpts = {
|
|
7
|
+
preloadOnMouseOver: false,
|
|
8
|
+
deubgTiming: false,
|
|
9
|
+
};
|
|
10
|
+
export function trimSlashEnd(str) {
|
|
11
|
+
return str.endsWith('/') ? str.substring(0, str.length - 1) : str;
|
|
12
|
+
}
|
|
13
|
+
// Make sure route and nextRoute are not the same object as _inner.route
|
|
14
|
+
export default class Router {
|
|
15
|
+
/**
|
|
16
|
+
* The current route
|
|
17
|
+
*/
|
|
18
|
+
_route;
|
|
19
|
+
/**
|
|
20
|
+
* The current site
|
|
21
|
+
*/
|
|
22
|
+
_site;
|
|
23
|
+
/**
|
|
24
|
+
* The next route which is currently being loaded
|
|
25
|
+
*/
|
|
26
|
+
_nextRoute;
|
|
27
|
+
/**
|
|
28
|
+
* The next site which is currently being loaded
|
|
29
|
+
*/
|
|
30
|
+
_nextSite;
|
|
31
|
+
/**
|
|
32
|
+
* The loading flag, specifies if a page is currently
|
|
33
|
+
* getting loaded
|
|
34
|
+
*/
|
|
35
|
+
_loading;
|
|
36
|
+
/**
|
|
37
|
+
* The loading progress, the value is between 0 and 1
|
|
38
|
+
*/
|
|
39
|
+
_loadingProgress;
|
|
40
|
+
_onRouteEv;
|
|
41
|
+
_onNextRoute;
|
|
42
|
+
_renderBarrier;
|
|
43
|
+
// doc hidden
|
|
44
|
+
_internal;
|
|
45
|
+
inner;
|
|
46
|
+
pageLoader;
|
|
47
|
+
constructor(sites, opts = {}) {
|
|
48
|
+
opts = { ...defaultRouterOpts, ...opts };
|
|
49
|
+
this.inner = new InnerRouter(sites, {
|
|
50
|
+
preloadOnMouseOver: opts.preloadOnMouseOver,
|
|
51
|
+
});
|
|
52
|
+
this.pageLoader = new PageLoader({
|
|
53
|
+
debugTiming: opts.debugTiming,
|
|
54
|
+
});
|
|
55
|
+
// in the first onRoute call we will update this value
|
|
56
|
+
this._route = new Writable(null);
|
|
57
|
+
this._site = new Writable(null);
|
|
58
|
+
this._nextRoute = new Writable(null);
|
|
59
|
+
this._nextSite = new Writable(null);
|
|
60
|
+
this._loading = new Writable(false);
|
|
61
|
+
this._loadingProgress = new Writable(0);
|
|
62
|
+
this._onRouteEv = new Listeners();
|
|
63
|
+
this._onNextRoute = new Listeners();
|
|
64
|
+
this._renderBarrier = null;
|
|
65
|
+
this._internal = {
|
|
66
|
+
onLoaded: () => { },
|
|
67
|
+
onLoad: () => { },
|
|
68
|
+
domReady: route => this.inner.domReady(route),
|
|
69
|
+
initClient: () => this._initClient(),
|
|
70
|
+
initServer: (url, acceptLang) => this._initServer(url, acceptLang),
|
|
71
|
+
};
|
|
72
|
+
this.inner.onRoute = (route, site, changeHistory) => this._onRoute(route, site, changeHistory);
|
|
73
|
+
this.inner.onPreload = (route, site) => this._onPreload(route, site);
|
|
74
|
+
this.pageLoader.onLoaded = (resp, route, site, more) => this._onLoaded(resp, route, site, more);
|
|
75
|
+
this.pageLoader.loadFn = (route, site, opts) => this._internal.onLoad(route, site, opts);
|
|
76
|
+
this.pageLoader.onProgress = (loading, progress) => this._onProgress(loading, progress);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The current route
|
|
80
|
+
*
|
|
81
|
+
* this is a svelte store
|
|
82
|
+
*/
|
|
83
|
+
get route() {
|
|
84
|
+
return this._route.readclone();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The current site
|
|
88
|
+
*/
|
|
89
|
+
get site() {
|
|
90
|
+
return this._site.readonly();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The next route which is currently being loaded
|
|
94
|
+
*/
|
|
95
|
+
get nextRoute() {
|
|
96
|
+
return this._nextRoute.readclone();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The next site which is currently being loaded
|
|
100
|
+
*/
|
|
101
|
+
get nextSite() {
|
|
102
|
+
return this._nextSite.readonly();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The sites which are available
|
|
106
|
+
*/
|
|
107
|
+
get sites() {
|
|
108
|
+
return this.inner.sites;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The loading flag, specifies if a page is currently
|
|
112
|
+
* getting loaded
|
|
113
|
+
*/
|
|
114
|
+
get loading() {
|
|
115
|
+
return this._loading.readonly();
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The loading progress, the value is between 0 and 1
|
|
119
|
+
*/
|
|
120
|
+
get loadingProgress() {
|
|
121
|
+
return this._loadingProgress.readonly();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Open a new route
|
|
125
|
+
*
|
|
126
|
+
* @param target the target to open can be an url or a route
|
|
127
|
+
* the url needs to start with http or with a / which will be considered as
|
|
128
|
+
* the site baseUrl
|
|
129
|
+
*/
|
|
130
|
+
open(target) {
|
|
131
|
+
this.inner.open(target);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* This pushes the state of the route without triggering an event
|
|
135
|
+
*
|
|
136
|
+
* You can use this when using pagination for example change the route object
|
|
137
|
+
* (search argument) and then call pushState
|
|
138
|
+
*
|
|
139
|
+
* @param route
|
|
140
|
+
*/
|
|
141
|
+
pushState(route) {
|
|
142
|
+
this.pageLoader.discard();
|
|
143
|
+
this.inner.pushState(route);
|
|
144
|
+
this.setNewRoute(route);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* This replaces the state of the route without triggering an event
|
|
148
|
+
*
|
|
149
|
+
* @param route
|
|
150
|
+
*/
|
|
151
|
+
replaceState(route) {
|
|
152
|
+
this.pageLoader.discard();
|
|
153
|
+
this.inner.replaceState(route);
|
|
154
|
+
this.setNewRoute(route);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Checks if there are previous routes which would allow it to go back
|
|
158
|
+
*/
|
|
159
|
+
canGoBack() {
|
|
160
|
+
return this.inner.route?.canGoBack() ?? false;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Go back in the history
|
|
164
|
+
*/
|
|
165
|
+
back() {
|
|
166
|
+
window.history.back();
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Preload a url
|
|
170
|
+
*/
|
|
171
|
+
preload(target) {
|
|
172
|
+
this.inner.preload(target);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Add a listener for the onRoute event
|
|
176
|
+
*
|
|
177
|
+
* This differs from router.route.subscribe in the way that
|
|
178
|
+
* it will only trigger if a new render / load will occur
|
|
179
|
+
*/
|
|
180
|
+
onRoute(fn) {
|
|
181
|
+
return this._onRouteEv.add(fn);
|
|
182
|
+
}
|
|
183
|
+
onNextRoute(fn) {
|
|
184
|
+
return this._onNextRoute.add(fn);
|
|
185
|
+
}
|
|
186
|
+
setNewRoute(route) {
|
|
187
|
+
this._route.setSilent(route);
|
|
188
|
+
this._nextRoute.setSilent(route);
|
|
189
|
+
if (route.site) {
|
|
190
|
+
this._site.setSilent(route.site);
|
|
191
|
+
this._nextSite.setSilent(route.site);
|
|
192
|
+
}
|
|
193
|
+
this._nextRoute.notify();
|
|
194
|
+
this._route.notify();
|
|
195
|
+
if (route.site) {
|
|
196
|
+
this._nextSite.notify();
|
|
197
|
+
this._site.notify();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async _initClient() {
|
|
201
|
+
this.inner.initClient();
|
|
202
|
+
}
|
|
203
|
+
async _initServer(url, acceptLang) {
|
|
204
|
+
this.inner.initServer();
|
|
205
|
+
const prom = new Promise(resolve => {
|
|
206
|
+
this._internal.onLoaded = (success, route, site, ready) => {
|
|
207
|
+
const props = ready();
|
|
208
|
+
this._internal.onLoaded = () => { };
|
|
209
|
+
resolve({
|
|
210
|
+
success,
|
|
211
|
+
redirect: false,
|
|
212
|
+
route,
|
|
213
|
+
site,
|
|
214
|
+
props,
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
});
|
|
218
|
+
const route = this.inner.targetToRoute(url);
|
|
219
|
+
route.origin = 'init';
|
|
220
|
+
// let's see if the url matches any route and site
|
|
221
|
+
// if not let's redirect to the site which matches the acceptLang
|
|
222
|
+
if (!route.site) {
|
|
223
|
+
const site = this.inner.siteByAcceptLang(acceptLang);
|
|
224
|
+
return {
|
|
225
|
+
success: true,
|
|
226
|
+
redirect: true,
|
|
227
|
+
route: new Route(site.url, site),
|
|
228
|
+
site,
|
|
229
|
+
props: {},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
const hist = this.inner.history;
|
|
233
|
+
if (hist.url) {
|
|
234
|
+
const nRoute = new Route(hist.url, null);
|
|
235
|
+
if (!route.eq(nRoute)) {
|
|
236
|
+
return {
|
|
237
|
+
success: true,
|
|
238
|
+
redirect: true,
|
|
239
|
+
route: nRoute,
|
|
240
|
+
site: route.site,
|
|
241
|
+
props: {},
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
this.inner.setRoute(route);
|
|
246
|
+
return await prom;
|
|
247
|
+
}
|
|
248
|
+
_onRoute(route, site, changeHistory) {
|
|
249
|
+
this._nextRoute.setSilent(route);
|
|
250
|
+
const siteChanged = this.nextSite.get()?.id !== site.id;
|
|
251
|
+
this._nextSite.setSilent(site);
|
|
252
|
+
this._nextRoute.notify();
|
|
253
|
+
if (siteChanged)
|
|
254
|
+
this._nextSite.notify();
|
|
255
|
+
if (this._renderBarrier) {
|
|
256
|
+
const barr = this._renderBarrier;
|
|
257
|
+
this._renderBarrier = null;
|
|
258
|
+
// make sure nobody waits forevery
|
|
259
|
+
barr.cancel();
|
|
260
|
+
}
|
|
261
|
+
const barrier = new RenderBarrier();
|
|
262
|
+
this._renderBarrier = barrier;
|
|
263
|
+
this._onNextRoute.trigger(route.clone(), site, {
|
|
264
|
+
delayRender: () => barrier.add(),
|
|
265
|
+
});
|
|
266
|
+
// route prepared
|
|
267
|
+
this.pageLoader.load(route.clone(), site, { changeHistory });
|
|
268
|
+
}
|
|
269
|
+
_onPreload(route, site) {
|
|
270
|
+
this.pageLoader.preload(route, site);
|
|
271
|
+
}
|
|
272
|
+
async _onLoaded(resp, route, site, more) {
|
|
273
|
+
// we need to wait on the renderBarrier
|
|
274
|
+
const renderBarrier = this._renderBarrier;
|
|
275
|
+
if (renderBarrier) {
|
|
276
|
+
// check if the render was cancelled
|
|
277
|
+
if (await renderBarrier.ready())
|
|
278
|
+
return;
|
|
279
|
+
this._renderBarrier = null;
|
|
280
|
+
}
|
|
281
|
+
// when the data is loaded let's update the route of the inner
|
|
282
|
+
// this is will only happen if no other route has been requested
|
|
283
|
+
// in the meantime
|
|
284
|
+
more.changeHistory();
|
|
285
|
+
const updateRoute = () => {
|
|
286
|
+
this._route.setSilent(route);
|
|
287
|
+
const siteChanged = this.site.get()?.id !== site.id;
|
|
288
|
+
this._site.setSilent(site);
|
|
289
|
+
this._route.notify();
|
|
290
|
+
if (siteChanged)
|
|
291
|
+
this._site.notify();
|
|
292
|
+
this._onRouteEv.trigger(route.clone(), site);
|
|
293
|
+
};
|
|
294
|
+
this._internal.onLoaded(resp.success, route, site, () => {
|
|
295
|
+
updateRoute();
|
|
296
|
+
return resp.data;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
_onProgress(loading, progress) {
|
|
300
|
+
if (this._loading.get() !== loading)
|
|
301
|
+
this._loading.set(loading);
|
|
302
|
+
if (typeof progress === 'number')
|
|
303
|
+
this._loadingProgress.set(progress);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
class RenderBarrier {
|
|
307
|
+
inner;
|
|
308
|
+
cancelled;
|
|
309
|
+
root;
|
|
310
|
+
constructor() {
|
|
311
|
+
this.inner = new Barrier();
|
|
312
|
+
this.cancelled = false;
|
|
313
|
+
this.root = this.add();
|
|
314
|
+
}
|
|
315
|
+
add() {
|
|
316
|
+
const action = this.inner.add();
|
|
317
|
+
return {
|
|
318
|
+
ready: async () => {
|
|
319
|
+
await action.ready(null);
|
|
320
|
+
return this.cancelled;
|
|
321
|
+
},
|
|
322
|
+
remove: () => action.remove(),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
cancel() {
|
|
326
|
+
this.cancelled = true;
|
|
327
|
+
this.root.remove();
|
|
328
|
+
}
|
|
329
|
+
// returns if the render was cancelled
|
|
330
|
+
ready() {
|
|
331
|
+
return this.root.ready();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type SiteFromGraphQl = {
|
|
2
|
+
id: number;
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
language: string;
|
|
5
|
+
name: string | null;
|
|
6
|
+
handle: string;
|
|
7
|
+
primary: boolean;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Craft Site
|
|
11
|
+
*/
|
|
12
|
+
export default class Site {
|
|
13
|
+
/**
|
|
14
|
+
* The id of the site
|
|
15
|
+
*/
|
|
16
|
+
id: number;
|
|
17
|
+
/**
|
|
18
|
+
* The base url of the site
|
|
19
|
+
*/
|
|
20
|
+
url: URL;
|
|
21
|
+
/**
|
|
22
|
+
* The language of the site
|
|
23
|
+
*
|
|
24
|
+
* ex: de-CH
|
|
25
|
+
*/
|
|
26
|
+
language: string;
|
|
27
|
+
/**
|
|
28
|
+
* The name of the site
|
|
29
|
+
*/
|
|
30
|
+
name: string | null;
|
|
31
|
+
/**
|
|
32
|
+
* The handle of the site
|
|
33
|
+
*/
|
|
34
|
+
handle: string;
|
|
35
|
+
/**
|
|
36
|
+
* Is this the primary site
|
|
37
|
+
*/
|
|
38
|
+
primary: boolean;
|
|
39
|
+
constructor(obj: SiteFromGraphQl);
|
|
40
|
+
/**
|
|
41
|
+
* Returns the uri of the site
|
|
42
|
+
*
|
|
43
|
+
* Never ends with a slash
|
|
44
|
+
*/
|
|
45
|
+
get uri(): string;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=Site.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Site.d.ts","sourceRoot":"","sources":["../../../../src/routing/Site.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,eAAe,GAAG;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,IAAI;IACxB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,GAAG,EAAE,GAAG,CAAC;IAET;;;;OAIG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAEpB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;gBAEL,GAAG,EAAE,eAAe;IAShC;;;;OAIG;IACH,IAAI,GAAG,IAAI,MAAM,CAEhB;CACD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { trimSlashEnd } from './Router.js';
|
|
2
|
+
/**
|
|
3
|
+
* Craft Site
|
|
4
|
+
*/
|
|
5
|
+
export default class Site {
|
|
6
|
+
/**
|
|
7
|
+
* The id of the site
|
|
8
|
+
*/
|
|
9
|
+
id;
|
|
10
|
+
/**
|
|
11
|
+
* The base url of the site
|
|
12
|
+
*/
|
|
13
|
+
url;
|
|
14
|
+
/**
|
|
15
|
+
* The language of the site
|
|
16
|
+
*
|
|
17
|
+
* ex: de-CH
|
|
18
|
+
*/
|
|
19
|
+
language;
|
|
20
|
+
/**
|
|
21
|
+
* The name of the site
|
|
22
|
+
*/
|
|
23
|
+
name;
|
|
24
|
+
/**
|
|
25
|
+
* The handle of the site
|
|
26
|
+
*/
|
|
27
|
+
handle;
|
|
28
|
+
/**
|
|
29
|
+
* Is this the primary site
|
|
30
|
+
*/
|
|
31
|
+
primary;
|
|
32
|
+
constructor(obj) {
|
|
33
|
+
this.id = obj.id;
|
|
34
|
+
this.url = new URL(obj.baseUrl);
|
|
35
|
+
this.language = obj.language;
|
|
36
|
+
this.name = obj.name;
|
|
37
|
+
this.handle = obj.handle;
|
|
38
|
+
this.primary = obj.primary;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns the uri of the site
|
|
42
|
+
*
|
|
43
|
+
* Never ends with a slash
|
|
44
|
+
*/
|
|
45
|
+
get uri() {
|
|
46
|
+
return trimSlashEnd(this.url.pathname);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/routing/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,EAAE,EAAE,KAAK,WAAW,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAC7E,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,eAAe,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function calcKey(data: any): Promise<string>;
|
|
2
|
+
export default class SsrCache {
|
|
3
|
+
private store;
|
|
4
|
+
constructor();
|
|
5
|
+
load<T>(key: string, fn: () => Promise<T>): Promise<any>;
|
|
6
|
+
get<T>(key: string): T | null;
|
|
7
|
+
set<T>(key: string, val: T): T;
|
|
8
|
+
clear(): void;
|
|
9
|
+
private exportAsJson;
|
|
10
|
+
_exportToHead(): string;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=SsrCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SsrCache.d.ts","sourceRoot":"","sources":["../../../../src/ssr/SsrCache.ts"],"names":[],"mappings":"AAAA,wBAAsB,OAAO,CAAC,IAAI,EAAE,GAAG,mBAetC;AAED,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC5B,OAAO,CAAC,KAAK,CAAsB;;IAa7B,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAQ/C,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAI7B,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAI9B,KAAK;IAKL,OAAO,CAAC,YAAY;IAKpB,aAAa,IAAI,MAAM;CAGvB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export async function calcKey(data) {
|
|
2
|
+
// Convert the string data to an ArrayBuffer
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
const dataBuffer = encoder.encode(JSON.stringify(data));
|
|
5
|
+
// Use the Web Crypto API to hash the data with SHA-1
|
|
6
|
+
const hashBuffer = await crypto.subtle.digest('SHA-1', dataBuffer);
|
|
7
|
+
// Convert the ArrayBuffer to a hex string
|
|
8
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer)); // Create an array from the buffer
|
|
9
|
+
const hashHex = hashArray
|
|
10
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
11
|
+
.join(''); // Convert to hex string
|
|
12
|
+
return hashHex;
|
|
13
|
+
}
|
|
14
|
+
export default class SsrCache {
|
|
15
|
+
store;
|
|
16
|
+
constructor() {
|
|
17
|
+
this.store = {};
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
if (typeof window !== 'undefined' && window.SSR_STORE) {
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
this.store = window.SSR_STORE;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/// check if the value is in the cache else calls the fn
|
|
25
|
+
async load(key, fn) {
|
|
26
|
+
if (key in this.store)
|
|
27
|
+
return this.store[key];
|
|
28
|
+
const v = await fn();
|
|
29
|
+
this.set(key, v);
|
|
30
|
+
return v;
|
|
31
|
+
}
|
|
32
|
+
/// returns null if the data does not exists
|
|
33
|
+
get(key) {
|
|
34
|
+
return this.store[key] ?? null;
|
|
35
|
+
}
|
|
36
|
+
set(key, val) {
|
|
37
|
+
return (this.store[key] = val);
|
|
38
|
+
}
|
|
39
|
+
clear() {
|
|
40
|
+
this.store = {};
|
|
41
|
+
}
|
|
42
|
+
// internal
|
|
43
|
+
exportAsJson() {
|
|
44
|
+
return JSON.stringify(this.store).replace(/</g, '\\u003c');
|
|
45
|
+
}
|
|
46
|
+
// internal
|
|
47
|
+
_exportToHead() {
|
|
48
|
+
return `\n\t\t<script>window.SSR_STORE = ${this.exportAsJson()};</script>`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SsrComponents.d.ts","sourceRoot":"","sources":["../../../../src/ssr/SsrComponents.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,aAAa;IACjC,OAAO,CAAC,OAAO,CAAc;;IAO7B,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC;IAOlC,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;CAkB5C"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export default class SsrComponents {
|
|
2
|
+
modules;
|
|
3
|
+
constructor() {
|
|
4
|
+
this.modules = new Set();
|
|
5
|
+
}
|
|
6
|
+
/// ctx: Map
|
|
7
|
+
addToContext(ctx) {
|
|
8
|
+
ctx.set('modules', this.modules);
|
|
9
|
+
}
|
|
10
|
+
/// Returns preload for js and link stylesheet for css
|
|
11
|
+
///
|
|
12
|
+
/// ssrManifest: {}, gets generated by client build
|
|
13
|
+
toHead(ssrManifest) {
|
|
14
|
+
const requirements = new Set();
|
|
15
|
+
for (const mod of this.modules) {
|
|
16
|
+
const deps = ssrManifest[mod] ?? [];
|
|
17
|
+
deps.forEach(dep => requirements.add(dep));
|
|
18
|
+
}
|
|
19
|
+
let head = '';
|
|
20
|
+
for (const req of requirements) {
|
|
21
|
+
if (req.endsWith('.js')) {
|
|
22
|
+
head += `\n\t\t<link rel="preload" href="${req}" as="script">`;
|
|
23
|
+
}
|
|
24
|
+
else if (req.endsWith('.css')) {
|
|
25
|
+
head += `\n\t\t<link rel="stylesheet" href="${req}">`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return head;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/ssr/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,aAAa,MAAM,oBAAoB,CAAC;AAE/C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "crelte",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "Crelte <support@crelte.com>",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"check": "prettier -c .",
|
|
8
|
+
"lint": "eslint ./src",
|
|
9
|
+
"test": "vitest",
|
|
10
|
+
"build": "svelte-package -i ./src",
|
|
11
|
+
"watch": "svelte-package -i ./src -w"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/crelte/crelte"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"src"
|
|
20
|
+
],
|
|
21
|
+
"module": "dist/index.js",
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"types": "dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./client": {
|
|
30
|
+
"types": "./dist/init/client.d.ts",
|
|
31
|
+
"default": "./dist/init/client.js"
|
|
32
|
+
},
|
|
33
|
+
"./server": {
|
|
34
|
+
"types": "./dist/init/server.d.ts",
|
|
35
|
+
"default": "./dist/init/server.js"
|
|
36
|
+
},
|
|
37
|
+
"./graphql": {
|
|
38
|
+
"types": "./dist/graphql/index.d.ts",
|
|
39
|
+
"default": "./dist/graphql/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./routing": {
|
|
42
|
+
"types": "./dist/routing/index.d.ts",
|
|
43
|
+
"default": "./dist/routing/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./ssr": {
|
|
46
|
+
"types": "./dist/ssr/index.d.ts",
|
|
47
|
+
"default": "./dist/ssr/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./loadData": {
|
|
50
|
+
"types": "./dist/loadData/index.d.ts",
|
|
51
|
+
"default": "./dist/loadData/index.js"
|
|
52
|
+
},
|
|
53
|
+
"./plugins": {
|
|
54
|
+
"types": "./dist/plugins/index.d.ts",
|
|
55
|
+
"default": "./dist/plugins/index.js"
|
|
56
|
+
},
|
|
57
|
+
"./cookies": {
|
|
58
|
+
"types": "./dist/cookies/index.d.ts",
|
|
59
|
+
"default": "./dist/cookies/index.js"
|
|
60
|
+
},
|
|
61
|
+
"./blocks": {
|
|
62
|
+
"types": "./dist/blocks/index.d.ts",
|
|
63
|
+
"default": "./dist/blocks/index.js"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"crelte-std": "^0.1.0",
|
|
68
|
+
"svelte": "^4.2.12"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@sveltejs/package": "^2.3.1",
|
|
72
|
+
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
|
73
|
+
"@tsconfig/svelte": "^5.0.2",
|
|
74
|
+
"tslib": "^2.6.2",
|
|
75
|
+
"typescript": "^5.2.2",
|
|
76
|
+
"typescript-eslint": "^7.11.0",
|
|
77
|
+
"vitest": "^2.0.0"
|
|
78
|
+
}
|
|
79
|
+
}
|