eddev 2.0.0-beta.73 → 2.0.0-beta.75
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/dist/app/server/server-context.js +41 -31
- package/dist/app/server/utils/swr-cache.d.ts +3 -3
- package/dist/app/server/utils/swr-cache.js +24 -20
- package/dist/node/cli/version.d.ts +1 -1
- package/dist/node/cli/version.js +1 -1
- package/dist/node/project/project.js +2 -3
- package/dist/node/project/wp-info.d.ts +1 -1
- package/dist/node/project/wp-info.js +8 -3
- package/package.json +1 -1
|
@@ -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.75";
|
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.75";
|
|
@@ -12,7 +12,6 @@ import { loadRouteManifest } from "./manifest/routes-manifest.js";
|
|
|
12
12
|
import { loadViewManifest } from "./manifest/view-manifest.js";
|
|
13
13
|
import { loadWidgetManifest } from "./manifest/widget-manifest.js";
|
|
14
14
|
import { WPInfo } from "./wp-info.js";
|
|
15
|
-
import { fetchWP } from "../utils/fetch-wp.js";
|
|
16
15
|
const console = createConsole("Project", "project");
|
|
17
16
|
export const projectLog = console;
|
|
18
17
|
/**
|
|
@@ -47,7 +46,7 @@ export class Project {
|
|
|
47
46
|
}
|
|
48
47
|
async verifyOriginAccess() {
|
|
49
48
|
try {
|
|
50
|
-
const response = await
|
|
49
|
+
const response = await this.wp.ping();
|
|
51
50
|
if (!response.ok) {
|
|
52
51
|
return {
|
|
53
52
|
success: false,
|
|
@@ -68,7 +67,7 @@ export class Project {
|
|
|
68
67
|
message: [
|
|
69
68
|
`Failed to access WordPress origin at ${this.origin}`,
|
|
70
69
|
`Using API Key?: ${ProjectEnvUtils.getSafe("SITE_API_KEY") ? "Yes" : "No"}`,
|
|
71
|
-
`The following error occurred
|
|
70
|
+
`The following error occurred:${err}`,
|
|
72
71
|
].join("\n"),
|
|
73
72
|
};
|
|
74
73
|
}
|
|
@@ -6,8 +6,13 @@ export class WPInfo {
|
|
|
6
6
|
constructor(siteUrl) {
|
|
7
7
|
this.siteUrl = siteUrl;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
async ping() {
|
|
10
|
+
if (!this.siteUrl) {
|
|
11
|
+
throw new Error("Attempted to ping WordPress, but SITE_URL was not present in the environment.");
|
|
12
|
+
}
|
|
13
|
+
const url = resolveURL(this.siteUrl, "/wp-json/ed/v1/handshake");
|
|
14
|
+
const response = await fetchWP(url);
|
|
15
|
+
return response;
|
|
11
16
|
}
|
|
12
17
|
async getInfo(force = false) {
|
|
13
18
|
if (!this.siteUrl) {
|
|
@@ -15,7 +20,7 @@ export class WPInfo {
|
|
|
15
20
|
}
|
|
16
21
|
if (this.cached && !force)
|
|
17
22
|
return this.cached;
|
|
18
|
-
const url = this.
|
|
23
|
+
const url = resolveURL(this.siteUrl, "/wp-json/ed/v1/dev/site-info");
|
|
19
24
|
const response = await fetchWP(url);
|
|
20
25
|
const result = (await response.json());
|
|
21
26
|
this.cached = result;
|