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.
@@ -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.75";
@@ -1 +1 @@
1
- export const VERSION = "2.0.0-beta.73";
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 fetchWP(this.wp.infoUrl);
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:\n${err}`,
70
+ `The following error occurred:${err}`,
72
71
  ].join("\n"),
73
72
  };
74
73
  }
@@ -26,7 +26,7 @@ export declare class WPInfo {
26
26
  siteUrl: string;
27
27
  private cached?;
28
28
  constructor(siteUrl: string);
29
- get infoUrl(): string;
29
+ ping(): Promise<Response>;
30
30
  getInfo(force?: boolean): Promise<WPInfoData>;
31
31
  getPluginIssues(): Promise<string[]>;
32
32
  }
@@ -6,8 +6,13 @@ export class WPInfo {
6
6
  constructor(siteUrl) {
7
7
  this.siteUrl = siteUrl;
8
8
  }
9
- get infoUrl() {
10
- return resolveURL(this.siteUrl, "/wp-json/ed/v1/dev/site-info");
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.infoUrl;
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;
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.75",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",