@cedarjs/prerender 0.9.1-next.21 → 0.10.0-rc.24

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 (64) hide show
  1. package/dist/babelPlugins/babel-plugin-redwood-cell.d.ts +5 -0
  2. package/dist/babelPlugins/babel-plugin-redwood-cell.d.ts.map +1 -0
  3. package/dist/babelPlugins/babel-plugin-redwood-cell.js +97 -0
  4. package/dist/babelPlugins/babel-plugin-redwood-prerender-media-imports.d.ts +5 -0
  5. package/dist/babelPlugins/babel-plugin-redwood-prerender-media-imports.d.ts.map +1 -0
  6. package/dist/babelPlugins/babel-plugin-redwood-prerender-media-imports.js +76 -0
  7. package/dist/babelPlugins/utils.d.ts +2 -0
  8. package/dist/babelPlugins/utils.d.ts.map +1 -0
  9. package/dist/babelPlugins/utils.js +15 -0
  10. package/dist/cjs/babelPlugins/babel-plugin-redwood-cell.d.ts +5 -0
  11. package/dist/cjs/babelPlugins/babel-plugin-redwood-cell.d.ts.map +1 -0
  12. package/dist/cjs/babelPlugins/babel-plugin-redwood-cell.js +121 -0
  13. package/dist/cjs/babelPlugins/babel-plugin-redwood-prerender-media-imports.d.ts +5 -0
  14. package/dist/cjs/babelPlugins/babel-plugin-redwood-prerender-media-imports.d.ts.map +1 -0
  15. package/dist/cjs/babelPlugins/babel-plugin-redwood-prerender-media-imports.js +100 -0
  16. package/dist/cjs/babelPlugins/utils.d.ts +2 -0
  17. package/dist/cjs/babelPlugins/utils.d.ts.map +1 -0
  18. package/dist/cjs/babelPlugins/utils.js +49 -0
  19. package/dist/cjs/browserUtils/index.d.ts +8 -0
  20. package/dist/cjs/browserUtils/index.d.ts.map +1 -0
  21. package/dist/cjs/browserUtils/index.js +47 -0
  22. package/dist/cjs/build-and-import/buildAndImport.js +173 -0
  23. package/dist/cjs/build-and-import/load-tsconfig.d.js +1 -0
  24. package/dist/cjs/build-and-import/rollupPlugins/rollup-plugin-cedarjs-cell.js +183 -0
  25. package/dist/cjs/build-and-import/rollupPlugins/rollup-plugin-cedarjs-directory-named-imports.js +83 -0
  26. package/dist/cjs/build-and-import/rollupPlugins/rollup-plugin-cedarjs-external.js +125 -0
  27. package/dist/cjs/build-and-import/rollupPlugins/rollup-plugin-cedarjs-ignore-html-and-css-imports.js +48 -0
  28. package/dist/cjs/build-and-import/rollupPlugins/rollup-plugin-cedarjs-inject-file-globals.js +66 -0
  29. package/dist/cjs/build-and-import/rollupPlugins/rollup-plugin-cedarjs-prerender-media-imports.js +118 -0
  30. package/dist/cjs/build-and-import/rollupPlugins/rollup-plugin-cedarjs-routes-auto-loader.js +146 -0
  31. package/dist/cjs/build-and-import/rollupPlugins/utils.js +78 -0
  32. package/dist/cjs/build-and-import/utils.js +68 -0
  33. package/dist/cjs/detection/detection.d.ts +11 -0
  34. package/dist/cjs/detection/detection.d.ts.map +1 -0
  35. package/dist/cjs/detection/detection.js +48 -0
  36. package/dist/cjs/errors.d.ts +19 -0
  37. package/dist/cjs/errors.d.ts.map +1 -0
  38. package/dist/cjs/errors.js +52 -0
  39. package/dist/cjs/graphql/graphql.d.ts +19 -0
  40. package/dist/cjs/graphql/graphql.d.ts.map +1 -0
  41. package/dist/cjs/graphql/graphql.js +152 -0
  42. package/dist/cjs/graphql/node-runner.js +109 -0
  43. package/dist/cjs/graphql/vite-plugin-cedar-auto-import.js +107 -0
  44. package/dist/cjs/graphql/vite-plugin-cedar-import-dir.js +118 -0
  45. package/dist/{index.d.ts.map → cjs/index.d.ts.map} +1 -1
  46. package/dist/cjs/index.js +22 -0
  47. package/dist/cjs/internal.d.ts +28 -0
  48. package/dist/cjs/internal.d.ts.map +1 -0
  49. package/dist/cjs/internal.js +135 -0
  50. package/dist/cjs/package.json +1 -0
  51. package/dist/cjs/runPrerender.d.ts.map +1 -0
  52. package/dist/cjs/runPrerender.js +307 -0
  53. package/dist/cjs/runPrerenderEsm.js +304 -0
  54. package/dist/graphql/graphql.d.ts +3 -3
  55. package/dist/graphql/graphql.d.ts.map +1 -1
  56. package/dist/graphql/graphql.js +4 -4
  57. package/dist/runPrerenderEsm.d.ts +9 -0
  58. package/dist/runPrerenderEsm.d.ts.map +1 -0
  59. package/dist/{runPrerender.js → runPrerenderEsm.js} +5 -5
  60. package/package.json +44 -17
  61. package/dist/index.js +0 -1
  62. package/dist/runPrerender.d.ts.map +0 -1
  63. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  64. /package/dist/{runPrerender.d.ts → cjs/runPrerender.d.ts} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runPrerender.d.ts","sourceRoot":"","sources":["../../src/runPrerender.tsx"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAmO7C,UAAU,eAAe;IACvB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IACrC,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,eAAO,MAAM,YAAY,gCAGtB,eAAe,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAkIzC,CAAA;AAGD,eAAO,MAAM,wBAAwB,mBACnB,MAAM,WACb,MAAM,SAchB,CAAA"}
@@ -0,0 +1,307 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var runPrerender_exports = {};
30
+ __export(runPrerender_exports, {
31
+ runPrerender: () => runPrerender,
32
+ writePrerenderedHtmlFile: () => writePrerenderedHtmlFile
33
+ });
34
+ module.exports = __toCommonJS(runPrerender_exports);
35
+ var import_node_fs = __toESM(require("node:fs"), 1);
36
+ var import_node_path = __toESM(require("node:path"), 1);
37
+ var import_react = __toESM(require("react"), 1);
38
+ var apolloClient = __toESM(require("@apollo/client"), 1);
39
+ var import_cheerio = require("cheerio");
40
+ var import_server = __toESM(require("react-dom/server"), 1);
41
+ var import_babel_config = require("@cedarjs/babel-config");
42
+ var import_project_config = require("@cedarjs/project-config");
43
+ var import_router = require("@cedarjs/router");
44
+ var import_util = require("@cedarjs/router/dist/util");
45
+ var import_babel_plugin_redwood_cell = require("./babelPlugins/babel-plugin-redwood-cell.js");
46
+ var import_babel_plugin_redwood_prerender_media_imports = require("./babelPlugins/babel-plugin-redwood-prerender-media-imports.js");
47
+ var import_detection = require("./detection/detection.js");
48
+ var import_errors = require("./errors.js");
49
+ var import_graphql = require("./graphql/graphql.js");
50
+ var import_internal = require("./internal.js");
51
+ const { ApolloClient, InMemoryCache } = apolloClient.default || apolloClient;
52
+ const prerenderApolloClient = new ApolloClient({ cache: new InMemoryCache() });
53
+ async function recursivelyRender(App, Routes, renderPath, fileImporter, gqlHandler, queryCache) {
54
+ const { CellCacheContextProvider, getOperationName } = require("@cedarjs/web");
55
+ let shouldShowGraphqlHandlerNotFoundWarn = false;
56
+ await Promise.all(
57
+ Object.entries(queryCache).map(async ([cacheKey, value]) => {
58
+ if (value.hasProcessed) {
59
+ return Promise.resolve("");
60
+ }
61
+ try {
62
+ const resultString = await (0, import_graphql.executeQuery)(
63
+ fileImporter,
64
+ gqlHandler,
65
+ value.query,
66
+ value.variables
67
+ );
68
+ let result;
69
+ try {
70
+ result = JSON.parse(resultString);
71
+ } catch (e) {
72
+ if (e instanceof SyntaxError) {
73
+ throw new import_errors.JSONParseError({
74
+ query: value.query,
75
+ variables: value.variables,
76
+ result: resultString
77
+ });
78
+ }
79
+ }
80
+ if (result.errors) {
81
+ const message = result.errors[0].message ?? JSON.stringify(result.errors, null, 4);
82
+ if (result.errors[0]?.extensions?.code === "UNAUTHENTICATED") {
83
+ const queryName = getOperationName(value.query);
84
+ console.error(
85
+ `
86
+
87
+ \u{1F6D1} Cannot prerender the query ${queryName} as it requires auth.
88
+ `
89
+ );
90
+ }
91
+ throw new import_errors.PrerenderGqlError(message);
92
+ }
93
+ queryCache[cacheKey] = {
94
+ ...value,
95
+ data: result.data,
96
+ hasProcessed: true
97
+ };
98
+ return result;
99
+ } catch (e) {
100
+ if (e instanceof import_errors.GqlHandlerImportError) {
101
+ shouldShowGraphqlHandlerNotFoundWarn = true;
102
+ queryCache[cacheKey] = {
103
+ ...value,
104
+ // tried to fetch, but failed
105
+ renderLoading: true,
106
+ hasProcessed: true
107
+ };
108
+ return;
109
+ } else {
110
+ throw e;
111
+ }
112
+ }
113
+ })
114
+ );
115
+ const prerenderUrl = process.env.RWJS_PRERENDER_ORIGIN || "http://localhost" + renderPath;
116
+ const componentAsHtml = import_server.default.renderToString(
117
+ /* @__PURE__ */ import_react.default.createElement(import_router.LocationProvider, { location: new URL(prerenderUrl) }, /* @__PURE__ */ import_react.default.createElement(CellCacheContextProvider, { queryCache }, /* @__PURE__ */ import_react.default.createElement(App, null, /* @__PURE__ */ import_react.default.createElement(Routes, null))))
118
+ );
119
+ if (Object.values(queryCache).some((value) => !value.hasProcessed)) {
120
+ return recursivelyRender(
121
+ App,
122
+ Routes,
123
+ renderPath,
124
+ fileImporter,
125
+ gqlHandler,
126
+ queryCache
127
+ );
128
+ } else {
129
+ if (shouldShowGraphqlHandlerNotFoundWarn) {
130
+ console.warn(
131
+ '\n \u26A0\uFE0F Could not load your GraphQL handler. \n Your Cells have been prerendered in the "Loading" state. \n'
132
+ );
133
+ }
134
+ return Promise.resolve(componentAsHtml);
135
+ }
136
+ }
137
+ function insertChunkLoadingScript(indexHtmlTree, renderPath) {
138
+ const prerenderRoutes = (0, import_detection.detectPrerenderRoutes)();
139
+ const route = prerenderRoutes.find((route2) => {
140
+ if (!route2.routePath) {
141
+ return false;
142
+ }
143
+ return (0, import_util.matchPath)(route2.routePath, renderPath).match;
144
+ });
145
+ if (!route) {
146
+ throw new Error("Could not find a Route matching " + renderPath);
147
+ }
148
+ if (!route.pageIdentifier) {
149
+ throw new Error(`Route for ${renderPath} has no pageIdentifier`);
150
+ }
151
+ const buildManifest = JSON.parse(
152
+ import_node_fs.default.readFileSync(
153
+ import_node_path.default.join((0, import_project_config.getPaths)().web.dist, "client-build-manifest.json"),
154
+ "utf-8"
155
+ )
156
+ );
157
+ const chunkPaths = [];
158
+ if (route?.filePath) {
159
+ const pagesIndex = route.filePath.indexOf(import_node_path.default.join("web", "src", "pages")) + 8;
160
+ const pagePath = (0, import_project_config.ensurePosixPath)(route.filePath.slice(pagesIndex));
161
+ const pageChunkPath = buildManifest[pagePath]?.file;
162
+ if (pageChunkPath) {
163
+ chunkPaths.push("/" + pageChunkPath);
164
+ }
165
+ }
166
+ if (chunkPaths.length === 0) {
167
+ return;
168
+ }
169
+ chunkPaths.forEach((chunkPath) => {
170
+ indexHtmlTree("head").prepend(
171
+ `<script defer="defer" src="${chunkPath}" type="module"></script>`
172
+ );
173
+ });
174
+ chunkPaths.forEach((chunkPath) => {
175
+ const fullChunkPath = import_node_path.default.join((0, import_project_config.getPaths)().web.dist, chunkPath);
176
+ const jsChunk = import_node_fs.default.readFileSync(fullChunkPath, "utf-8");
177
+ const matches = jsChunk.match(/export\s*\{\s*\w+ as default\s*\}/g) || [];
178
+ const lastIndex = jsChunk.lastIndexOf(matches[matches.length - 1]);
179
+ const varNameMatch = jsChunk.slice(lastIndex).match(/export\s*\{\s*(\w+) as default\s*\}/);
180
+ import_node_fs.default.writeFileSync(
181
+ fullChunkPath,
182
+ jsChunk + `globalThis.__REDWOOD__PRERENDER_PAGES = globalThis.__REDWOOD__PRERENDER_PAGES || {};
183
+ globalThis.__REDWOOD__PRERENDER_PAGES.${route?.pageIdentifier}=${varNameMatch?.[1]};
184
+ `
185
+ );
186
+ });
187
+ }
188
+ const runPrerender = async ({
189
+ queryCache,
190
+ renderPath
191
+ }) => {
192
+ (0, import_internal.registerShims)(renderPath);
193
+ (0, import_babel_config.registerApiSideBabelHook)({
194
+ plugins: [
195
+ [
196
+ "babel-plugin-module-resolver",
197
+ {
198
+ alias: {
199
+ api: (0, import_project_config.getPaths)().api.base,
200
+ web: (0, import_project_config.getPaths)().web.base
201
+ },
202
+ loglevel: "silent"
203
+ // to silence the unnecessary warnings
204
+ }
205
+ ]
206
+ ],
207
+ overrides: [
208
+ {
209
+ test: ["./api/"],
210
+ plugins: [
211
+ [
212
+ "babel-plugin-module-resolver",
213
+ {
214
+ alias: {
215
+ src: (0, import_project_config.getPaths)().api.src
216
+ },
217
+ loglevel: "silent"
218
+ },
219
+ "exec-api-src-module-resolver"
220
+ ]
221
+ ]
222
+ }
223
+ ]
224
+ });
225
+ const gqlHandler = await (0, import_graphql.getGqlHandler)(require);
226
+ (0, import_babel_config.registerWebSideBabelHook)({
227
+ overrides: [
228
+ {
229
+ plugins: [
230
+ ["ignore-html-and-css-imports"],
231
+ // webpack/postcss handles CSS imports
232
+ [import_babel_plugin_redwood_prerender_media_imports.babelPluginRedwoodPrerenderMediaImports]
233
+ ]
234
+ },
235
+ {
236
+ test: /.+Cell.(js|tsx|jsx)$/,
237
+ plugins: [import_babel_plugin_redwood_cell.babelPluginRedwoodCell]
238
+ }
239
+ ],
240
+ options: {
241
+ forPrerender: true
242
+ }
243
+ });
244
+ const indexContent = import_node_fs.default.readFileSync((0, import_internal.getRootHtmlPath)()).toString();
245
+ const { default: App } = require((0, import_project_config.getPaths)().web.app);
246
+ const { default: Routes } = require((0, import_project_config.getPaths)().web.routes);
247
+ const componentAsHtml = await recursivelyRender(
248
+ App,
249
+ Routes,
250
+ renderPath,
251
+ require,
252
+ gqlHandler,
253
+ queryCache
254
+ );
255
+ const { helmet } = globalThis.__REDWOOD__HELMET_CONTEXT;
256
+ Object.keys(queryCache).forEach((queryKey) => {
257
+ const { query, variables, data } = queryCache[queryKey];
258
+ prerenderApolloClient.writeQuery({
259
+ query,
260
+ variables,
261
+ data
262
+ });
263
+ });
264
+ const indexHtmlTree = (0, import_cheerio.load)(indexContent);
265
+ if (helmet) {
266
+ const helmetElements = `
267
+ ${helmet?.link.toString()}
268
+ ${helmet?.meta.toString()}
269
+ ${helmet?.script.toString()}
270
+ ${helmet?.noscript.toString()}
271
+ `;
272
+ indexHtmlTree("head").prepend(helmetElements);
273
+ const titleHtmlTag = helmet.title.toString();
274
+ if ((0, import_cheerio.load)(titleHtmlTag)("title").text() !== "") {
275
+ if (indexHtmlTree("title").text().length === 0) {
276
+ indexHtmlTree("head").prepend(titleHtmlTag);
277
+ } else {
278
+ indexHtmlTree("title").replaceWith(titleHtmlTag);
279
+ }
280
+ }
281
+ }
282
+ indexHtmlTree("head").append(
283
+ `<script> globalThis.__REDWOOD__APOLLO_STATE = ${JSON.stringify(
284
+ prerenderApolloClient.extract()
285
+ )}</script>`
286
+ );
287
+ prerenderApolloClient.resetStore();
288
+ insertChunkLoadingScript(indexHtmlTree, renderPath);
289
+ indexHtmlTree("#redwood-app").append(componentAsHtml);
290
+ const renderOutput = indexHtmlTree.html();
291
+ return renderOutput;
292
+ };
293
+ const writePrerenderedHtmlFile = (outputHtmlPath, content) => {
294
+ const outputHtmlAbsPath = import_node_path.default.join((0, import_project_config.getPaths)().base, outputHtmlPath);
295
+ if (outputHtmlPath === "web/dist/index.html") {
296
+ const html200Path = import_node_path.default.join((0, import_project_config.getPaths)().web.dist, "200.html");
297
+ if (!import_node_fs.default.existsSync(html200Path)) {
298
+ import_node_fs.default.copyFileSync(outputHtmlAbsPath, html200Path);
299
+ }
300
+ }
301
+ (0, import_internal.writeToDist)(outputHtmlAbsPath, content);
302
+ };
303
+ // Annotate the CommonJS export names for ESM import in node:
304
+ 0 && (module.exports = {
305
+ runPrerender,
306
+ writePrerenderedHtmlFile
307
+ });
@@ -0,0 +1,304 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var runPrerenderEsm_exports = {};
30
+ __export(runPrerenderEsm_exports, {
31
+ runPrerender: () => runPrerender,
32
+ writePrerenderedHtmlFile: () => writePrerenderedHtmlFile
33
+ });
34
+ module.exports = __toCommonJS(runPrerenderEsm_exports);
35
+ var import_node_fs = __toESM(require("node:fs"), 1);
36
+ var import_node_path = __toESM(require("node:path"), 1);
37
+ var import_react = __toESM(require("react"), 1);
38
+ var apolloClient = __toESM(require("@apollo/client"), 1);
39
+ var import_cheerio = require("cheerio");
40
+ var import_server = __toESM(require("react-dom/server"), 1);
41
+ var import_project_config = require("@cedarjs/project-config");
42
+ var import_util = require("@cedarjs/router/dist/util");
43
+ var import_buildAndImport = require("./build-and-import/buildAndImport.js");
44
+ var import_detection = require("./detection/detection.js");
45
+ var import_errors = require("./errors.js");
46
+ var import_graphql = require("./graphql/graphql.js");
47
+ var import_node_runner = require("./graphql/node-runner.js");
48
+ var import_internal = require("./internal.js");
49
+ const { ApolloClient, InMemoryCache } = apolloClient.default;
50
+ const prerenderApolloClient = new ApolloClient({ cache: new InMemoryCache() });
51
+ async function recursivelyRender(App, Routes, CellCacheContextProvider, LocationProvider, renderPath, fileImporter, gqlHandler, queryCache) {
52
+ const { getOperationName } = await import("@cedarjs/web");
53
+ let shouldShowGraphqlHandlerNotFoundWarn = false;
54
+ await Promise.all(
55
+ Object.entries(queryCache).map(async ([cacheKey, value]) => {
56
+ if (value.hasProcessed) {
57
+ return Promise.resolve("");
58
+ }
59
+ try {
60
+ const resultString = await (0, import_graphql.executeQuery)(
61
+ fileImporter,
62
+ gqlHandler,
63
+ value.query,
64
+ value.variables
65
+ );
66
+ let result;
67
+ try {
68
+ result = JSON.parse(resultString);
69
+ } catch (e) {
70
+ if (e instanceof SyntaxError) {
71
+ throw new import_errors.JSONParseError({
72
+ query: value.query,
73
+ variables: value.variables,
74
+ result: resultString
75
+ });
76
+ }
77
+ }
78
+ if (result.errors) {
79
+ const message = result.errors[0].message ?? JSON.stringify(result.errors, null, 4);
80
+ if (result.errors[0]?.extensions?.code === "UNAUTHENTICATED") {
81
+ const queryName = getOperationName(value.query);
82
+ console.error(
83
+ `
84
+
85
+ \u{1F6D1} Cannot prerender the query ${queryName} as it requires auth.
86
+ `
87
+ );
88
+ }
89
+ throw new import_errors.PrerenderGqlError(message);
90
+ }
91
+ queryCache[cacheKey] = {
92
+ ...value,
93
+ data: result.data,
94
+ hasProcessed: true
95
+ };
96
+ return result;
97
+ } catch (e) {
98
+ if (e instanceof import_errors.GqlHandlerImportError) {
99
+ shouldShowGraphqlHandlerNotFoundWarn = true;
100
+ queryCache[cacheKey] = {
101
+ ...value,
102
+ // tried to fetch, but failed
103
+ renderLoading: true,
104
+ hasProcessed: true
105
+ };
106
+ return;
107
+ } else {
108
+ throw e;
109
+ }
110
+ }
111
+ })
112
+ );
113
+ const prerenderUrl = process.env.RWJS_PRERENDER_ORIGIN || "http://localhost" + renderPath;
114
+ const componentAsHtml = import_server.default.renderToString(
115
+ /* @__PURE__ */ import_react.default.createElement(LocationProvider, { location: new URL(prerenderUrl) }, /* @__PURE__ */ import_react.default.createElement(CellCacheContextProvider, { queryCache }, /* @__PURE__ */ import_react.default.createElement(App, null, /* @__PURE__ */ import_react.default.createElement(Routes, null))))
116
+ );
117
+ if (Object.values(queryCache).some((value) => !value.hasProcessed)) {
118
+ return recursivelyRender(
119
+ App,
120
+ Routes,
121
+ CellCacheContextProvider,
122
+ LocationProvider,
123
+ renderPath,
124
+ fileImporter,
125
+ gqlHandler,
126
+ queryCache
127
+ );
128
+ } else {
129
+ if (shouldShowGraphqlHandlerNotFoundWarn) {
130
+ console.warn(
131
+ '\n \u26A0\uFE0F Could not load your GraphQL handler. \n Your Cells have been prerendered in the "Loading" state. \n'
132
+ );
133
+ }
134
+ return Promise.resolve(componentAsHtml);
135
+ }
136
+ }
137
+ function insertChunkLoadingScript(indexHtmlTree, renderPath) {
138
+ const prerenderRoutes = (0, import_detection.detectPrerenderRoutes)();
139
+ const route = prerenderRoutes.find((route2) => {
140
+ if (!route2.routePath) {
141
+ return false;
142
+ }
143
+ return (0, import_util.matchPath)(route2.routePath, renderPath).match;
144
+ });
145
+ if (!route) {
146
+ throw new Error("Could not find a Route matching " + renderPath);
147
+ }
148
+ if (!route.pageIdentifier) {
149
+ throw new Error(`Route for ${renderPath} has no pageIdentifier`);
150
+ }
151
+ const buildManifest = JSON.parse(
152
+ import_node_fs.default.readFileSync(
153
+ import_node_path.default.join((0, import_project_config.getPaths)().web.dist, "client-build-manifest.json"),
154
+ "utf-8"
155
+ )
156
+ );
157
+ const chunkPaths = [];
158
+ if (route?.filePath) {
159
+ const pagesIndex = route.filePath.indexOf(import_node_path.default.join("web", "src", "pages")) + 8;
160
+ const pagePath = (0, import_project_config.ensurePosixPath)(route.filePath.slice(pagesIndex));
161
+ const pageChunkPath = buildManifest[pagePath]?.file;
162
+ if (pageChunkPath) {
163
+ chunkPaths.push("/" + pageChunkPath);
164
+ }
165
+ }
166
+ if (chunkPaths.length === 0) {
167
+ return;
168
+ }
169
+ chunkPaths.forEach((chunkPath) => {
170
+ indexHtmlTree("head").prepend(
171
+ `<script defer="defer" src="${chunkPath}" type="module"></script>`
172
+ );
173
+ });
174
+ chunkPaths.forEach((chunkPath) => {
175
+ const fullChunkPath = import_node_path.default.join((0, import_project_config.getPaths)().web.dist, chunkPath);
176
+ const jsChunk = import_node_fs.default.readFileSync(fullChunkPath, "utf-8");
177
+ const matches = jsChunk.match(/export\s*\{\s*\w+ as default\s*\}/g) || [];
178
+ const lastIndex = jsChunk.lastIndexOf(matches[matches.length - 1]);
179
+ const varNameMatch = jsChunk.slice(lastIndex).match(/export\s*\{\s*(\w+) as default\s*\}/);
180
+ import_node_fs.default.writeFileSync(
181
+ fullChunkPath,
182
+ jsChunk + `globalThis.__REDWOOD__PRERENDER_PAGES = globalThis.__REDWOOD__PRERENDER_PAGES || {};
183
+ globalThis.__REDWOOD__PRERENDER_PAGES.${route?.pageIdentifier}=${varNameMatch?.[1]};
184
+ `
185
+ );
186
+ });
187
+ }
188
+ async function createCombinedEntry({ appPath, routesPath, outDir }) {
189
+ const combinedContent = `
190
+ import { LocationProvider } from '@cedarjs/router'
191
+ import { CellCacheContextProvider } from '@cedarjs/web'
192
+
193
+ import App from "${(0, import_project_config.importStatementPath)(appPath.replace(".tsx", ""))}";
194
+ import Routes from "${(0, import_project_config.importStatementPath)(routesPath.replace(".tsx", ""))}";
195
+
196
+ export { LocationProvider, CellCacheContextProvider, App, Routes };
197
+ `;
198
+ const tempFilePath = import_node_path.default.join(outDir, "__prerender-temp-entry.tsx");
199
+ await import_node_fs.default.promises.writeFile(tempFilePath, combinedContent, "utf8");
200
+ return tempFilePath;
201
+ }
202
+ const renderCache = {};
203
+ const runPrerender = async ({
204
+ queryCache,
205
+ renderPath
206
+ }) => {
207
+ (0, import_internal.registerShims)(renderPath);
208
+ const nodeRunner = new import_node_runner.NodeRunner();
209
+ const gqlHandler = await (0, import_graphql.getGqlHandler)(nodeRunner.importFile.bind(nodeRunner));
210
+ const prerenderDistPath = import_node_path.default.join((0, import_project_config.getPaths)().web.dist, "__prerender");
211
+ import_node_fs.default.mkdirSync(prerenderDistPath, { recursive: true });
212
+ if (!renderCache.App) {
213
+ const entryPath = await createCombinedEntry({
214
+ appPath: (0, import_project_config.getPaths)().web.app,
215
+ routesPath: (0, import_project_config.getPaths)().web.routes,
216
+ outDir: prerenderDistPath
217
+ });
218
+ const required = await (0, import_buildAndImport.buildAndImport)({
219
+ filepath: entryPath,
220
+ preserveTemporaryFile: true
221
+ });
222
+ renderCache.App = required.App;
223
+ renderCache.Routes = required.Routes;
224
+ renderCache.CellCacheContextProvider = required.CellCacheContextProvider;
225
+ renderCache.LocationProvider = required.LocationProvider;
226
+ }
227
+ const { LocationProvider, App, Routes, CellCacheContextProvider } = renderCache;
228
+ if (!App) {
229
+ throw new Error("App not found");
230
+ }
231
+ if (!Routes) {
232
+ throw new Error("Routes not found");
233
+ }
234
+ if (!CellCacheContextProvider) {
235
+ throw new Error("CellCacheContextProvider not found");
236
+ }
237
+ if (!LocationProvider) {
238
+ throw new Error("LocationProvider not found");
239
+ }
240
+ const componentAsHtml = await recursivelyRender(
241
+ App,
242
+ Routes,
243
+ CellCacheContextProvider,
244
+ LocationProvider,
245
+ renderPath,
246
+ nodeRunner.importFile.bind(nodeRunner),
247
+ gqlHandler,
248
+ queryCache
249
+ );
250
+ nodeRunner.close();
251
+ const { helmet } = globalThis.__REDWOOD__HELMET_CONTEXT;
252
+ Object.keys(queryCache).forEach((queryKey) => {
253
+ const { query, variables, data } = queryCache[queryKey];
254
+ prerenderApolloClient.writeQuery({
255
+ query,
256
+ variables,
257
+ data
258
+ });
259
+ });
260
+ const indexContent = import_node_fs.default.readFileSync((0, import_internal.getRootHtmlPath)()).toString();
261
+ const indexHtmlTree = (0, import_cheerio.load)(indexContent);
262
+ if (helmet) {
263
+ const helmetElements = `
264
+ ${helmet?.link.toString()}
265
+ ${helmet?.meta.toString()}
266
+ ${helmet?.script.toString()}
267
+ ${helmet?.noscript.toString()}
268
+ `;
269
+ indexHtmlTree("head").prepend(helmetElements);
270
+ const titleHtmlTag = helmet.title.toString();
271
+ if ((0, import_cheerio.load)(titleHtmlTag)("title").text() !== "") {
272
+ if (indexHtmlTree("title").text().length === 0) {
273
+ indexHtmlTree("head").prepend(titleHtmlTag);
274
+ } else {
275
+ indexHtmlTree("title").replaceWith(titleHtmlTag);
276
+ }
277
+ }
278
+ }
279
+ indexHtmlTree("head").append(
280
+ `<script> globalThis.__REDWOOD__APOLLO_STATE = ${JSON.stringify(
281
+ prerenderApolloClient.extract()
282
+ )}</script>`
283
+ );
284
+ prerenderApolloClient.resetStore();
285
+ insertChunkLoadingScript(indexHtmlTree, renderPath);
286
+ indexHtmlTree("#redwood-app").append(componentAsHtml);
287
+ const renderOutput = indexHtmlTree.html();
288
+ return renderOutput;
289
+ };
290
+ const writePrerenderedHtmlFile = (outputHtmlPath, content) => {
291
+ const outputHtmlAbsPath = import_node_path.default.join((0, import_project_config.getPaths)().base, outputHtmlPath);
292
+ if (outputHtmlPath === "web/dist/index.html") {
293
+ const html200Path = import_node_path.default.join((0, import_project_config.getPaths)().web.dist, "200.html");
294
+ if (!import_node_fs.default.existsSync(html200Path)) {
295
+ import_node_fs.default.copyFileSync(outputHtmlAbsPath, html200Path);
296
+ }
297
+ }
298
+ (0, import_internal.writeToDist)(outputHtmlAbsPath, content);
299
+ };
300
+ // Annotate the CommonJS export names for ESM import in node:
301
+ 0 && (module.exports = {
302
+ runPrerender,
303
+ writePrerenderedHtmlFile
304
+ });
@@ -1,5 +1,5 @@
1
1
  import type { DocumentNode } from 'graphql';
2
- import type { NodeRunner } from './node-runner.js';
2
+ export type FileImporter = (path: string) => Promise<any>;
3
3
  /**
4
4
  * Loads the graphql server, with all the user's settings
5
5
  * And execute the query against it
@@ -8,12 +8,12 @@ import type { NodeRunner } from './node-runner.js';
8
8
  * there is a GraphQL error. Instead, it returns the result with the graphql
9
9
  * error.
10
10
  */
11
- export declare function executeQuery(nodeRunner: NodeRunner, gqlHandler: (args: any) => Promise<any>, query: DocumentNode, variables?: Record<string, unknown>): Promise<any>;
11
+ export declare function executeQuery(fileImporter: FileImporter, gqlHandler: (args: any) => Promise<any>, query: DocumentNode, variables?: Record<string, unknown>): Promise<any>;
12
12
  /**
13
13
  * Finds the graphql handler, returns a function
14
14
  * that can be used to execute queries against it
15
15
  *
16
16
  * Throws GqlHandlerImportError, so that we can warn the user (but not blow up)
17
17
  */
18
- export declare function getGqlHandler(nodeRunner: NodeRunner): Promise<(operation: Record<string, unknown>) => Promise<any>>;
18
+ export declare function getGqlHandler(fileImporter: FileImporter): Promise<(operation: Record<string, unknown>) => Promise<any>>;
19
19
  //# sourceMappingURL=graphql.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../../src/graphql/graphql.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAS3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AASlD;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,UAAU,EACtB,UAAU,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,EACvC,KAAK,EAAE,YAAY,EACnB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,gBAmCpC;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CAAC,UAAU,EAAE,UAAU,uBAM7B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,mBAUnD"}
1
+ {"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../../src/graphql/graphql.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAS3C,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;AASzD;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,YAAY,EAAE,YAAY,EAC1B,UAAU,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,EACvC,KAAK,EAAE,YAAY,EACnB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,gBAmCpC;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CAAC,YAAY,EAAE,YAAY,uBAMjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,mBAUnD"}
@@ -3,7 +3,7 @@ import { print } from "graphql";
3
3
  import { getConfig, getPaths } from "@cedarjs/project-config";
4
4
  import { getOperationName } from "@cedarjs/web/dist/graphql.js";
5
5
  import { GqlHandlerImportError } from "../errors.js";
6
- async function executeQuery(nodeRunner, gqlHandler, query, variables) {
6
+ async function executeQuery(fileImporter, gqlHandler, query, variables) {
7
7
  const config = getConfig();
8
8
  const operationName = getOperationName(query);
9
9
  const operation = {
@@ -13,7 +13,7 @@ async function executeQuery(nodeRunner, gqlHandler, query, variables) {
13
13
  };
14
14
  if (config.graphql.trustedDocuments) {
15
15
  const documentsPath = path.join(getPaths().web.graphql, "graphql");
16
- const documents = await nodeRunner.importFile(documentsPath);
16
+ const documents = await fileImporter(documentsPath);
17
17
  const documentName = operationName[0].toUpperCase() + operationName.slice(1) + "Document";
18
18
  const queryHash = documents?.[documentName]?.__meta__?.hash;
19
19
  operation.query = void 0;
@@ -27,10 +27,10 @@ async function executeQuery(nodeRunner, gqlHandler, query, variables) {
27
27
  const handlerResult = await gqlHandler(operation);
28
28
  return handlerResult?.body;
29
29
  }
30
- async function getGqlHandler(nodeRunner) {
30
+ async function getGqlHandler(fileImporter) {
31
31
  const gqlPath = path.join(getPaths().api.functions, "graphql");
32
32
  try {
33
- const { handler } = await nodeRunner.importFile(gqlPath);
33
+ const { handler } = await fileImporter(gqlPath);
34
34
  return async (operation) => {
35
35
  return await handler(buildApiEvent(operation), buildContext());
36
36
  };
@@ -0,0 +1,9 @@
1
+ import type { QueryInfo } from '@cedarjs/web';
2
+ interface PrerenderParams {
3
+ queryCache: Record<string, QueryInfo>;
4
+ renderPath: string;
5
+ }
6
+ export declare const runPrerender: ({ queryCache, renderPath, }: PrerenderParams) => Promise<string | void>;
7
+ export declare const writePrerenderedHtmlFile: (outputHtmlPath: string, content: string) => void;
8
+ export {};
9
+ //# sourceMappingURL=runPrerenderEsm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runPrerenderEsm.d.ts","sourceRoot":"","sources":["../src/runPrerenderEsm.tsx"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAoQ7C,UAAU,eAAe;IACvB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IACrC,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,eAAO,MAAM,YAAY,gCAGtB,eAAe,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAwHzC,CAAA;AAGD,eAAO,MAAM,wBAAwB,mBACnB,MAAM,WACb,MAAM,SAchB,CAAA"}