eddev 2.0.0-beta.12 → 2.0.0-beta.121

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.
Files changed (224) hide show
  1. package/css/editor-styles.css +4 -0
  2. package/dist/app/entry/HydrationOverlay.d.ts +1 -0
  3. package/dist/app/entry/HydrationOverlay.js +2 -0
  4. package/dist/app/entry/MetaTags.d.ts +7 -0
  5. package/dist/app/entry/MetaTags.js +17 -0
  6. package/dist/app/entry/boot-admin.js +9 -3
  7. package/dist/app/entry/hydration-script.d.ts +1 -0
  8. package/dist/app/entry/hydration-script.js +18 -0
  9. package/dist/app/entry/spa-root.js +3 -5
  10. package/dist/app/entry/ssr-root-client.d.ts +3 -1
  11. package/dist/app/entry/ssr-root-client.js +18 -6
  12. package/dist/app/entry/ssr-root.d.ts +3 -4
  13. package/dist/app/entry/ssr-root.js +19 -19
  14. package/dist/app/lib/admin/index.d.ts +2 -2
  15. package/dist/app/lib/admin/index.js +2 -2
  16. package/dist/app/lib/admin/installFieldTypes.js +3 -1
  17. package/dist/app/lib/admin/runWidgets.js +1 -1
  18. package/dist/app/lib/blocks/ContentBlocks.d.ts +1 -1
  19. package/dist/app/lib/blocks/ContentBlocks.js +5 -5
  20. package/dist/app/lib/blocks/EditableText.d.ts +15 -2
  21. package/dist/app/lib/blocks/EditableText.js +10 -4
  22. package/dist/app/lib/blocks/InnerBlocks.d.ts +21 -7
  23. package/dist/app/lib/blocks/InnerBlocks.js +75 -28
  24. package/dist/app/lib/blocks/block-utils.d.ts +2 -2
  25. package/dist/app/lib/blocks/block-utils.js +2 -2
  26. package/dist/app/lib/blocks/defineBlock.d.ts +3 -0
  27. package/dist/app/lib/blocks/defineBlock.js +7 -0
  28. package/dist/app/lib/blocks/editor/EditorHighlights.d.ts +7 -0
  29. package/dist/app/lib/blocks/editor/EditorHighlights.js +164 -0
  30. package/dist/app/lib/blocks/editor/EditorSupport.js +17 -12
  31. package/dist/app/lib/blocks/editor/ErrorBoundaryEditor.d.ts +1 -1
  32. package/dist/app/lib/blocks/editor/block-templates.d.ts +6 -0
  33. package/dist/app/lib/blocks/editor/block-templates.js +64 -0
  34. package/dist/app/lib/blocks/editor/create-block.d.ts +9 -0
  35. package/dist/app/lib/blocks/editor/create-block.js +13 -0
  36. package/dist/app/lib/blocks/editor/editor-config.d.ts +70 -7
  37. package/dist/app/lib/blocks/editor/editor-config.js +29 -62
  38. package/dist/app/lib/blocks/editor/installGutenbergHooks.d.ts +3 -0
  39. package/dist/app/lib/blocks/editor/installGutenbergHooks.js +105 -16
  40. package/dist/app/lib/blocks/editor/root-blocks.d.ts +6 -0
  41. package/dist/app/lib/blocks/editor/root-blocks.js +30 -0
  42. package/dist/app/lib/blocks/editor/usePostEditor.d.ts +1 -1
  43. package/dist/app/lib/blocks/index.d.ts +10 -9
  44. package/dist/app/lib/blocks/index.js +10 -9
  45. package/dist/app/lib/blocks/inline-editing.d.ts +9 -1
  46. package/dist/app/lib/blocks/inline-editing.js +7 -5
  47. package/dist/app/lib/devtools/components/BreakpointIndicator.js +1 -1
  48. package/dist/app/lib/devtools/components/DevUI.js +4 -3
  49. package/dist/app/lib/devtools/components/GridIndicator.d.ts +1 -0
  50. package/dist/app/lib/devtools/components/GridIndicator.js +29 -0
  51. package/dist/app/lib/devtools/hooks/usePersistState.d.ts +1 -1
  52. package/dist/app/lib/devtools/hooks/usePersistState.js +11 -2
  53. package/dist/app/lib/devtools/hooks/useTailwind.d.ts +2305 -1261
  54. package/dist/app/lib/devtools/hooks/useTailwind.js +1 -1
  55. package/dist/app/lib/devtools/index.d.ts +1 -1
  56. package/dist/app/lib/devtools/index.js +1 -1
  57. package/dist/app/lib/devtools/loader.js +8 -7
  58. package/dist/app/lib/devtools/useQueryDebug.d.ts +7 -1
  59. package/dist/app/lib/devtools/useQueryDebug.js +5 -8
  60. package/dist/app/lib/dynamic/dynamic.d.ts +1 -1
  61. package/dist/app/lib/dynamic/dynamic.js +5 -1
  62. package/dist/app/lib/dynamic/index.d.ts +1 -1
  63. package/dist/app/lib/dynamic/index.js +1 -1
  64. package/dist/app/lib/hooks/index.d.ts +4 -5
  65. package/dist/app/lib/hooks/index.js +4 -5
  66. package/dist/app/lib/hooks/queryUtils.d.ts +37 -3
  67. package/dist/app/lib/hooks/queryUtils.js +66 -26
  68. package/dist/app/lib/hooks/useAppData.js +12 -1
  69. package/dist/app/lib/hooks/useRPC.d.ts +0 -4
  70. package/dist/app/lib/hooks/useRPC.js +1 -8
  71. package/dist/app/lib/internal/finalize-rpc.d.ts +17 -0
  72. package/dist/app/lib/internal/finalize-rpc.js +3 -0
  73. package/dist/app/lib/internal/index.d.ts +5 -4
  74. package/dist/app/lib/internal/index.js +5 -4
  75. package/dist/app/lib/internal/read-admin-manifest.d.ts +1 -1
  76. package/dist/app/lib/legacy-stitches/createStitches.d.ts +21 -21
  77. package/dist/app/lib/legacy-stitches/createStitches.js +1 -1
  78. package/dist/app/lib/legacy-stitches/index.d.ts +1 -1
  79. package/dist/app/lib/legacy-stitches/index.js +1 -1
  80. package/dist/app/lib/routing/components/BackButton.d.ts +49 -0
  81. package/dist/app/lib/routing/components/BackButton.js +47 -0
  82. package/dist/app/lib/routing/components/BrowserRouter.d.ts +5 -1
  83. package/dist/app/lib/routing/components/BrowserRouter.js +98 -19
  84. package/dist/app/lib/routing/components/ClientOnly.d.ts +1 -1
  85. package/dist/app/lib/routing/components/ClientOnly.js +2 -2
  86. package/dist/app/lib/routing/components/Link.d.ts +1 -0
  87. package/dist/app/lib/routing/components/Link.js +11 -12
  88. package/dist/app/lib/routing/components/RouteRenderer.d.ts +1 -1
  89. package/dist/app/lib/routing/components/RouteRenderer.js +7 -6
  90. package/dist/app/lib/routing/components/SSRRouter.d.ts +2 -2
  91. package/dist/app/lib/routing/components/SSRRouter.js +5 -6
  92. package/dist/app/lib/routing/components/ScrollRestoration.js +5 -2
  93. package/dist/app/lib/routing/context.d.ts +8 -5
  94. package/dist/app/lib/routing/context.js +13 -96
  95. package/dist/app/lib/routing/hooks/useRestorableState.d.ts +2 -1
  96. package/dist/app/lib/routing/hooks/useRestorableState.js +2 -1
  97. package/dist/app/lib/routing/hooks/useRoute.d.ts +16 -1
  98. package/dist/app/lib/routing/hooks/useRoute.js +22 -1
  99. package/dist/app/lib/routing/hooks/useRouteMeta.d.ts +5 -0
  100. package/dist/app/lib/routing/hooks/useRouteMeta.js +9 -0
  101. package/dist/app/lib/routing/hooks/useRouteTransition.d.ts +1 -1
  102. package/dist/app/lib/routing/hooks/useRouteTransition.js +1 -1
  103. package/dist/app/lib/routing/hooks/useRouter.d.ts +1 -1
  104. package/dist/app/lib/routing/hooks/useRouter.js +1 -1
  105. package/dist/app/lib/routing/hooks/useRouterEvents.d.ts +1 -1
  106. package/dist/app/lib/routing/hooks/useRouterEvents.js +1 -1
  107. package/dist/app/lib/routing/hooks/useRouterState.d.ts +1 -1
  108. package/dist/app/lib/routing/hooks/useRouterState.js +1 -1
  109. package/dist/app/lib/routing/hooks/useSearchParams.js +2 -2
  110. package/dist/app/lib/routing/index.d.ts +14 -13
  111. package/dist/app/lib/routing/index.js +14 -13
  112. package/dist/app/lib/routing/loader.d.ts +2 -2
  113. package/dist/app/lib/routing/loader.js +20 -11
  114. package/dist/app/lib/routing/types.d.ts +37 -10
  115. package/dist/app/lib/routing/utils.d.ts +5 -2
  116. package/dist/app/lib/routing/utils.js +37 -4
  117. package/dist/app/lib/{hooks → runtime}/apiConfig.d.ts +6 -2
  118. package/dist/app/lib/runtime/apiConfig.js +6 -0
  119. package/dist/app/lib/runtime/errorHandling.d.ts +39 -0
  120. package/dist/app/lib/runtime/errorHandling.js +6 -0
  121. package/dist/app/lib/runtime/index.d.ts +2 -0
  122. package/dist/app/lib/runtime/index.js +2 -0
  123. package/dist/app/lib/views/index.d.ts +1 -1
  124. package/dist/app/lib/views/index.js +1 -1
  125. package/dist/app/server/defineRouter.d.ts +2 -0
  126. package/dist/app/server/defineRouter.js +4 -0
  127. package/dist/app/server/index.d.ts +5 -3
  128. package/dist/app/server/index.js +5 -3
  129. package/dist/app/server/proxy-wp-admin.d.ts +1 -2
  130. package/dist/app/server/proxy-wp-admin.js +41 -14
  131. package/dist/app/server/render-ssr-page.d.ts +27 -2
  132. package/dist/app/server/render-ssr-page.js +192 -12
  133. package/dist/app/server/rpc.d.ts +56 -0
  134. package/dist/app/server/rpc.js +18 -0
  135. package/dist/app/server/server-context.d.ts +44 -4
  136. package/dist/app/server/server-context.js +305 -28
  137. package/dist/app/server/utils/replace-host.d.ts +1 -1
  138. package/dist/app/server/utils/replace-host.js +10 -2
  139. package/dist/app/server/utils/swr-cache.d.ts +4 -0
  140. package/dist/app/server/utils/swr-cache.js +31 -0
  141. package/dist/app/utils/APIProvider.d.ts +2 -0
  142. package/dist/app/utils/APIProvider.js +5 -0
  143. package/dist/app/utils/BlockErrorBoundary.d.ts +19 -0
  144. package/dist/app/utils/BlockErrorBoundary.js +38 -0
  145. package/dist/app/utils/ErrorMessage.d.ts +5 -0
  146. package/dist/app/utils/ErrorMessage.js +14 -0
  147. package/dist/app/utils/RouteErrorBoundary.d.ts +18 -0
  148. package/dist/app/utils/RouteErrorBoundary.js +38 -0
  149. package/dist/app/utils/asset-capture.d.ts +2 -0
  150. package/dist/app/utils/asset-capture.js +5 -0
  151. package/dist/app/utils/hydration-debugger.d.ts +13 -0
  152. package/dist/app/utils/hydration-debugger.js +11 -0
  153. package/dist/app/utils/query-client.d.ts +2 -0
  154. package/dist/app/utils/query-client.js +5 -1
  155. package/dist/app/utils/query-monitor.d.ts +26 -0
  156. package/dist/app/utils/query-monitor.js +7 -0
  157. package/dist/app/utils/trpc-client.d.ts +2 -0
  158. package/dist/app/utils/trpc-client.js +39 -0
  159. package/dist/node/cli/cli-worker.js +10 -5
  160. package/dist/node/cli/cli.js +81 -11
  161. package/dist/node/cli/display/CLIApp.js +3 -6
  162. package/dist/node/cli/display/boot-cli-app.js +1 -1
  163. package/dist/node/cli/display/tools/CreateBlock.d.ts +1 -1
  164. package/dist/node/cli/display/tools/cli-tools.d.ts +1 -11
  165. package/dist/node/cli/display/tools/cli-tools.js +9 -9
  166. package/dist/node/cli/version.d.ts +1 -1
  167. package/dist/node/cli/version.js +1 -1
  168. package/dist/node/compiler/build-vinxi.js +2 -1
  169. package/dist/node/compiler/bundler.admin.d.ts +1 -1
  170. package/dist/node/compiler/bundler.admin.js +1 -1
  171. package/dist/node/compiler/bundler.frontend.js +1 -1
  172. package/dist/node/compiler/dev-server.js +10 -0
  173. package/dist/node/compiler/get-vite-config.d.ts +7 -1
  174. package/dist/node/compiler/get-vite-config.js +96 -16
  175. package/dist/node/compiler/vinxi-app.d.ts +12 -0
  176. package/dist/node/compiler/vinxi-app.js +139 -32
  177. package/dist/node/compiler/vinxi-codegen.js +345 -112
  178. package/dist/node/graphql/graphql-codegen.d.ts +11 -1
  179. package/dist/node/graphql/graphql-codegen.js +210 -33
  180. package/dist/node/graphql/graphql-schema-loader.d.ts +2 -1
  181. package/dist/node/graphql/graphql-schema-loader.js +5 -16
  182. package/dist/node/graphql/plugins/gql-plugin-queries.js +1 -1
  183. package/dist/node/graphql/query-files-loader.d.ts +3 -0
  184. package/dist/node/graphql/query-files-loader.js +5 -0
  185. package/dist/node/project/config.d.ts +159 -73
  186. package/dist/node/project/config.js +69 -20
  187. package/dist/node/project/env.d.ts +4 -0
  188. package/dist/node/project/env.js +1 -0
  189. package/dist/node/project/manifest/block-manifest.js +2 -1
  190. package/dist/node/project/manifest/manifest.d.ts +1 -0
  191. package/dist/node/project/manifest/manifest.js +14 -10
  192. package/dist/node/project/manifest/routes-manifest.d.ts +20 -0
  193. package/dist/node/project/manifest/routes-manifest.js +74 -0
  194. package/dist/node/project/manifest/view-manifest.js +1 -1
  195. package/dist/node/project/project.d.ts +11 -1
  196. package/dist/node/project/project.js +61 -7
  197. package/dist/node/project/wp-info.d.ts +1 -0
  198. package/dist/node/project/wp-info.js +13 -1
  199. package/dist/node/storybook/index.d.ts +2 -0
  200. package/dist/node/storybook/index.js +13 -0
  201. package/dist/node/types/block-type.d.ts +25 -20
  202. package/dist/node/types/block-type.js +1 -0
  203. package/dist/node/types/view-type.d.ts +7 -7
  204. package/dist/node/utils/fetch-wp.d.ts +1 -0
  205. package/dist/node/utils/fetch-wp.js +27 -0
  206. package/dist/node/utils/fs-codegen.d.ts +2 -0
  207. package/dist/node/utils/fs-codegen.js +2 -1
  208. package/dist/node/utils/is-deploying.js +1 -1
  209. package/dist/node/utils/report-builder.d.ts +6 -6
  210. package/dist/node/utils/self-signed-cert.d.ts +2 -0
  211. package/dist/node/utils/self-signed-cert.js +10 -2
  212. package/dist/node/utils/stateful-log.js +2 -0
  213. package/dist/node/utils/watch-file-tree.d.ts +17 -3
  214. package/dist/node/utils/watch-file-tree.js +12 -5
  215. package/package.json +21 -12
  216. package/types.app.d.ts +4 -2
  217. package/types.app.internal.d.ts +2 -2
  218. package/types.meta.d.ts +105 -0
  219. package/types.node.d.ts +3 -3
  220. package/dist/app/lib/blocks/ErrorBoundaryFrontend.d.ts +0 -15
  221. package/dist/app/lib/blocks/ErrorBoundaryFrontend.js +0 -35
  222. package/dist/app/lib/hooks/apiConfig.js +0 -4
  223. package/dist/app/lib/hooks/usePageLoad.d.ts +0 -6
  224. package/dist/app/lib/hooks/usePageLoad.js +0 -5
@@ -1,7 +1,12 @@
1
- import { parseURL, stringifyParsedURL, withQuery } from "ufo";
1
+ import { parseURL, stringifyParsedURL, withQuery, withTrailingSlash } from "ufo";
2
+ import { fetchWP } from "../../node/utils/fetch-wp.js";
2
3
  import { filterHeader } from "./utils/headers.js";
3
4
  import { createUrlReplacer } from "./utils/replace-host.js";
4
- const PROXY_RESPONSE_HEADERS = ["content-type", "set-cookie", /^x-/, "cache-control"];
5
+ import { pageCache, queryCache, swr } from "./utils/swr-cache.js";
6
+ import { QueryMonitor } from "../utils/query-monitor.js";
7
+ import chalk from "chalk";
8
+ import { inspect } from "node:util";
9
+ const PROXY_RESPONSE_HEADERS = ["content-type", "set-cookie", /^x-/, "cache-control", /woocommerce/, "location"];
5
10
  const PROXY_REQUEST_HEADERS = [
6
11
  "content-type",
7
12
  "accept",
@@ -11,28 +16,49 @@ const PROXY_REQUEST_HEADERS = [
11
16
  "referer",
12
17
  "user-agent",
13
18
  "authorization",
19
+ "woocommerce-session",
14
20
  ];
21
+ const VARIES_HEADERS = ["Authorization", "woocommerce-session"];
22
+ let runtime;
15
23
  export class ServerContext {
16
24
  dev;
17
25
  origin;
18
- replaceUrls;
26
+ _urlReplacer;
27
+ rpcBases = [];
28
+ config;
29
+ static main;
19
30
  constructor(conf) {
20
31
  this.dev = conf.dev;
21
32
  this.origin = conf.origin;
33
+ this.rpcBases = conf.rpcBases ?? [];
34
+ this.config = conf.config;
22
35
  if (conf.replaceUrls) {
23
- this.replaceUrls = createUrlReplacer(conf.replaceUrls);
36
+ this._urlReplacer = createUrlReplacer(conf.replaceUrls);
24
37
  }
38
+ ServerContext.main = this;
39
+ }
40
+ replaceUrls(text, origin) {
41
+ if (this._urlReplacer) {
42
+ return this._urlReplacer(text, origin);
43
+ }
44
+ return text;
45
+ }
46
+ get runtime() {
47
+ return runtime;
48
+ }
49
+ static setRuntime(rt) {
50
+ runtime = rt;
25
51
  }
26
52
  getOriginUrl(url) {
27
53
  const parsed = parseURL(url);
28
- const parsedOrigin = parseURL(env.origin);
54
+ const parsedOrigin = parseURL(this.origin);
29
55
  parsed.host = parsedOrigin.host;
30
56
  parsed.protocol = parsedOrigin.protocol;
31
57
  return stringifyParsedURL(parsed);
32
58
  }
33
59
  async fetchOrigin(url, opts) {
34
60
  url = this.getOriginUrl(url);
35
- const response = await fetch(url, {
61
+ const response = await fetchWP(url, {
36
62
  ...opts,
37
63
  headers: {
38
64
  "Content-Type": "application/json",
@@ -50,8 +76,8 @@ export class ServerContext {
50
76
  }
51
77
  });
52
78
  let text = await response.text();
53
- if (opts?.replaceUrls && this.replaceUrls) {
54
- text = this.replaceUrls(text);
79
+ if (opts?.replaceUrls) {
80
+ text = this.replaceUrls(text, opts.newOrigin);
55
81
  }
56
82
  return new Response(text, {
57
83
  status: response.status,
@@ -59,22 +85,185 @@ export class ServerContext {
59
85
  headers,
60
86
  });
61
87
  }
88
+ debugLogQueryMonitor(kind, uri, monitor) {
89
+ if (!QueryMonitor.hasLogEntries(monitor))
90
+ return;
91
+ let indentLevel = 2;
92
+ const finalOutput = [];
93
+ let label = "";
94
+ if (kind === "props") {
95
+ label = "While fetching props for " + uri + "";
96
+ }
97
+ else if (kind === "query") {
98
+ label = "While querying " + uri + " via REST API";
99
+ }
100
+ else if (kind === "mutation") {
101
+ label = "While mutating " + uri + " via REST API";
102
+ }
103
+ else if (kind === "app") {
104
+ label = "While fetching app data";
105
+ }
106
+ finalOutput.push(chalk.whiteBright("➜ " + label));
107
+ function printEntry(entry) {
108
+ finalOutput.push(indent(indentLevel, chalk.whiteBright("➜ " + entry.file) + " " + chalk.gray(entry.label ? `(${entry.label})` : "")));
109
+ indentLevel += 2;
110
+ if (entry.log && entry.log.length) {
111
+ for (let item of entry.log) {
112
+ let output = "";
113
+ let prefix = "";
114
+ let message = item.message;
115
+ let stack = [];
116
+ if (item.type === "php_error") {
117
+ prefix = chalk.red("PHP Error:");
118
+ }
119
+ else if (item.type === "php_warning") {
120
+ prefix = chalk.yellow("PHP Warning:");
121
+ }
122
+ else if (item.type === "php_notice") {
123
+ prefix = chalk.yellow("PHP Notice:");
124
+ }
125
+ else if (item.type === "GRAPHQL_DEBUG") {
126
+ prefix = chalk.cyan("debug:");
127
+ }
128
+ else if (item.type === "error" && item.extensions?.category === "user") {
129
+ prefix = chalk.red("UserError:");
130
+ }
131
+ else {
132
+ prefix = chalk.gray((item.type || "Log") + ":");
133
+ }
134
+ if (typeof message !== "string") {
135
+ message = inspect(message, { depth: 4 });
136
+ }
137
+ if (item.debugMessage) {
138
+ message += "\n" + chalk.gray(item.debugMessage);
139
+ }
140
+ output += prefix + " " + message;
141
+ if (item.path) {
142
+ stack.push(chalk.gray(" at path " + JSON.stringify(item.path)));
143
+ }
144
+ if (item.file) {
145
+ stack.push(chalk.gray(`at ${chalk.white(item.file)} line ${item.line || "??"}`));
146
+ }
147
+ if (item.stack?.length) {
148
+ stack.push(item.stack
149
+ .filter((line) => {
150
+ return line !== item.file + ":" + item.line;
151
+ })
152
+ .slice(0, 4)
153
+ .map((line) => " " + chalk.gray(line))
154
+ .join("\n"));
155
+ }
156
+ if (stack.length > 0) {
157
+ output += indent(2, "\n" + stack.join("\n"));
158
+ }
159
+ finalOutput.push(indent(indentLevel, "➜ " + output));
160
+ }
161
+ }
162
+ for (let child of entry.children ?? []) {
163
+ printEntry(child);
164
+ }
165
+ indentLevel -= 2;
166
+ }
167
+ for (let entry of monitor) {
168
+ printEntry(entry);
169
+ }
170
+ console.debug(finalOutput.join("\n"));
171
+ }
62
172
  async fetchRouteData(req) {
63
- const fetchUrl = withQuery(req.pathname, {
64
- ...req.query,
65
- _props: req.withAppData ? "all" : "1",
66
- _ssr: "1",
67
- _debug: this.dev ? "1" : undefined,
173
+ /**
174
+ * Calculcate the cache key.
175
+ * For route data, the cache key is the pathname.
176
+ * TODO: Potential support for Vercel draft mode.
177
+ */
178
+ let cacheKey = "fetchRouteData:" + req.pathname;
179
+ const result = await swr({
180
+ key: cacheKey,
181
+ cache: pageCache,
182
+ forceFresh: this.dev,
183
+ getFreshValue: async (ctx) => {
184
+ ctx.metadata.createdTime;
185
+ const fetchUrl = withQuery(withTrailingSlash(req.pathname), {
186
+ _props: "1",
187
+ _ssr: "1",
188
+ _debug: this.dev ? "1" : undefined,
189
+ });
190
+ const result = await this.fetchOrigin(fetchUrl, {
191
+ cache: "no-cache",
192
+ replaceUrls: true,
193
+ newOrigin: req.newOrigin,
194
+ headers: {
195
+ "Content-Type": "application/json",
196
+ Accept: "application/json",
197
+ },
198
+ redirect: "manual",
199
+ });
200
+ const preferredCacheDuration = parseInt(result.headers.get("x-ed-cache-duration") ?? "");
201
+ if (isFinite(preferredCacheDuration)) {
202
+ ctx.metadata.ttl = preferredCacheDuration * 1000;
203
+ }
204
+ let resultStatus = result.status;
205
+ let resultHeaders = new Headers(result.headers);
206
+ let resultData = {};
207
+ // Special case for redirects
208
+ if (result.headers.get("location")) {
209
+ let location = result.headers.get("location");
210
+ let status = result.status;
211
+ if (this.replaceUrls) {
212
+ location = this.replaceUrls(location);
213
+ }
214
+ resultHeaders.delete("location");
215
+ return {
216
+ status: 200,
217
+ headers: resultHeaders,
218
+ data: JSON.stringify({ redirect: location, status: status }),
219
+ };
220
+ }
221
+ resultData = await result.json();
222
+ if (resultData && typeof resultData === "object") {
223
+ resultData.__generated = new Date().toISOString();
224
+ }
225
+ if (resultData.queryMonitor) {
226
+ this.debugLogQueryMonitor("props", req.pathname, resultData.queryMonitor);
227
+ }
228
+ return {
229
+ status: resultStatus,
230
+ headers: resultHeaders,
231
+ data: JSON.stringify(resultData),
232
+ };
233
+ },
68
234
  });
69
- return this.fetchOrigin(fetchUrl, {
70
- cache: "no-cache",
71
- replaceUrls: true,
72
- headers: {
73
- "Content-Type": "application/json",
74
- Accept: "application/json",
75
- ...this.extractRequestHeaders(req.headers),
235
+ return new Response(result.data, {
236
+ status: result.status,
237
+ headers: result.headers,
238
+ });
239
+ }
240
+ async fetchAppData() {
241
+ const data = await swr({
242
+ key: "fetchAppData",
243
+ cache: pageCache,
244
+ getFreshValue: async (ctx) => {
245
+ const response = await this.fetchOrigin("/_appdata", {
246
+ cache: "no-cache",
247
+ replaceUrls: true,
248
+ headers: {
249
+ "Content-Type": "application/json",
250
+ Accept: "application/json",
251
+ },
252
+ });
253
+ const result = await response.json();
254
+ const preferredCacheDuration = parseInt(response.headers.get("x-ed-cache-duration") ?? "");
255
+ if (isFinite(preferredCacheDuration)) {
256
+ ctx.metadata.ttl = preferredCacheDuration * 1000;
257
+ }
258
+ if (result.queryMonitor) {
259
+ this.debugLogQueryMonitor("app", "", result.queryMonitor);
260
+ }
261
+ result.__generated = new Date().toISOString();
262
+ return result;
76
263
  },
264
+ forceFresh: this.dev,
77
265
  });
266
+ return data;
78
267
  }
79
268
  extractRequestHeaders(req) {
80
269
  const headers = {};
@@ -89,14 +278,52 @@ export class ServerContext {
89
278
  }
90
279
  async fetchNamedQuery(req) {
91
280
  const url = `/wp-json/ed/v1/query/${req.name}?params=${encodeURIComponent(JSON.stringify(req.params))}`;
92
- return this.fetchOrigin(url, {
93
- cache: "no-cache",
94
- replaceUrls: true,
95
- headers: {
96
- ...this.extractRequestHeaders(req.headers),
97
- "Content-Type": "application/json",
98
- Accept: "application/json",
99
- },
281
+ const key = `fetchNamedQuery:${req.name}:${JSON.stringify(req.params)}`;
282
+ const fetch = async () => {
283
+ const result = await this.fetchOrigin(url, {
284
+ cache: "no-cache",
285
+ replaceUrls: true,
286
+ headers: {
287
+ ...this.extractRequestHeaders(req.headers),
288
+ "Content-Type": "application/json",
289
+ Accept: "application/json",
290
+ },
291
+ });
292
+ const preferredCacheDuration = parseInt(result.headers.get("x-ed-cache-duration") ?? "");
293
+ let resultStatus = result.status;
294
+ let resultHeaders = result.headers;
295
+ let resultData = await result.json();
296
+ if (resultData && typeof resultData === "object") {
297
+ resultData.__generated = new Date().toISOString();
298
+ }
299
+ if (resultData.queryMonitor) {
300
+ this.debugLogQueryMonitor("query", req.name, resultData.queryMonitor);
301
+ }
302
+ return {
303
+ status: resultStatus,
304
+ headers: resultHeaders,
305
+ data: JSON.stringify(resultData),
306
+ cacheTime: isFinite(preferredCacheDuration) ? preferredCacheDuration * 1000 : undefined,
307
+ };
308
+ };
309
+ // If headers like 'Authorization' are set, then we shouldn't cache.
310
+ const hasVariedHeaders = VARIES_HEADERS.some((key) => !!req.headers[key]);
311
+ // Get the result, either from cache or by fetching.
312
+ const result = hasVariedHeaders
313
+ ? await fetch()
314
+ : await swr({
315
+ key,
316
+ cache: queryCache,
317
+ forceFresh: this.dev,
318
+ getFreshValue: async (ctx) => {
319
+ const result = await fetch();
320
+ ctx.metadata.ttl = result.cacheTime;
321
+ return result;
322
+ },
323
+ });
324
+ return new Response(result.data, {
325
+ status: result.status,
326
+ headers: result.headers,
100
327
  });
101
328
  }
102
329
  async fetchMutation(req) {
@@ -113,4 +340,54 @@ export class ServerContext {
113
340
  body: JSON.stringify(req.body),
114
341
  });
115
342
  }
343
+ get allowedCorsOrigins() {
344
+ let result = [];
345
+ if (this.config.serverless.endpoints) {
346
+ result.push(...Object.keys(this.config.serverless.endpoints));
347
+ result.push(...Object.values(this.config.serverless.endpoints));
348
+ result = result.filter((origin) => origin !== "*");
349
+ }
350
+ if (this.config.serverless.cors?.origins) {
351
+ result.push(...this.config.serverless.cors.origins);
352
+ }
353
+ return result;
354
+ }
355
+ getCorsOrigin(originHeader) {
356
+ const parsed = parseURL(originHeader);
357
+ if (this.allowedCorsOrigins.includes(parsed.host)) {
358
+ return originHeader;
359
+ }
360
+ if (this.allowedCorsOrigins.includes("*")) {
361
+ return "*";
362
+ }
363
+ }
364
+ getCorsHeaders(origin) {
365
+ if (!origin)
366
+ return {};
367
+ const allowedOrigin = this.getCorsOrigin(origin);
368
+ if (allowedOrigin) {
369
+ return {
370
+ "Access-Control-Allow-Methods": "OPTIONS,GET,HEAD,PUT,PATCH,POST,DELETE",
371
+ "Access-Control-Allow-Origin": allowedOrigin,
372
+ "Access-Control-Allow-Credentials": "true",
373
+ "Access-Control-Allow-Headers": "*",
374
+ "Access-Control-Expose-Headers": "*",
375
+ };
376
+ }
377
+ return {};
378
+ }
379
+ }
380
+ function indent(count, str) {
381
+ const indent = " ".repeat(count);
382
+ return str
383
+ .split("\n")
384
+ .map((line) => indent + line)
385
+ .join("\n");
386
+ }
387
+ function indentAllButFirst(count, str) {
388
+ const indent = " ".repeat(count);
389
+ return str
390
+ .split("\n")
391
+ .map((line, i) => (i === 0 ? line : indent + line))
392
+ .join("\n");
116
393
  }
@@ -7,4 +7,4 @@ export type UrlReplacerConf = {
7
7
  replace: boolean;
8
8
  }[];
9
9
  };
10
- export declare function createUrlReplacer(conf: UrlReplacerConf): (text: string) => string;
10
+ export declare function createUrlReplacer(conf: UrlReplacerConf): (text: string, newOrigin?: string) => string;
@@ -2,7 +2,7 @@ export function createUrlReplacer(conf) {
2
2
  // Create a regular expression to match the URL
3
3
  // Note that that the regex doesn't necessarily match the entire URL
4
4
  const lookup = new RegExp(conf.from.replace(/https?[:\\\/]+/, "https?[:\\/\\\\]+") + "([a-z0-9\\-_/\\\\]+)", "ig");
5
- return (text) => {
5
+ return (text, newOrigin) => {
6
6
  return text.replace(lookup, (url) => {
7
7
  // Strip out the origin, to create a relative path
8
8
  const path = url.replace(/https?:[\/\\]+[^\/\\]+/, "");
@@ -18,10 +18,18 @@ export function createUrlReplacer(conf) {
18
18
  return url;
19
19
  }
20
20
  else {
21
- return path;
21
+ break;
22
22
  }
23
23
  }
24
24
  }
25
+ if (newOrigin) {
26
+ if (isEscaped) {
27
+ return newOrigin.replace(/\//g, "\\/") + path;
28
+ }
29
+ else {
30
+ return newOrigin + path;
31
+ }
32
+ }
25
33
  return path;
26
34
  // if (path.startsWith("/wp-content/uploads/")) {
27
35
  // if (!conf.ignoreUploads) {
@@ -0,0 +1,4 @@
1
+ import { Cache, CachifiedOptions } from "@epic-web/cachified";
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>;
@@ -0,0 +1,31 @@
1
+ import { LRUCache } from "lru-cache";
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();
24
+ export function swr(args) {
25
+ return cachified({
26
+ ttl: args.ttl ?? 10_000,
27
+ swr: 3600_000,
28
+ fallbackToCache: 15_000,
29
+ ...args,
30
+ });
31
+ }
@@ -0,0 +1,2 @@
1
+ import { PropsWithChildren } from "react";
2
+ export declare function APIProvider(props: PropsWithChildren<{}>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,5 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { QueryProvider } from "./query-client.js";
3
+ export function APIProvider(props) {
4
+ return _jsx(QueryProvider, { children: props.children });
5
+ }
@@ -0,0 +1,19 @@
1
+ import { Component, ErrorInfo, ReactNode } from "react";
2
+ import { BlocksContext, ContentBlock } from "../lib/blocks/ContentBlocks.js";
3
+ interface Props {
4
+ block: ContentBlock;
5
+ blockContext: BlocksContext;
6
+ children: ReactNode;
7
+ }
8
+ interface State {
9
+ hasError: boolean;
10
+ error?: Error;
11
+ errorInfo?: ErrorInfo;
12
+ }
13
+ export declare class BlockErrorBoundary extends Component<Props, State> {
14
+ state: State;
15
+ static getDerivedStateFromError(error: Error): State;
16
+ componentDidCatch(error: Error, errorInfo: ErrorInfo): void;
17
+ render(): ReactNode;
18
+ }
19
+ export {};
@@ -0,0 +1,38 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Component } from "react";
3
+ import { errorHandling } from "../lib/runtime/errorHandling.js";
4
+ import { ErrorMessage } from "./ErrorMessage.js";
5
+ export class BlockErrorBoundary extends Component {
6
+ state = {
7
+ hasError: false,
8
+ };
9
+ static getDerivedStateFromError(error) {
10
+ // Update state so the next render will show the fallback UI.
11
+ return { hasError: true, error: error };
12
+ }
13
+ componentDidCatch(error, errorInfo) {
14
+ if (errorHandling.onReactError) {
15
+ errorHandling.onReactError({
16
+ type: "block",
17
+ block: this.props.block,
18
+ error,
19
+ componentStack: errorInfo.componentStack,
20
+ });
21
+ }
22
+ }
23
+ render() {
24
+ if (this.state.hasError) {
25
+ const err = this.state.error;
26
+ let display = env.dev ? _jsx(ErrorMessage, { error: err }) : null;
27
+ if (errorHandling.renderFallback) {
28
+ display = errorHandling.renderFallback({
29
+ type: "block",
30
+ block: this.props.block,
31
+ error: err,
32
+ }, display);
33
+ }
34
+ return display;
35
+ }
36
+ return this.props.children;
37
+ }
38
+ }
@@ -0,0 +1,5 @@
1
+ type Props = {
2
+ error: Error;
3
+ };
4
+ export declare function ErrorMessage({ error }: Props): import("react/jsx-runtime").JSX.Element;
5
+ export {};
@@ -0,0 +1,14 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export function ErrorMessage({ error }) {
3
+ return (_jsx("pre", { style: {
4
+ backgroundColor: "#060309",
5
+ fontSize: "12px",
6
+ color: "#d92a0b",
7
+ outline: "1px solid #d92a0b",
8
+ outlineOffset: "-4px",
9
+ borderRadius: "6px",
10
+ padding: "12px 16px",
11
+ overflowX: "auto",
12
+ fontFamily: '"Roboto Mono", Menlo, monospace',
13
+ }, children: error.stack ?? String(error) }));
14
+ }
@@ -0,0 +1,18 @@
1
+ import { Component, ErrorInfo, ReactNode } from "react";
2
+ import { RouteState } from "../lib/routing/types.js";
3
+ interface Props {
4
+ route: RouteState;
5
+ children: ReactNode;
6
+ }
7
+ interface State {
8
+ hasError: boolean;
9
+ error?: Error;
10
+ errorInfo?: ErrorInfo;
11
+ }
12
+ export declare class RouteErrorBoundary extends Component<Props, State> {
13
+ state: State;
14
+ static getDerivedStateFromError(error: Error): State;
15
+ componentDidCatch(error: Error, errorInfo: ErrorInfo): void;
16
+ render(): ReactNode;
17
+ }
18
+ export {};
@@ -0,0 +1,38 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Component } from "react";
3
+ import { errorHandling } from "../lib/runtime/errorHandling.js";
4
+ import { ErrorMessage } from "./ErrorMessage.js";
5
+ export class RouteErrorBoundary extends Component {
6
+ state = {
7
+ hasError: false,
8
+ };
9
+ static getDerivedStateFromError(error) {
10
+ // Update state so the next render will show the fallback UI.
11
+ return { hasError: true, error: error };
12
+ }
13
+ componentDidCatch(error, errorInfo) {
14
+ if (errorHandling.onReactError) {
15
+ errorHandling.onReactError({
16
+ type: "route",
17
+ route: this.props.route,
18
+ error,
19
+ componentStack: errorInfo.componentStack,
20
+ });
21
+ }
22
+ }
23
+ render() {
24
+ if (this.state.hasError) {
25
+ const err = this.state.error;
26
+ let display = env.dev ? _jsx(ErrorMessage, { error: err }) : null;
27
+ if (errorHandling.renderFallback) {
28
+ display = errorHandling.renderFallback({
29
+ type: "route",
30
+ route: this.props.route,
31
+ error: err,
32
+ }, display);
33
+ }
34
+ return display;
35
+ }
36
+ return this.props.children;
37
+ }
38
+ }
@@ -0,0 +1,2 @@
1
+ export declare const AssetCaptureContext: import("react").Context<Set<string>>;
2
+ export declare const useAssetCapture: () => Set<string>;
@@ -0,0 +1,5 @@
1
+ import { createContext, useContext } from "react";
2
+ export const AssetCaptureContext = createContext(new Set());
3
+ export const useAssetCapture = () => {
4
+ return useContext(AssetCaptureContext);
5
+ };
@@ -0,0 +1,13 @@
1
+ export type HydrationErrorSnapshot = {
2
+ html: string;
3
+ error: Error;
4
+ info: any;
5
+ };
6
+ export declare const hydrationDebug: {
7
+ initialHTML: string;
8
+ events: HydrationErrorSnapshot[];
9
+ add(item: {
10
+ error: Error;
11
+ info: any;
12
+ }): void;
13
+ };
@@ -0,0 +1,11 @@
1
+ import { proxy } from "valtio";
2
+ export const hydrationDebug = proxy({
3
+ initialHTML: "",
4
+ events: [],
5
+ add(item) {
6
+ hydrationDebug.events.push({
7
+ html: document.querySelector("#_app")?.innerHTML ?? "",
8
+ ...item,
9
+ });
10
+ },
11
+ });
@@ -1,2 +1,4 @@
1
1
  import { QueryClient } from "@tanstack/react-query";
2
+ import { PropsWithChildren } from "react";
2
3
  export declare function getQueryClient(): QueryClient;
4
+ export declare function QueryProvider(props: PropsWithChildren<{}>): import("react/jsx-runtime").JSX.Element;