@cedarjs/prerender 0.0.4
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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/babelPlugins/babel-plugin-redwood-prerender-media-imports.d.ts +5 -0
- package/dist/babelPlugins/babel-plugin-redwood-prerender-media-imports.d.ts.map +1 -0
- package/dist/babelPlugins/babel-plugin-redwood-prerender-media-imports.js +94 -0
- package/dist/babelPlugins/utils.d.ts +2 -0
- package/dist/babelPlugins/utils.d.ts.map +1 -0
- package/dist/babelPlugins/utils.js +49 -0
- package/dist/browserUtils/index.d.ts +8 -0
- package/dist/browserUtils/index.d.ts.map +1 -0
- package/dist/browserUtils/index.js +47 -0
- package/dist/detection/index.d.ts +11 -0
- package/dist/detection/index.d.ts.map +1 -0
- package/dist/detection/index.js +48 -0
- package/dist/errors.d.ts +19 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +52 -0
- package/dist/graphql/graphql.d.ts +19 -0
- package/dist/graphql/graphql.d.ts.map +1 -0
- package/dist/graphql/graphql.js +152 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/internal.d.ts +4 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +83 -0
- package/dist/runPrerender.d.ts +9 -0
- package/dist/runPrerender.d.ts.map +1 -0
- package/dist/runPrerender.js +293 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Cedar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Prerender
|
|
2
|
+
|
|
3
|
+
- [Prerender](#prerender)
|
|
4
|
+
- [Purpose and Vision](#purpose-and-vision)
|
|
5
|
+
- [Package Leads](#package-leads)
|
|
6
|
+
- [Contributing](#contributing)
|
|
7
|
+
|
|
8
|
+
## Purpose and Vision
|
|
9
|
+
|
|
10
|
+
Build-time prerender for pages that don't have dynamic content. See:
|
|
11
|
+
https://redwoodjs.com/docs/prerender
|
|
12
|
+
|
|
13
|
+
## Package Leads
|
|
14
|
+
|
|
15
|
+
- Daniel Choudhury (@dac09)
|
|
16
|
+
|
|
17
|
+
## Contributing
|
|
18
|
+
|
|
19
|
+
**ToDo**
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babel-plugin-redwood-prerender-media-imports.d.ts","sourceRoot":"","sources":["../../src/babelPlugins/babel-plugin-redwood-prerender-media-imports.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAY,MAAM,aAAa,CAAA;AAsC7D,MAAM,CAAC,OAAO,WAAW,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IAAE,KAAK,EAAE,OAAO,KAAK,CAAA;CAAE,GAAG,SAAS,CAqDzE"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var babel_plugin_redwood_prerender_media_imports_exports = {};
|
|
20
|
+
__export(babel_plugin_redwood_prerender_media_imports_exports, {
|
|
21
|
+
default: () => babel_plugin_redwood_prerender_media_imports_default
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(babel_plugin_redwood_prerender_media_imports_exports);
|
|
24
|
+
var import_path = require("path");
|
|
25
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
26
|
+
var import_utils = require("./utils");
|
|
27
|
+
const defaultOptions = {
|
|
28
|
+
// This list of extensions matches config for file-loader in
|
|
29
|
+
extensions: [
|
|
30
|
+
".ico",
|
|
31
|
+
".jpg",
|
|
32
|
+
".jpeg",
|
|
33
|
+
".png",
|
|
34
|
+
".gif",
|
|
35
|
+
".svg",
|
|
36
|
+
".eot",
|
|
37
|
+
".otf",
|
|
38
|
+
".webp",
|
|
39
|
+
".ttf",
|
|
40
|
+
".woff",
|
|
41
|
+
".woff2",
|
|
42
|
+
".cur",
|
|
43
|
+
".ani",
|
|
44
|
+
".pdf",
|
|
45
|
+
".bmp"
|
|
46
|
+
]
|
|
47
|
+
};
|
|
48
|
+
function getVariableName(p) {
|
|
49
|
+
if (p.node.specifiers?.[0]?.local) {
|
|
50
|
+
return p.node.specifiers[0].local.name;
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
function babel_plugin_redwood_prerender_media_imports_default({ types: t }) {
|
|
55
|
+
const manifestPath = (0, import_path.join)((0, import_project_config.getPaths)().web.dist, "client-build-manifest.json");
|
|
56
|
+
const buildManifest = require(manifestPath);
|
|
57
|
+
return {
|
|
58
|
+
name: "babel-plugin-redwood-prerender-media-imports",
|
|
59
|
+
visitor: {
|
|
60
|
+
ImportDeclaration(p, state) {
|
|
61
|
+
const importPath = p.node.source.value;
|
|
62
|
+
const ext = (0, import_path.extname)(importPath);
|
|
63
|
+
const options = {
|
|
64
|
+
...defaultOptions,
|
|
65
|
+
...state.opts
|
|
66
|
+
};
|
|
67
|
+
if (ext && options.extensions.includes(ext)) {
|
|
68
|
+
const importConstName = getVariableName(p);
|
|
69
|
+
if (state.filename === void 0) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const absPath = (0, import_path.join)((0, import_path.dirname)(state.filename), p.node.source.value);
|
|
73
|
+
const viteManifestKey = (0, import_project_config.ensurePosixPath)(
|
|
74
|
+
(0, import_path.relative)((0, import_project_config.getPaths)().web.src, absPath)
|
|
75
|
+
);
|
|
76
|
+
const copiedAssetPath = buildManifest[viteManifestKey]?.file;
|
|
77
|
+
const assetSrc = copiedAssetPath ?? (0, import_utils.convertToDataUrl)(
|
|
78
|
+
(0, import_path.join)(state.file.opts.sourceRoot || "./", importPath)
|
|
79
|
+
);
|
|
80
|
+
if (importConstName) {
|
|
81
|
+
p.replaceWith(
|
|
82
|
+
t.variableDeclaration("const", [
|
|
83
|
+
t.variableDeclarator(
|
|
84
|
+
t.identifier(importConstName),
|
|
85
|
+
t.stringLiteral(assetSrc)
|
|
86
|
+
)
|
|
87
|
+
])
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/babelPlugins/utils.ts"],"names":[],"mappings":"AAQA,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,UASjD"}
|
|
@@ -0,0 +1,49 @@
|
|
|
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 utils_exports = {};
|
|
30
|
+
__export(utils_exports, {
|
|
31
|
+
convertToDataUrl: () => convertToDataUrl
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(utils_exports);
|
|
34
|
+
var import_fs = __toESM(require("fs"));
|
|
35
|
+
var import_mime_types = __toESM(require("mime-types"));
|
|
36
|
+
function convertToDataUrl(assetPath) {
|
|
37
|
+
try {
|
|
38
|
+
const base64AssetContents = import_fs.default.readFileSync(assetPath, "base64");
|
|
39
|
+
const mimeType = import_mime_types.default.lookup(assetPath);
|
|
40
|
+
return `data:${mimeType};base64,${base64AssetContents}`;
|
|
41
|
+
} catch {
|
|
42
|
+
console.warn(`Could not read file ${assetPath} for conversion to data uri`);
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
convertToDataUrl
|
|
49
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
export declare const isPrerendering: () => boolean;
|
|
3
|
+
export declare const isBrowser: boolean;
|
|
4
|
+
export declare const useIsBrowser: () => boolean;
|
|
5
|
+
export declare const BrowserOnly: ({ children }: {
|
|
6
|
+
children: ReactElement;
|
|
7
|
+
}) => ReactElement<any, string | React.JSXElementConstructor<any>> | null;
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/browserUtils/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAKzC,eAAO,MAAM,cAAc,QAAO,OAEjC,CAAA;AAED,eAAO,MAAM,SAAS,SAAoB,CAAA;AAE1C,eAAO,MAAM,YAAY,eAIxB,CAAA;AAED,eAAO,MAAM,WAAW,iBAAkB;IAAE,QAAQ,EAAE,YAAY,CAAA;CAAE,wEAInE,CAAA"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var browserUtils_exports = {};
|
|
20
|
+
__export(browserUtils_exports, {
|
|
21
|
+
BrowserOnly: () => BrowserOnly,
|
|
22
|
+
isBrowser: () => isBrowser,
|
|
23
|
+
isPrerendering: () => isPrerendering,
|
|
24
|
+
useIsBrowser: () => useIsBrowser
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(browserUtils_exports);
|
|
27
|
+
var import_react = require("react");
|
|
28
|
+
const isPrerendering = () => {
|
|
29
|
+
return globalThis.__REDWOOD__PRERENDERING ?? false;
|
|
30
|
+
};
|
|
31
|
+
const isBrowser = !isPrerendering();
|
|
32
|
+
const useIsBrowser = () => {
|
|
33
|
+
return (0, import_react.useMemo)(() => {
|
|
34
|
+
return !globalThis?.__REDWOOD__PRERENDERING;
|
|
35
|
+
}, []);
|
|
36
|
+
};
|
|
37
|
+
const BrowserOnly = ({ children }) => {
|
|
38
|
+
const isBrowser2 = useIsBrowser();
|
|
39
|
+
return isBrowser2 ? children : null;
|
|
40
|
+
};
|
|
41
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
+
0 && (module.exports = {
|
|
43
|
+
BrowserOnly,
|
|
44
|
+
isBrowser,
|
|
45
|
+
isPrerendering,
|
|
46
|
+
useIsBrowser
|
|
47
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const detectPrerenderRoutes: () => {
|
|
2
|
+
name: string | undefined;
|
|
3
|
+
path: string | undefined;
|
|
4
|
+
routePath: string | undefined;
|
|
5
|
+
hasParams: boolean;
|
|
6
|
+
id: string;
|
|
7
|
+
isNotFound: boolean;
|
|
8
|
+
filePath: string | undefined;
|
|
9
|
+
pageIdentifier: string | undefined;
|
|
10
|
+
}[];
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/detection/index.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,qBAAqB;;;;;;;;;GAsBjC,CAAA"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var detection_exports = {};
|
|
20
|
+
__export(detection_exports, {
|
|
21
|
+
detectPrerenderRoutes: () => detectPrerenderRoutes
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(detection_exports);
|
|
24
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
25
|
+
var import_structure = require("@cedarjs/structure");
|
|
26
|
+
const detectPrerenderRoutes = () => {
|
|
27
|
+
const rwProject = (0, import_structure.getProject)((0, import_project_config.getPaths)().base);
|
|
28
|
+
const routes = rwProject.getRouter().routes;
|
|
29
|
+
const prerenderRoutes = routes.filter((route) => route.prerender).map((route) => ({
|
|
30
|
+
name: route.isNotFound ? "404" : route.name,
|
|
31
|
+
// `path` will be updated/expanded later where route parameters will be
|
|
32
|
+
// replaced with actual values
|
|
33
|
+
path: route.isNotFound ? "/404" : route.path,
|
|
34
|
+
// `routePath` is always the path specified on the <Route> component
|
|
35
|
+
// (or the special /404 path)
|
|
36
|
+
routePath: route.isNotFound ? "/404" : route.path,
|
|
37
|
+
hasParams: route.hasParameters,
|
|
38
|
+
id: route.id,
|
|
39
|
+
isNotFound: route.isNotFound,
|
|
40
|
+
filePath: route.page?.filePath,
|
|
41
|
+
pageIdentifier: route.page_identifier_str
|
|
42
|
+
}));
|
|
43
|
+
return prerenderRoutes;
|
|
44
|
+
};
|
|
45
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
46
|
+
0 && (module.exports = {
|
|
47
|
+
detectPrerenderRoutes
|
|
48
|
+
});
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DocumentNode } from 'graphql';
|
|
2
|
+
export declare class PrerenderGqlError extends Error {
|
|
3
|
+
stack: string;
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
export declare class GqlHandlerImportError extends Error {
|
|
7
|
+
stack: string;
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
interface JSONParseErrorArgs {
|
|
11
|
+
query: DocumentNode;
|
|
12
|
+
variables?: Record<string, unknown>;
|
|
13
|
+
result: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class JSONParseError extends Error {
|
|
16
|
+
constructor({ query, variables, result }: JSONParseErrorArgs);
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAG3C,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,KAAK,EAAE,MAAM,CAAA;gBAED,OAAO,EAAE,MAAM;CAO5B;AAED,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,KAAK,EAAE,MAAM,CAAA;gBAED,OAAO,EAAE,MAAM;CAO5B;AAED,UAAU,kBAAkB;IAC1B,KAAK,EAAE,YAAY,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACnC,MAAM,EAAE,MAAM,CAAA;CACf;AAED,qBAAa,cAAe,SAAQ,KAAK;gBAC3B,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,kBAAkB;CAY7D"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var errors_exports = {};
|
|
20
|
+
__export(errors_exports, {
|
|
21
|
+
GqlHandlerImportError: () => GqlHandlerImportError,
|
|
22
|
+
JSONParseError: () => JSONParseError,
|
|
23
|
+
PrerenderGqlError: () => PrerenderGqlError
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(errors_exports);
|
|
26
|
+
var import_graphql = require("graphql");
|
|
27
|
+
class PrerenderGqlError extends Error {
|
|
28
|
+
stack;
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super("GQL error: " + message);
|
|
31
|
+
this.stack = this.message;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class GqlHandlerImportError extends Error {
|
|
35
|
+
stack;
|
|
36
|
+
constructor(message) {
|
|
37
|
+
super("Gql Handler Import Error: " + message);
|
|
38
|
+
this.stack = this.message;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
class JSONParseError extends Error {
|
|
42
|
+
constructor({ query, variables, result }) {
|
|
43
|
+
const message = "Could not parse the GraphQL response.\n Request: " + (0, import_graphql.print)(query).split("\n").join("\n ") + "\n Variables: " + JSON.stringify(variables) + "\n Response: " + result;
|
|
44
|
+
super(message);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
GqlHandlerImportError,
|
|
50
|
+
JSONParseError,
|
|
51
|
+
PrerenderGqlError
|
|
52
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DocumentNode } from 'graphql';
|
|
2
|
+
/**
|
|
3
|
+
* Loads the graphql server, with all the user's settings
|
|
4
|
+
* And execute the query against it
|
|
5
|
+
*
|
|
6
|
+
* Note that this function does NOT throw errors, even when
|
|
7
|
+
* there is a GraphQL error. Instead, it returns the result with the graphql error.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Promise<QueryResult>}
|
|
10
|
+
*/
|
|
11
|
+
export declare function executeQuery(gqlHandler: (args: any) => Promise<any>, query: DocumentNode, variables?: Record<string, unknown>): Promise<any>;
|
|
12
|
+
/**
|
|
13
|
+
* Finds the graphql handler, returns a function
|
|
14
|
+
* that can be used to execute queries against it
|
|
15
|
+
*
|
|
16
|
+
* Throws GqlHandlerImportError, so that we can warn the user (but not blow up)
|
|
17
|
+
*/
|
|
18
|
+
export declare function getGqlHandler(): Promise<(operation: Record<string, unknown>) => Promise<any>>;
|
|
19
|
+
//# sourceMappingURL=graphql.d.ts.map
|
|
@@ -0,0 +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;AAgB3C;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,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,gBAkCpC;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,wBAMN,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,mBAUnD"}
|
|
@@ -0,0 +1,152 @@
|
|
|
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 graphql_exports = {};
|
|
30
|
+
__export(graphql_exports, {
|
|
31
|
+
executeQuery: () => executeQuery,
|
|
32
|
+
getGqlHandler: () => getGqlHandler
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(graphql_exports);
|
|
35
|
+
var import_path = __toESM(require("path"));
|
|
36
|
+
var import_graphql = require("graphql");
|
|
37
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
38
|
+
var import_graphql2 = require("@cedarjs/web/dist/graphql.js");
|
|
39
|
+
var import_errors = require("../errors");
|
|
40
|
+
async function executeQuery(gqlHandler, query, variables) {
|
|
41
|
+
const config = (0, import_project_config.getConfig)();
|
|
42
|
+
const operationName = (0, import_graphql2.getOperationName)(query);
|
|
43
|
+
const operation = {
|
|
44
|
+
operationName,
|
|
45
|
+
query: (0, import_graphql.print)(query),
|
|
46
|
+
variables
|
|
47
|
+
};
|
|
48
|
+
if (config.graphql.trustedDocuments) {
|
|
49
|
+
const documentsPath = import_path.default.join((0, import_project_config.getPaths)().web.graphql, "graphql");
|
|
50
|
+
const documents = require(documentsPath);
|
|
51
|
+
const documentName = operationName[0].toUpperCase() + operationName.slice(1) + "Document";
|
|
52
|
+
const queryHash = documents?.[documentName]?.__meta__?.hash;
|
|
53
|
+
operation.query = void 0;
|
|
54
|
+
operation.extensions = {
|
|
55
|
+
persistedQuery: {
|
|
56
|
+
version: 1,
|
|
57
|
+
sha256Hash: queryHash
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const handlerResult = await gqlHandler(operation);
|
|
62
|
+
return handlerResult?.body;
|
|
63
|
+
}
|
|
64
|
+
async function getGqlHandler() {
|
|
65
|
+
const gqlPath = import_path.default.join((0, import_project_config.getPaths)().api.functions, "graphql");
|
|
66
|
+
try {
|
|
67
|
+
const { handler } = require(gqlPath);
|
|
68
|
+
return async (operation) => {
|
|
69
|
+
return await handler(buildApiEvent(operation), buildContext());
|
|
70
|
+
};
|
|
71
|
+
} catch {
|
|
72
|
+
return () => {
|
|
73
|
+
throw new import_errors.GqlHandlerImportError(
|
|
74
|
+
`Unable to import GraphQL handler at ${gqlPath}`
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function buildApiEvent(body) {
|
|
80
|
+
return {
|
|
81
|
+
body: JSON.stringify(body),
|
|
82
|
+
headers: {
|
|
83
|
+
origin: "http://localhost:8910",
|
|
84
|
+
"content-type": "application/json",
|
|
85
|
+
accept: "*/*",
|
|
86
|
+
host: "localhost:8910"
|
|
87
|
+
},
|
|
88
|
+
multiValueHeaders: {},
|
|
89
|
+
httpMethod: "POST",
|
|
90
|
+
isBase64Encoded: false,
|
|
91
|
+
path: "/graphql",
|
|
92
|
+
pathParameters: null,
|
|
93
|
+
queryStringParameters: {},
|
|
94
|
+
multiValueQueryStringParameters: null,
|
|
95
|
+
stageVariables: null,
|
|
96
|
+
resource: "",
|
|
97
|
+
requestContext: {
|
|
98
|
+
requestId: "req-3",
|
|
99
|
+
identity: {
|
|
100
|
+
sourceIp: "::1",
|
|
101
|
+
accessKey: null,
|
|
102
|
+
accountId: null,
|
|
103
|
+
apiKey: null,
|
|
104
|
+
apiKeyId: null,
|
|
105
|
+
caller: null,
|
|
106
|
+
clientCert: null,
|
|
107
|
+
cognitoAuthenticationProvider: null,
|
|
108
|
+
cognitoAuthenticationType: null,
|
|
109
|
+
cognitoIdentityId: null,
|
|
110
|
+
cognitoIdentityPoolId: null,
|
|
111
|
+
principalOrgId: null,
|
|
112
|
+
user: null,
|
|
113
|
+
userAgent: null,
|
|
114
|
+
userArn: null
|
|
115
|
+
},
|
|
116
|
+
authorizer: {},
|
|
117
|
+
protocol: "http",
|
|
118
|
+
httpMethod: "POST",
|
|
119
|
+
path: "/graphql",
|
|
120
|
+
stage: "",
|
|
121
|
+
requestTimeEpoch: 0,
|
|
122
|
+
resourceId: "",
|
|
123
|
+
resourcePath: "",
|
|
124
|
+
accountId: "",
|
|
125
|
+
apiId: ""
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function buildContext() {
|
|
130
|
+
return {
|
|
131
|
+
callbackWaitsForEmptyEventLoop: false,
|
|
132
|
+
functionName: "",
|
|
133
|
+
functionVersion: "",
|
|
134
|
+
invokedFunctionArn: "",
|
|
135
|
+
memoryLimitInMB: "",
|
|
136
|
+
awsRequestId: "",
|
|
137
|
+
logGroupName: "",
|
|
138
|
+
logStreamName: "",
|
|
139
|
+
getRemainingTimeInMillis: () => 100,
|
|
140
|
+
done: () => {
|
|
141
|
+
},
|
|
142
|
+
fail: () => {
|
|
143
|
+
},
|
|
144
|
+
succeed: () => {
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
149
|
+
0 && (module.exports = {
|
|
150
|
+
executeQuery,
|
|
151
|
+
getGqlHandler
|
|
152
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var index_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(index_exports);
|
|
18
|
+
__reExport(index_exports, require("./runPrerender"), module.exports);
|
|
19
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
20
|
+
0 && (module.exports = {
|
|
21
|
+
...require("./runPrerender")
|
|
22
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,eAAe,cAM3B,CAAA;AAED,eAAO,MAAM,aAAa,eAAgB,MAAM,SAmC/C,CAAA;AAED,eAAO,MAAM,WAAW,mBAAoB,MAAM,gBAAgB,MAAM,SAQvE,CAAA"}
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
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 internal_exports = {};
|
|
30
|
+
__export(internal_exports, {
|
|
31
|
+
getRootHtmlPath: () => getRootHtmlPath,
|
|
32
|
+
registerShims: () => registerShims,
|
|
33
|
+
writeToDist: () => writeToDist
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(internal_exports);
|
|
36
|
+
var import_fs = __toESM(require("fs"));
|
|
37
|
+
var import_path = __toESM(require("path"));
|
|
38
|
+
var import_fetch = require("@whatwg-node/fetch");
|
|
39
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
40
|
+
const INDEX_FILE = import_path.default.join((0, import_project_config.getPaths)().web.dist, "index.html");
|
|
41
|
+
const DEFAULT_INDEX = import_path.default.join((0, import_project_config.getPaths)().web.dist, "200.html");
|
|
42
|
+
const getRootHtmlPath = () => {
|
|
43
|
+
if (import_fs.default.existsSync(DEFAULT_INDEX)) {
|
|
44
|
+
return DEFAULT_INDEX;
|
|
45
|
+
} else {
|
|
46
|
+
return INDEX_FILE;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const registerShims = (routerPath) => {
|
|
50
|
+
const rwjsConfig = (0, import_project_config.getConfig)();
|
|
51
|
+
globalThis.RWJS_ENV = {
|
|
52
|
+
RWJS_API_GRAPHQL_URL: rwjsConfig.web.apiGraphQLUrl ?? rwjsConfig.web.apiUrl + "/graphql",
|
|
53
|
+
RWJS_API_URL: rwjsConfig.web.apiUrl,
|
|
54
|
+
__REDWOOD__APP_TITLE: rwjsConfig.web.title
|
|
55
|
+
};
|
|
56
|
+
globalThis.RWJS_DEBUG_ENV = {
|
|
57
|
+
RWJS_SRC_ROOT: (0, import_project_config.getPaths)().web.src
|
|
58
|
+
};
|
|
59
|
+
globalThis.__REDWOOD__PRERENDERING = true;
|
|
60
|
+
globalThis.__REDWOOD__HELMET_CONTEXT = {};
|
|
61
|
+
process.env.__REDWOOD__PRERENDERING = "1";
|
|
62
|
+
globalThis.location = {
|
|
63
|
+
...globalThis.location,
|
|
64
|
+
pathname: routerPath
|
|
65
|
+
};
|
|
66
|
+
if (!globalThis.fetch) {
|
|
67
|
+
globalThis.fetch = import_fetch.fetch;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const writeToDist = (outputHtmlPath, renderOutput) => {
|
|
71
|
+
const dirName = import_path.default.dirname(outputHtmlPath);
|
|
72
|
+
const exist = import_fs.default.existsSync(dirName);
|
|
73
|
+
if (!exist) {
|
|
74
|
+
import_fs.default.mkdirSync(dirName, { recursive: true });
|
|
75
|
+
}
|
|
76
|
+
import_fs.default.writeFileSync(outputHtmlPath, renderOutput);
|
|
77
|
+
};
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
getRootHtmlPath,
|
|
81
|
+
registerShims,
|
|
82
|
+
writeToDist
|
|
83
|
+
});
|
|
@@ -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=runPrerender.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runPrerender.d.ts","sourceRoot":"","sources":["../src/runPrerender.tsx"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAsN7C,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,CA4HzC,CAAA;AAGD,eAAO,MAAM,wBAAwB,mBACnB,MAAM,WACb,MAAM,SAchB,CAAA"}
|
|
@@ -0,0 +1,293 @@
|
|
|
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_fs = __toESM(require("fs"));
|
|
36
|
+
var import_path = __toESM(require("path"));
|
|
37
|
+
var import_react = __toESM(require("react"));
|
|
38
|
+
var import_client = require("@apollo/client");
|
|
39
|
+
var import_cheerio = require("cheerio");
|
|
40
|
+
var import_server = __toESM(require("react-dom/server"));
|
|
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_prerender_media_imports = __toESM(require("./babelPlugins/babel-plugin-redwood-prerender-media-imports"));
|
|
46
|
+
var import_detection = require("./detection");
|
|
47
|
+
var import_errors = require("./errors");
|
|
48
|
+
var import_graphql = require("./graphql/graphql");
|
|
49
|
+
var import_internal = require("./internal");
|
|
50
|
+
const prerenderApolloClient = new import_client.ApolloClient({ cache: new import_client.InMemoryCache() });
|
|
51
|
+
async function recursivelyRender(App, Routes, renderPath, gqlHandler, queryCache) {
|
|
52
|
+
const { CellCacheContextProvider, getOperationName } = require("@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
|
+
gqlHandler,
|
|
62
|
+
value.query,
|
|
63
|
+
value.variables
|
|
64
|
+
);
|
|
65
|
+
let result;
|
|
66
|
+
try {
|
|
67
|
+
result = JSON.parse(resultString);
|
|
68
|
+
} catch (e) {
|
|
69
|
+
if (e instanceof SyntaxError) {
|
|
70
|
+
throw new import_errors.JSONParseError({
|
|
71
|
+
query: value.query,
|
|
72
|
+
variables: value.variables,
|
|
73
|
+
result: resultString
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (result.errors) {
|
|
78
|
+
const message = result.errors[0].message ?? JSON.stringify(result.errors, null, 4);
|
|
79
|
+
if (result.errors[0]?.extensions?.code === "UNAUTHENTICATED") {
|
|
80
|
+
console.error(
|
|
81
|
+
`
|
|
82
|
+
|
|
83
|
+
\u{1F6D1} Cannot prerender the query ${getOperationName(
|
|
84
|
+
value.query
|
|
85
|
+
)} 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(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))))
|
|
116
|
+
);
|
|
117
|
+
if (Object.values(queryCache).some((value) => !value.hasProcessed)) {
|
|
118
|
+
return recursivelyRender(App, Routes, renderPath, gqlHandler, queryCache);
|
|
119
|
+
} else {
|
|
120
|
+
if (shouldShowGraphqlHandlerNotFoundWarn) {
|
|
121
|
+
console.warn(
|
|
122
|
+
'\n \u26A0\uFE0F Could not load your GraphQL handler. \n Your Cells have been prerendered in the "Loading" state. \n'
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return Promise.resolve(componentAsHtml);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function insertChunkLoadingScript(indexHtmlTree, renderPath) {
|
|
129
|
+
const prerenderRoutes = (0, import_detection.detectPrerenderRoutes)();
|
|
130
|
+
const route = prerenderRoutes.find((route2) => {
|
|
131
|
+
if (!route2.routePath) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
return (0, import_util.matchPath)(route2.routePath, renderPath).match;
|
|
135
|
+
});
|
|
136
|
+
if (!route) {
|
|
137
|
+
throw new Error("Could not find a Route matching " + renderPath);
|
|
138
|
+
}
|
|
139
|
+
if (!route.pageIdentifier) {
|
|
140
|
+
throw new Error(`Route for ${renderPath} has no pageIdentifier`);
|
|
141
|
+
}
|
|
142
|
+
const buildManifest = JSON.parse(
|
|
143
|
+
import_fs.default.readFileSync(
|
|
144
|
+
import_path.default.join((0, import_project_config.getPaths)().web.dist, "client-build-manifest.json"),
|
|
145
|
+
"utf-8"
|
|
146
|
+
)
|
|
147
|
+
);
|
|
148
|
+
const chunkPaths = [];
|
|
149
|
+
if (route?.filePath) {
|
|
150
|
+
const pagesIndex = route.filePath.indexOf(import_path.default.join("web", "src", "pages")) + 8;
|
|
151
|
+
const pagePath = (0, import_project_config.ensurePosixPath)(route.filePath.slice(pagesIndex));
|
|
152
|
+
const pageChunkPath = buildManifest[pagePath]?.file;
|
|
153
|
+
if (pageChunkPath) {
|
|
154
|
+
chunkPaths.push("/" + pageChunkPath);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (chunkPaths.length === 0) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
chunkPaths.forEach((chunkPath) => {
|
|
161
|
+
indexHtmlTree("head").prepend(
|
|
162
|
+
`<script defer="defer" src="${chunkPath}" type="module"></script>`
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
chunkPaths.forEach((chunkPath) => {
|
|
166
|
+
const fullChunkPath = import_path.default.join((0, import_project_config.getPaths)().web.dist, chunkPath);
|
|
167
|
+
const jsChunk = import_fs.default.readFileSync(fullChunkPath, "utf-8");
|
|
168
|
+
const matches = jsChunk.match(/export\s*\{\s*\w+ as default\s*\}/g) || [];
|
|
169
|
+
const lastIndex = jsChunk.lastIndexOf(matches[matches.length - 1]);
|
|
170
|
+
const varNameMatch = jsChunk.slice(lastIndex).match(/export\s*\{\s*(\w+) as default\s*\}/);
|
|
171
|
+
import_fs.default.writeFileSync(
|
|
172
|
+
fullChunkPath,
|
|
173
|
+
jsChunk + `globalThis.__REDWOOD__PRERENDER_PAGES = globalThis.__REDWOOD__PRERENDER_PAGES || {};
|
|
174
|
+
globalThis.__REDWOOD__PRERENDER_PAGES.${route?.pageIdentifier}=${varNameMatch?.[1]};
|
|
175
|
+
`
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const runPrerender = async ({
|
|
180
|
+
queryCache,
|
|
181
|
+
renderPath
|
|
182
|
+
}) => {
|
|
183
|
+
(0, import_internal.registerShims)(renderPath);
|
|
184
|
+
(0, import_babel_config.registerApiSideBabelHook)({
|
|
185
|
+
plugins: [
|
|
186
|
+
[
|
|
187
|
+
"babel-plugin-module-resolver",
|
|
188
|
+
{
|
|
189
|
+
alias: {
|
|
190
|
+
api: (0, import_project_config.getPaths)().api.base,
|
|
191
|
+
web: (0, import_project_config.getPaths)().web.base
|
|
192
|
+
},
|
|
193
|
+
loglevel: "silent"
|
|
194
|
+
// to silence the unnecessary warnings
|
|
195
|
+
}
|
|
196
|
+
]
|
|
197
|
+
],
|
|
198
|
+
overrides: [
|
|
199
|
+
{
|
|
200
|
+
test: ["./api/"],
|
|
201
|
+
plugins: [
|
|
202
|
+
[
|
|
203
|
+
"babel-plugin-module-resolver",
|
|
204
|
+
{
|
|
205
|
+
alias: {
|
|
206
|
+
src: (0, import_project_config.getPaths)().api.src
|
|
207
|
+
},
|
|
208
|
+
loglevel: "silent"
|
|
209
|
+
},
|
|
210
|
+
"exec-api-src-module-resolver"
|
|
211
|
+
]
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
]
|
|
215
|
+
});
|
|
216
|
+
const gqlHandler = await (0, import_graphql.getGqlHandler)();
|
|
217
|
+
(0, import_babel_config.registerWebSideBabelHook)({
|
|
218
|
+
overrides: [
|
|
219
|
+
{
|
|
220
|
+
plugins: [
|
|
221
|
+
["ignore-html-and-css-imports"],
|
|
222
|
+
// webpack/postcss handles CSS imports
|
|
223
|
+
[import_babel_plugin_redwood_prerender_media_imports.default]
|
|
224
|
+
]
|
|
225
|
+
}
|
|
226
|
+
],
|
|
227
|
+
options: {
|
|
228
|
+
forPrerender: true
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
const indexContent = import_fs.default.readFileSync((0, import_internal.getRootHtmlPath)()).toString();
|
|
232
|
+
const { default: App } = require((0, import_project_config.getPaths)().web.app);
|
|
233
|
+
const { default: Routes } = require((0, import_project_config.getPaths)().web.routes);
|
|
234
|
+
const componentAsHtml = await recursivelyRender(
|
|
235
|
+
App,
|
|
236
|
+
Routes,
|
|
237
|
+
renderPath,
|
|
238
|
+
gqlHandler,
|
|
239
|
+
queryCache
|
|
240
|
+
);
|
|
241
|
+
const { helmet } = globalThis.__REDWOOD__HELMET_CONTEXT;
|
|
242
|
+
Object.keys(queryCache).forEach((queryKey) => {
|
|
243
|
+
const { query, variables, data } = queryCache[queryKey];
|
|
244
|
+
prerenderApolloClient.writeQuery({
|
|
245
|
+
query,
|
|
246
|
+
variables,
|
|
247
|
+
data
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
const indexHtmlTree = (0, import_cheerio.load)(indexContent);
|
|
251
|
+
if (helmet) {
|
|
252
|
+
const helmetElements = `
|
|
253
|
+
${helmet?.link.toString()}
|
|
254
|
+
${helmet?.meta.toString()}
|
|
255
|
+
${helmet?.script.toString()}
|
|
256
|
+
${helmet?.noscript.toString()}
|
|
257
|
+
`;
|
|
258
|
+
indexHtmlTree("head").prepend(helmetElements);
|
|
259
|
+
const titleHtmlTag = helmet.title.toString();
|
|
260
|
+
if ((0, import_cheerio.load)(titleHtmlTag)("title").text() !== "") {
|
|
261
|
+
if (indexHtmlTree("title").text().length === 0) {
|
|
262
|
+
indexHtmlTree("head").prepend(titleHtmlTag);
|
|
263
|
+
} else {
|
|
264
|
+
indexHtmlTree("title").replaceWith(titleHtmlTag);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
indexHtmlTree("head").append(
|
|
269
|
+
`<script> globalThis.__REDWOOD__APOLLO_STATE = ${JSON.stringify(
|
|
270
|
+
prerenderApolloClient.extract()
|
|
271
|
+
)}</script>`
|
|
272
|
+
);
|
|
273
|
+
prerenderApolloClient.resetStore();
|
|
274
|
+
insertChunkLoadingScript(indexHtmlTree, renderPath);
|
|
275
|
+
indexHtmlTree("#redwood-app").append(componentAsHtml);
|
|
276
|
+
const renderOutput = indexHtmlTree.html();
|
|
277
|
+
return renderOutput;
|
|
278
|
+
};
|
|
279
|
+
const writePrerenderedHtmlFile = (outputHtmlPath, content) => {
|
|
280
|
+
const outputHtmlAbsPath = import_path.default.join((0, import_project_config.getPaths)().base, outputHtmlPath);
|
|
281
|
+
if (outputHtmlPath === "web/dist/index.html") {
|
|
282
|
+
const html200Path = import_path.default.join((0, import_project_config.getPaths)().web.dist, "200.html");
|
|
283
|
+
if (!import_fs.default.existsSync(html200Path)) {
|
|
284
|
+
import_fs.default.copyFileSync(outputHtmlAbsPath, html200Path);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
(0, import_internal.writeToDist)(outputHtmlAbsPath, content);
|
|
288
|
+
};
|
|
289
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
290
|
+
0 && (module.exports = {
|
|
291
|
+
runPrerender,
|
|
292
|
+
writePrerenderedHtmlFile
|
|
293
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cedarjs/prerender",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "CedarJS prerender",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
8
|
+
"directory": "packages/prerender"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./browserUtils": {
|
|
18
|
+
"types": "./dist/browserUtils/index.d.ts",
|
|
19
|
+
"default": "./dist/browserUtils/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./detection": {
|
|
22
|
+
"types": "./dist/detection/index.d.ts",
|
|
23
|
+
"default": "./dist/detection/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"main": "dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsx ./build.mts && yarn build:types",
|
|
33
|
+
"build:pack": "yarn pack -o cedar-prerender.tgz",
|
|
34
|
+
"build:types": "tsc --build --verbose ./tsconfig.build.json",
|
|
35
|
+
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx,template\" --ignore dist --exec \"yarn build\"",
|
|
36
|
+
"check:attw": "yarn rw-fwtools-attw",
|
|
37
|
+
"check:package": "concurrently npm:check:attw yarn:publint",
|
|
38
|
+
"prepublishOnly": "yarn build",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:watch": "vitest watch"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@cedarjs/babel-config": "0.0.4",
|
|
44
|
+
"@cedarjs/project-config": "0.0.4",
|
|
45
|
+
"@cedarjs/router": "0.0.4",
|
|
46
|
+
"@cedarjs/structure": "0.0.4",
|
|
47
|
+
"@cedarjs/web": "0.0.4",
|
|
48
|
+
"@whatwg-node/fetch": "0.9.21",
|
|
49
|
+
"babel-plugin-ignore-html-and-css-imports": "0.1.0",
|
|
50
|
+
"cheerio": "1.0.0",
|
|
51
|
+
"graphql": "16.9.0",
|
|
52
|
+
"mime-types": "2.1.35"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@cedarjs/framework-tools": "0.0.4",
|
|
56
|
+
"@types/mime-types": "2.1.4",
|
|
57
|
+
"babel-plugin-tester": "11.0.4",
|
|
58
|
+
"concurrently": "8.2.2",
|
|
59
|
+
"publint": "0.3.11",
|
|
60
|
+
"tsx": "4.19.3",
|
|
61
|
+
"typescript": "5.6.2",
|
|
62
|
+
"vitest": "2.1.9"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"react": "18.3.1",
|
|
66
|
+
"react-dom": "18.3.1"
|
|
67
|
+
},
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
},
|
|
71
|
+
"externals": {
|
|
72
|
+
"react": "react",
|
|
73
|
+
"react-dom": "react-dom"
|
|
74
|
+
},
|
|
75
|
+
"gitHead": "5b4f77f985bd86ee31ee7338312627accf0cb85b"
|
|
76
|
+
}
|