edges-svelte 0.3.1 → 0.5.1
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/README.md
CHANGED
@@ -30,6 +30,7 @@ To enable **Edge-S**, wrap your SvelteKit `handle` hook and serialize the state
|
|
30
30
|
|
31
31
|
```ts
|
32
32
|
// hooks.server.ts
|
33
|
+
import { dev } from '$app/environment';
|
33
34
|
import { edgesHandle } from 'edges-svelte/server';
|
34
35
|
|
35
36
|
export const handle: Handle = async ({ event, resolve }) => {
|
@@ -39,7 +40,7 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|
39
40
|
//...Your handle code, use edgesEvent as a default svelte event (RequestEvent)
|
40
41
|
return resolve(edgesEvent, { transformPageChunk: ({ html }) => serialize(html) });
|
41
42
|
},
|
42
|
-
|
43
|
+
dev
|
43
44
|
);
|
44
45
|
};
|
45
46
|
```
|
package/dist/context/Context.js
CHANGED
@@ -6,6 +6,7 @@ type StoreDeps = {
|
|
6
6
|
};
|
7
7
|
interface CreateProviderOptions<T, I extends Record<string, unknown> = Record<string, unknown>> {
|
8
8
|
inject?: I;
|
9
|
+
cache?: boolean;
|
9
10
|
factory: (args: StoreDeps & I) => T;
|
10
11
|
}
|
11
12
|
export declare const createProvider: <T, I extends Record<string, unknown> = Record<string, unknown>>(options: CreateProviderOptions<T, I>) => (() => T);
|
@@ -1,4 +1,7 @@
|
|
1
1
|
import { createState, createDerivedState, createRawState } from '../store/index.js';
|
2
|
+
import { RequestContext } from '../context/index.js';
|
3
|
+
import { browser } from '$app/environment';
|
4
|
+
const globalClientCache = new Map();
|
2
5
|
export const createProvider = (options) => {
|
3
6
|
const deps = {
|
4
7
|
...{
|
@@ -8,7 +11,30 @@ export const createProvider = (options) => {
|
|
8
11
|
},
|
9
12
|
...(options.inject || {})
|
10
13
|
};
|
11
|
-
|
14
|
+
const cacheKey = Symbol();
|
15
|
+
const cache = options.cache ?? true;
|
16
|
+
return () => {
|
17
|
+
if (!cache) {
|
18
|
+
return options.factory(deps);
|
19
|
+
}
|
20
|
+
if (browser) {
|
21
|
+
if (!globalClientCache.has(cacheKey)) {
|
22
|
+
globalClientCache.set(cacheKey, options.factory(deps));
|
23
|
+
}
|
24
|
+
return globalClientCache.get(cacheKey);
|
25
|
+
}
|
26
|
+
else {
|
27
|
+
const context = RequestContext.current();
|
28
|
+
if (!context.data.providers) {
|
29
|
+
context.data.providers = new Map();
|
30
|
+
}
|
31
|
+
const map = context.data.providers;
|
32
|
+
if (!map.has(cacheKey)) {
|
33
|
+
map.set(cacheKey, options.factory(deps));
|
34
|
+
}
|
35
|
+
return map.get(cacheKey);
|
36
|
+
}
|
37
|
+
};
|
12
38
|
};
|
13
39
|
export const createProviderFactory = (inject) => {
|
14
40
|
return function createInjectedProvider(options) {
|
@@ -14,7 +14,7 @@ const storage = new AsyncLocalStorage();
|
|
14
14
|
* and returns a 204 No Content response instead of a 404 error.
|
15
15
|
*/
|
16
16
|
export const edgesHandle = async (event, callback, silentChromeDevtools = false) => {
|
17
|
-
return await storage.run({ event: event, symbol: Symbol() }, async () => {
|
17
|
+
return await storage.run({ event: event, symbol: Symbol(), data: {} }, async () => {
|
18
18
|
RequestContext.current = () => {
|
19
19
|
const context = storage.getStore();
|
20
20
|
if (context === undefined) {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "edges-svelte",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.1",
|
4
4
|
"license": "MIT",
|
5
5
|
"author": "Pixel1917",
|
6
6
|
"description": "A blazing-fast, extremely lightweight and SSR-friendly store for Svelte",
|
@@ -37,6 +37,10 @@
|
|
37
37
|
"types": "./dist/server/EdgesHandle.d.ts",
|
38
38
|
"svelte": "./dist/server/EdgesHandle.js"
|
39
39
|
},
|
40
|
+
"./context": {
|
41
|
+
"types": "./dist/context/index.d.ts",
|
42
|
+
"svelte": "./dist/context/index.js"
|
43
|
+
},
|
40
44
|
"./state": {
|
41
45
|
"types": "./dist/store/index.d.ts",
|
42
46
|
"svelte": "./dist/store/index.js"
|