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
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
27
|
+
swr: 3600_000,
|
|
23
28
|
fallbackToCache: 15_000,
|
|
24
|
-
cache: args.cache ?? lru,
|
|
25
29
|
...args,
|
|
26
|
-
}
|
|
30
|
+
});
|
|
27
31
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "2.0.0-beta.
|
|
1
|
+
export declare const VERSION = "2.0.0-beta.74";
|
package/dist/node/cli/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "2.0.0-beta.
|
|
1
|
+
export const VERSION = "2.0.0-beta.74";
|