@cfdez11/vex 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/README.md +1383 -0
- package/client/app.webmanifest +14 -0
- package/client/favicon.ico +0 -0
- package/client/services/cache.js +55 -0
- package/client/services/hmr-client.js +22 -0
- package/client/services/html.js +377 -0
- package/client/services/hydrate-client-components.js +97 -0
- package/client/services/hydrate.js +25 -0
- package/client/services/index.js +9 -0
- package/client/services/navigation/create-layouts.js +172 -0
- package/client/services/navigation/create-navigation.js +103 -0
- package/client/services/navigation/index.js +8 -0
- package/client/services/navigation/link-interceptor.js +39 -0
- package/client/services/navigation/metadata.js +23 -0
- package/client/services/navigation/navigate.js +64 -0
- package/client/services/navigation/prefetch.js +43 -0
- package/client/services/navigation/render-page.js +45 -0
- package/client/services/navigation/render-ssr.js +157 -0
- package/client/services/navigation/router.js +48 -0
- package/client/services/navigation/use-query-params.js +225 -0
- package/client/services/navigation/use-route-params.js +76 -0
- package/client/services/reactive.js +231 -0
- package/package.json +24 -0
- package/server/index.js +115 -0
- package/server/prebuild.js +12 -0
- package/server/root.html +15 -0
- package/server/utils/cache.js +89 -0
- package/server/utils/component-processor.js +1526 -0
- package/server/utils/data-cache.js +62 -0
- package/server/utils/delay.js +1 -0
- package/server/utils/files.js +723 -0
- package/server/utils/hmr.js +21 -0
- package/server/utils/router.js +373 -0
- package/server/utils/streaming.js +315 -0
- package/server/utils/template.js +263 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-process TTL cache for server-side data fetching.
|
|
3
|
+
*
|
|
4
|
+
* ISR pages regenerate periodically and call `getData` on every regeneration.
|
|
5
|
+
* If multiple pages or multiple Suspense components call the same external API
|
|
6
|
+
* (e.g. a weather API, a CMS), each regeneration fires N duplicate HTTP requests
|
|
7
|
+
* even though the data is the same for all of them.
|
|
8
|
+
*
|
|
9
|
+
* `withCache(key, ttlSeconds, fn)` deduplicates these calls:
|
|
10
|
+
* - On the first call for a given key it invokes `fn`, caches the result,
|
|
11
|
+
* and returns it.
|
|
12
|
+
* - Subsequent calls within the TTL window return the cached value directly,
|
|
13
|
+
* with no I/O.
|
|
14
|
+
* - After the TTL expires the next call re-fetches and refreshes the cache.
|
|
15
|
+
*
|
|
16
|
+
* Usage inside a `<script server>` getData function:
|
|
17
|
+
*
|
|
18
|
+
* async function getData({ req }) {
|
|
19
|
+
* const weather = await withCache('weather:london', 60, () =>
|
|
20
|
+
* fetch('https://api.example.com/weather?city=london').then(r => r.json())
|
|
21
|
+
* );
|
|
22
|
+
* return { weather };
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* `withCache` is automatically available in every server script — no import needed.
|
|
26
|
+
*
|
|
27
|
+
* Key: any string that uniquely identifies the data source + parameters
|
|
28
|
+
* TTL: seconds the cached value is considered fresh
|
|
29
|
+
* fn: zero-argument function that returns a value or a Promise<value>
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/** @type {Map<string, { value: any, expiresAt: number }>} */
|
|
33
|
+
const cache = new Map();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns a cached value for `key` if still fresh, otherwise calls `fn`,
|
|
37
|
+
* stores the result, and returns it.
|
|
38
|
+
*
|
|
39
|
+
* @template T
|
|
40
|
+
* @param {string} key - Unique cache key.
|
|
41
|
+
* @param {number} ttlSeconds - Seconds before the cached value expires.
|
|
42
|
+
* @param {() => T | Promise<T>} fn - Fetcher called on a cache miss.
|
|
43
|
+
* @returns {T | Promise<T>}
|
|
44
|
+
*/
|
|
45
|
+
export function withCache(key, ttlSeconds, fn) {
|
|
46
|
+
const entry = cache.get(key);
|
|
47
|
+
if (entry && Date.now() < entry.expiresAt) {
|
|
48
|
+
return entry.value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const result = fn();
|
|
52
|
+
|
|
53
|
+
if (result instanceof Promise) {
|
|
54
|
+
return result.then((value) => {
|
|
55
|
+
cache.set(key, { value, expiresAt: Date.now() + ttlSeconds * 1000 });
|
|
56
|
+
return value;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
cache.set(key, { value: result, expiresAt: Date.now() + ttlSeconds * 1000 });
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|