eddev 0.2.9 → 0.2.12

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.
@@ -23,11 +23,8 @@ function NextRouter(_a) {
23
23
  var setIsLoading = (0, hooks_1.usePageLoad)(function (s) { return s.setIsLoading; });
24
24
  var route = __assign(__assign({}, nextRoute), { pathname: nextRoute.asPath });
25
25
  (0, react_1.useEffect)(function () {
26
- router_1.Router.events.on("routeChangeStart", function () {
27
- console.log("Loading");
28
- setIsLoading(true);
29
- });
30
- router_1.Router.events.on("routeChangeStart", function () { return setIsLoading(false); });
26
+ router_1.Router.events.on("routeChangeStart", function () { return setIsLoading(true); });
27
+ router_1.Router.events.on("routeChangeComplete", function () { return setIsLoading(false); });
31
28
  router_1.Router.events.on("routeChangeError", function () { return setIsLoading(false); });
32
29
  }, []);
33
30
  return ((0, jsx_runtime_1.jsx)(routing_1.RouterRoot, __assign({ url: nextRoute.asPath, data: data, onNavigateRequest: function (url) {
@@ -10,10 +10,12 @@ export declare const EDConfigSchema: z.ZodObject<{
10
10
  apiOnly: z.ZodOptional<z.ZodBoolean>;
11
11
  endpoints: z.ZodRecord<z.ZodString, z.ZodString>;
12
12
  defaultRevalidate: z.ZodOptional<z.ZodNumber>;
13
+ defaultRevalidateQueries: z.ZodOptional<z.ZodNumber>;
13
14
  }, "strip", z.ZodTypeAny, {
14
15
  themeAssets?: string[] | undefined;
15
16
  apiOnly?: boolean | undefined;
16
17
  defaultRevalidate?: number | undefined;
18
+ defaultRevalidateQueries?: number | undefined;
17
19
  enabled: boolean;
18
20
  uploads: "proxy" | "remote";
19
21
  plugins: "proxy" | "remote";
@@ -23,12 +25,23 @@ export declare const EDConfigSchema: z.ZodObject<{
23
25
  themeAssets?: string[] | undefined;
24
26
  apiOnly?: boolean | undefined;
25
27
  defaultRevalidate?: number | undefined;
28
+ defaultRevalidateQueries?: number | undefined;
26
29
  enabled: boolean;
27
30
  uploads: "proxy" | "remote";
28
31
  plugins: "proxy" | "remote";
29
32
  theme: "copy" | "proxy" | "remote";
30
33
  endpoints: Record<string, string>;
31
34
  }>>;
35
+ cache: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
36
+ props: z.ZodNumber;
37
+ queries: z.ZodNumber;
38
+ }, "strip", z.ZodTypeAny, {
39
+ props: number;
40
+ queries: number;
41
+ }, {
42
+ props: number;
43
+ queries: number;
44
+ }>>>;
32
45
  devUI: z.ZodEnum<["disabled", "enabled"]>;
33
46
  }, "strip", z.ZodTypeAny, {
34
47
  $schema?: string | undefined;
@@ -36,12 +49,17 @@ export declare const EDConfigSchema: z.ZodObject<{
36
49
  themeAssets?: string[] | undefined;
37
50
  apiOnly?: boolean | undefined;
38
51
  defaultRevalidate?: number | undefined;
52
+ defaultRevalidateQueries?: number | undefined;
39
53
  enabled: boolean;
40
54
  uploads: "proxy" | "remote";
41
55
  plugins: "proxy" | "remote";
42
56
  theme: "copy" | "proxy" | "remote";
43
57
  endpoints: Record<string, string>;
44
58
  } | undefined;
59
+ cache?: Record<string, {
60
+ props: number;
61
+ queries: number;
62
+ }> | undefined;
45
63
  devUI: "disabled" | "enabled";
46
64
  }, {
47
65
  $schema?: string | undefined;
@@ -49,12 +67,17 @@ export declare const EDConfigSchema: z.ZodObject<{
49
67
  themeAssets?: string[] | undefined;
50
68
  apiOnly?: boolean | undefined;
51
69
  defaultRevalidate?: number | undefined;
70
+ defaultRevalidateQueries?: number | undefined;
52
71
  enabled: boolean;
53
72
  uploads: "proxy" | "remote";
54
73
  plugins: "proxy" | "remote";
55
74
  theme: "copy" | "proxy" | "remote";
56
75
  endpoints: Record<string, string>;
57
76
  } | undefined;
77
+ cache?: Record<string, {
78
+ props: number;
79
+ queries: number;
80
+ }> | undefined;
58
81
  devUI: "disabled" | "enabled";
59
82
  }>;
60
83
  export declare type EDConfig = z.infer<typeof EDConfigSchema>;
@@ -14,7 +14,14 @@ exports.EDConfigSchema = zod_1.z.object({
14
14
  apiOnly: zod_1.z.boolean().optional(),
15
15
  endpoints: zod_1.z.record(zod_1.z.string(), zod_1.z.string()),
16
16
  defaultRevalidate: zod_1.z.number().optional(),
17
+ defaultRevalidateQueries: zod_1.z.number().optional(),
17
18
  })
18
19
  .optional(),
20
+ cache: zod_1.z
21
+ .record(zod_1.z.string(), zod_1.z.object({
22
+ props: zod_1.z.number(),
23
+ queries: zod_1.z.number(),
24
+ }))
25
+ .optional(),
19
26
  devUI: zod_1.z.enum(["disabled", "enabled"]),
20
27
  });
@@ -4,12 +4,17 @@ export declare function getEDConfig(dir?: string): import("zod").SafeParseReturn
4
4
  themeAssets?: string[] | undefined;
5
5
  apiOnly?: boolean | undefined;
6
6
  defaultRevalidate?: number | undefined;
7
+ defaultRevalidateQueries?: number | undefined;
7
8
  enabled: boolean;
8
9
  uploads: "proxy" | "remote";
9
10
  plugins: "proxy" | "remote";
10
11
  theme: "copy" | "proxy" | "remote";
11
12
  endpoints: Record<string, string>;
12
13
  } | undefined;
14
+ cache?: Record<string, {
15
+ props: number;
16
+ queries: number;
17
+ }> | undefined;
13
18
  devUI: "disabled" | "enabled";
14
19
  }, {
15
20
  $schema?: string | undefined;
@@ -17,12 +22,17 @@ export declare function getEDConfig(dir?: string): import("zod").SafeParseReturn
17
22
  themeAssets?: string[] | undefined;
18
23
  apiOnly?: boolean | undefined;
19
24
  defaultRevalidate?: number | undefined;
25
+ defaultRevalidateQueries?: number | undefined;
20
26
  enabled: boolean;
21
27
  uploads: "proxy" | "remote";
22
28
  plugins: "proxy" | "remote";
23
29
  theme: "copy" | "proxy" | "remote";
24
30
  endpoints: Record<string, string>;
25
31
  } | undefined;
32
+ cache?: Record<string, {
33
+ props: number;
34
+ queries: number;
35
+ }> | undefined;
26
36
  devUI: "disabled" | "enabled";
27
37
  }>;
28
38
  export declare function getEDConfigUnwrapped(dir?: string): {
@@ -31,12 +41,17 @@ export declare function getEDConfigUnwrapped(dir?: string): {
31
41
  themeAssets?: string[] | undefined;
32
42
  apiOnly?: boolean | undefined;
33
43
  defaultRevalidate?: number | undefined;
44
+ defaultRevalidateQueries?: number | undefined;
34
45
  enabled: boolean;
35
46
  uploads: "proxy" | "remote";
36
47
  plugins: "proxy" | "remote";
37
48
  theme: "copy" | "proxy" | "remote";
38
49
  endpoints: Record<string, string>;
39
50
  } | undefined;
51
+ cache?: Record<string, {
52
+ props: number;
53
+ queries: number;
54
+ }> | undefined;
40
55
  devUI: "disabled" | "enabled";
41
56
  };
42
57
  export declare function getEDConfigFile(dir?: string): string;
@@ -4,12 +4,17 @@ export declare function parseConfig(config: any): import("zod").SafeParseReturnT
4
4
  themeAssets?: string[] | undefined;
5
5
  apiOnly?: boolean | undefined;
6
6
  defaultRevalidate?: number | undefined;
7
+ defaultRevalidateQueries?: number | undefined;
7
8
  enabled: boolean;
8
9
  uploads: "proxy" | "remote";
9
10
  plugins: "proxy" | "remote";
10
11
  theme: "copy" | "proxy" | "remote";
11
12
  endpoints: Record<string, string>;
12
13
  } | undefined;
14
+ cache?: Record<string, {
15
+ props: number;
16
+ queries: number;
17
+ }> | undefined;
13
18
  devUI: "disabled" | "enabled";
14
19
  }, {
15
20
  $schema?: string | undefined;
@@ -17,11 +22,16 @@ export declare function parseConfig(config: any): import("zod").SafeParseReturnT
17
22
  themeAssets?: string[] | undefined;
18
23
  apiOnly?: boolean | undefined;
19
24
  defaultRevalidate?: number | undefined;
25
+ defaultRevalidateQueries?: number | undefined;
20
26
  enabled: boolean;
21
27
  uploads: "proxy" | "remote";
22
28
  plugins: "proxy" | "remote";
23
29
  theme: "copy" | "proxy" | "remote";
24
30
  endpoints: Record<string, string>;
25
31
  } | undefined;
32
+ cache?: Record<string, {
33
+ props: number;
34
+ queries: number;
35
+ }> | undefined;
26
36
  devUI: "disabled" | "enabled";
27
37
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eddev",
3
- "version": "0.2.9",
3
+ "version": "0.2.12",
4
4
  "main": "./index.js",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -11,10 +11,13 @@ export async function fetchWordpressProps(pathname: string) {
11
11
  console.log("Fetching from", propsURL)
12
12
 
13
13
  // Make the request
14
- let response = await fetchWP(propsURL, {})
14
+ let response = await fetchWP(propsURL, {}, async (response) => ({
15
+ text: await response.text(),
16
+ status: response.status,
17
+ }))
15
18
 
16
19
  // Convert to text, rather than JSON — so we can do find-and-replace
17
- let text = await response.text()
20
+ let text = response.text
18
21
 
19
22
  // Convert absolute site URL details to relative paths
20
23
  text = text.replace(new RegExp(origin.replace(/(http|https)/, `https?`) + "([a-z0-9-_./]+)", "g"), (url) => {
@@ -1,17 +1,35 @@
1
1
  import { Agent } from "https"
2
2
  import { fetch } from "cross-fetch"
3
+ import { callSWR } from "./swr"
3
4
 
4
5
  // Used to allow self-signed certificates
5
6
  const agent = new Agent({
6
7
  rejectUnauthorized: false,
7
8
  })
8
9
 
9
- export const fetchWP: typeof fetch = async (url, opts) => {
10
- // TODO basic auth via env variable
11
- // https://stackoverflow.com/questions/43842793/basic-authentication-with-fetch
12
- return fetch(url, {
13
- ...opts,
14
- // @ts-ignore
15
- agent: url.includes("https") ? agent : undefined,
16
- })
10
+ export async function fetchWP<T>(url: string, opts: RequestInit, handler: (response: Response) => Promise<T>) {
11
+ const getData = () => {
12
+ // TODO — basic auth via env variable
13
+ // https://stackoverflow.com/questions/43842793/basic-authentication-with-fetch
14
+ return fetch(url, {
15
+ ...opts,
16
+ // @ts-ignore
17
+ agent: url.includes("https") ? agent : undefined,
18
+ })
19
+ }
20
+
21
+ const requestIsGET = !opts || !opts.method || opts.method.toUpperCase() === "GET"
22
+ if (requestIsGET) {
23
+ return callSWR(url as string, async (setExpiry) => {
24
+ const response = await getData()
25
+ const cacheDuration = parseInt(response.headers.get("x-ed-cache-duration") || "") || 0
26
+ setExpiry(cacheDuration)
27
+ console.log(`CACHE for ${cacheDuration}: ${url}`)
28
+ const result = await handler(response)
29
+ return result
30
+ })
31
+ } else {
32
+ const response = await getData()
33
+ return handler(response)
34
+ }
17
35
  }
@@ -0,0 +1,43 @@
1
+ type CacheItem = {
2
+ data: Promise<any>
3
+ expires: Date
4
+ pending: boolean
5
+ }
6
+
7
+ const cache: { [key: string]: CacheItem } = {}
8
+
9
+ export function callSWR<T>(key: string, func: (setExpiry: (duration: number) => void) => Promise<T>): Promise<T> {
10
+ if (cache[key]) {
11
+ if (new Date() < cache[key].expires || cache[key].pending) {
12
+ // Cache is valid
13
+ console.log("VALID")
14
+ return cache[key].data
15
+ } else {
16
+ // Cache has expired - serve old one, but reload in background
17
+ cache[key].pending = true
18
+ console.log("EXPIRED")
19
+ setImmediate(async () => {
20
+ const data = await func((duration) => {
21
+ cache[key].expires = new Date(Date.now() + duration * 1000)
22
+ console.log("EXPIRY -> ", cache[key].expires)
23
+ })
24
+ cache[key].data = Promise.resolve(data)
25
+ cache[key].pending = false
26
+ })
27
+ return cache[key].data
28
+ }
29
+ } else {
30
+ // Not cached at all, user must wait for the cache to populate
31
+ console.log("MISS")
32
+ const item = {
33
+ data: func((duration) => {
34
+ item.expires = new Date(Date.now() + duration * 1000)
35
+ console.log("EXPIRY -> ", item.expires)
36
+ }),
37
+ expires: new Date(Date.now() + 10000),
38
+ pending: false,
39
+ }
40
+ cache[key] = item
41
+ return item.data
42
+ }
43
+ }
@@ -13,6 +13,18 @@ module.exports = (() => {
13
13
  rewrites() {
14
14
  return {
15
15
  afterFiles: [
16
+ {
17
+ source: "/sitemap.xml",
18
+ destination: process.env.SITE_URL + "/sitemap.xml",
19
+ },
20
+ {
21
+ source: "/:path*-sitemap.xml",
22
+ destination: process.env.SITE_URL + "/:path*-sitemap.xml",
23
+ },
24
+ settings.serverless.uploads === "proxy" && {
25
+ source: "/wp-content/uploads/:path*",
26
+ destination: process.env.SITE_URL + "/wp-content/uploads/:path*",
27
+ },
16
28
  settings.serverless.uploads === "proxy" && {
17
29
  source: "/wp-content/uploads/:path*",
18
30
  destination: process.env.SITE_URL + "/wp-content/uploads/:path*",
@@ -15,7 +15,6 @@ export async function getStaticPaths(): Promise<GetStaticPathsResult> {
15
15
 
16
16
  export async function getStaticProps({ params }: GetStaticPropsContext): Promise<GetStaticPropsResult<any>> {
17
17
  const result = await fetchWordpressProps((params?.slug as string[]).join("/"))
18
- console.log("RESULT", result)
19
18
  if (result.status === 404) {
20
19
  return {
21
20
  notFound: true,
@@ -29,7 +28,6 @@ export async function getStaticProps({ params }: GetStaticPropsContext): Promise
29
28
  }
30
29
  }
31
30
  const revalidate = result.data.revalidate || settings.serverless?.defaultRevalidate || false
32
- console.log("REVALIDATE", revalidate)
33
31
  return {
34
32
  props: result.data,
35
33
  revalidate: revalidate,
@@ -17,7 +17,6 @@ const validProxyPaths = {
17
17
 
18
18
  export default async function (req: any, res: any) {
19
19
  // Ensure that the request is for a proxy path
20
- console.log("METHOD", req.query.method)
21
20
  const proxyPath = validProxyPaths[req.query.method[0] as keyof typeof validProxyPaths]!
22
21
 
23
22
  if (!proxyPath) {
@@ -28,14 +27,18 @@ export default async function (req: any, res: any) {
28
27
 
29
28
  const finalPath = proxyPath.path.replace("*", req.query.method.slice(1).join("/"))
30
29
 
31
- const response = await fetchWP(process.env.SITE_URL + finalPath, {
32
- method: proxyPath.method,
33
- headers: {
34
- "Content-Type": "application/json",
30
+ const response = await fetchWP(
31
+ process.env.SITE_URL + finalPath,
32
+ {
33
+ method: proxyPath.method,
34
+ headers: {
35
+ "Content-Type": "application/json",
36
+ },
35
37
  },
36
- })
37
-
38
- const payload = await response.json()
38
+ async (response) => ({
39
+ payload: await response.json(),
40
+ })
41
+ )
39
42
 
40
- res.status(200).json(payload)
43
+ res.status(200).json(response.payload)
41
44
  }