eddev 2.0.0-beta.73 → 2.0.0-beta.74

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.
@@ -2,7 +2,7 @@ import { parseURL, stringifyParsedURL, withQuery, withTrailingSlash } from "ufo"
2
2
  import { fetchWP } from "../../node/utils/fetch-wp.js";
3
3
  import { filterHeader } from "./utils/headers.js";
4
4
  import { createUrlReplacer } from "./utils/replace-host.js";
5
- import { swr } from "./utils/swr-cache.js";
5
+ import { pageCache, queryCache, swr } from "./utils/swr-cache.js";
6
6
  const PROXY_RESPONSE_HEADERS = ["content-type", "set-cookie", /^x-/, "cache-control", /woocommerce/];
7
7
  const PROXY_REQUEST_HEADERS = [
8
8
  "content-type",
@@ -93,6 +93,7 @@ export class ServerContext {
93
93
  let cacheKey = "fetchRouteData:" + req.pathname;
94
94
  const result = await swr({
95
95
  key: cacheKey,
96
+ cache: pageCache,
96
97
  getFreshValue: async (ctx) => {
97
98
  ctx.metadata.createdTime;
98
99
  const fetchUrl = withQuery(withTrailingSlash(req.pathname), {
@@ -148,6 +149,7 @@ export class ServerContext {
148
149
  async fetchAppData() {
149
150
  const data = await swr({
150
151
  key: "fetchAppData",
152
+ cache: pageCache,
151
153
  getFreshValue: async (ctx) => {
152
154
  const response = await this.fetchOrigin("/_appdata", {
153
155
  cache: "no-cache",
@@ -181,37 +183,45 @@ export class ServerContext {
181
183
  }
182
184
  async fetchNamedQuery(req) {
183
185
  const url = `/wp-json/ed/v1/query/${req.name}?params=${encodeURIComponent(JSON.stringify(req.params))}`;
184
- const hasVariedHeaders = VARIES_HEADERS.some((key) => !!req.headers[key]);
185
186
  const key = `fetchNamedQuery:${req.name}:${JSON.stringify(req.params)}`;
186
- const result = await swr({
187
- key,
188
- getFreshValue: async (ctx) => {
189
- const result = await this.fetchOrigin(url, {
190
- cache: "no-cache",
191
- replaceUrls: true,
192
- headers: {
193
- ...this.extractRequestHeaders(req.headers),
194
- "Content-Type": "application/json",
195
- Accept: "application/json",
196
- },
197
- });
198
- const preferredCacheDuration = parseInt(result.headers.get("x-ed-cache-duration") ?? "");
199
- if (isFinite(preferredCacheDuration)) {
200
- ctx.metadata.ttl = preferredCacheDuration * 1000;
201
- }
202
- let resultStatus = result.status;
203
- let resultHeaders = result.headers;
204
- let resultData = await result.json();
205
- if (resultData && typeof resultData === "object") {
206
- resultData.__generated = new Date().toISOString();
207
- }
208
- return {
209
- status: resultStatus,
210
- headers: resultHeaders,
211
- data: JSON.stringify(resultData),
212
- };
213
- },
214
- });
187
+ const fetch = async () => {
188
+ const result = await this.fetchOrigin(url, {
189
+ cache: "no-cache",
190
+ replaceUrls: true,
191
+ headers: {
192
+ ...this.extractRequestHeaders(req.headers),
193
+ "Content-Type": "application/json",
194
+ Accept: "application/json",
195
+ },
196
+ });
197
+ const preferredCacheDuration = parseInt(result.headers.get("x-ed-cache-duration") ?? "");
198
+ let resultStatus = result.status;
199
+ let resultHeaders = result.headers;
200
+ let resultData = await result.json();
201
+ if (resultData && typeof resultData === "object") {
202
+ resultData.__generated = new Date().toISOString();
203
+ }
204
+ return {
205
+ status: resultStatus,
206
+ headers: resultHeaders,
207
+ data: JSON.stringify(resultData),
208
+ cacheTime: isFinite(preferredCacheDuration) ? preferredCacheDuration * 1000 : undefined,
209
+ };
210
+ };
211
+ // If headers like 'Authorization' are set, then we shouldn't cache.
212
+ const hasVariedHeaders = VARIES_HEADERS.some((key) => !!req.headers[key]);
213
+ // Get the result, either from cache or by fetching.
214
+ const result = hasVariedHeaders
215
+ ? await fetch()
216
+ : await swr({
217
+ key,
218
+ cache: queryCache,
219
+ getFreshValue: async (ctx) => {
220
+ const result = await fetch();
221
+ ctx.metadata.ttl = result.cacheTime;
222
+ return result;
223
+ },
224
+ });
215
225
  return new Response(result.data, {
216
226
  status: result.status,
217
227
  headers: result.headers,
@@ -1,4 +1,4 @@
1
1
  import { Cache, CachifiedOptions } from "@epic-web/cachified";
2
- export declare function swr<T>(args: Omit<CachifiedOptions<T>, "cache"> & {
3
- cache?: Cache;
4
- }): Promise<T>;
2
+ export declare const pageCache: Cache<any>;
3
+ export declare const queryCache: Cache<any>;
4
+ export declare function swr<T>(args: CachifiedOptions<T>): Promise<T>;
@@ -1,27 +1,31 @@
1
1
  import { LRUCache } from "lru-cache";
2
- import { cachified, totalTtl, verboseReporter } from "@epic-web/cachified";
3
- const lruInstance = new LRUCache({ max: 1000 });
4
- const lru = {
5
- set(key, value) {
6
- const ttl = totalTtl(value?.metadata);
7
- return lruInstance.set(key, value, {
8
- ttl: ttl === Infinity ? undefined : ttl,
9
- start: value?.metadata?.createdTime,
10
- });
11
- },
12
- get(key) {
13
- return lruInstance.get(key);
14
- },
15
- delete(key) {
16
- return lruInstance.delete(key);
17
- },
18
- };
2
+ import { cachified, totalTtl } from "@epic-web/cachified";
3
+ function createCache() {
4
+ const lruInstance = new LRUCache({ max: 1000 });
5
+ const lru = {
6
+ set(key, value) {
7
+ const ttl = totalTtl(value?.metadata);
8
+ return lruInstance.set(key, value, {
9
+ ttl: ttl === Infinity ? undefined : ttl,
10
+ start: value?.metadata?.createdTime,
11
+ });
12
+ },
13
+ get(key) {
14
+ return lruInstance.get(key);
15
+ },
16
+ delete(key) {
17
+ return lruInstance.delete(key);
18
+ },
19
+ };
20
+ return lru;
21
+ }
22
+ export const pageCache = createCache();
23
+ export const queryCache = createCache();
19
24
  export function swr(args) {
20
25
  return cachified({
21
26
  ttl: args.ttl ?? 10_000,
22
- swr: 3600_000 * 12,
27
+ swr: 3600_000,
23
28
  fallbackToCache: 15_000,
24
- cache: args.cache ?? lru,
25
29
  ...args,
26
- }, verboseReporter());
30
+ });
27
31
  }
@@ -1 +1 @@
1
- export declare const VERSION = "2.0.0-beta.73";
1
+ export declare const VERSION = "2.0.0-beta.74";
@@ -1 +1 @@
1
- export const VERSION = "2.0.0-beta.73";
1
+ export const VERSION = "2.0.0-beta.74";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eddev",
3
- "version": "2.0.0-beta.73",
3
+ "version": "2.0.0-beta.74",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",