@canonical/react-ssr 0.21.0 → 0.23.0
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/dist/esm/bin/serve-express.js +86 -0
- package/dist/esm/bin/serve-express.js.map +1 -0
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/index.js +3 -0
- package/dist/esm/lib/index.js.map +1 -0
- package/dist/esm/lib/renderer/Extractor.js +172 -0
- package/dist/esm/lib/renderer/Extractor.js.map +1 -0
- package/dist/esm/lib/renderer/JSXRenderer.js +283 -0
- package/dist/esm/lib/renderer/JSXRenderer.js.map +1 -0
- package/dist/esm/lib/renderer/SitemapRenderer.js +241 -0
- package/dist/esm/lib/renderer/SitemapRenderer.js.map +1 -0
- package/dist/esm/lib/renderer/TextRenderer.js +124 -0
- package/dist/esm/lib/renderer/TextRenderer.js.map +1 -0
- package/dist/esm/lib/renderer/constants.js.map +1 -0
- package/dist/esm/lib/renderer/index.js +6 -0
- package/dist/esm/lib/renderer/index.js.map +1 -0
- package/dist/esm/lib/renderer/types.js +10 -0
- package/dist/esm/lib/renderer/types.js.map +1 -0
- package/dist/esm/lib/server/index.js +3 -0
- package/dist/esm/lib/server/index.js.map +1 -0
- package/dist/esm/lib/server/serveStream.js +53 -0
- package/dist/esm/lib/server/serveStream.js.map +1 -0
- package/dist/esm/lib/server/serveString.js +49 -0
- package/dist/esm/lib/server/serveString.js.map +1 -0
- package/dist/types/bin/serve-express.d.ts +24 -0
- package/dist/types/bin/serve-express.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/lib/index.d.ts +3 -0
- package/dist/types/lib/index.d.ts.map +1 -0
- package/dist/types/lib/renderer/Extractor.d.ts +93 -0
- package/dist/types/lib/renderer/Extractor.d.ts.map +1 -0
- package/dist/types/lib/renderer/JSXRenderer.d.ts +163 -0
- package/dist/types/lib/renderer/JSXRenderer.d.ts.map +1 -0
- package/dist/types/lib/renderer/SitemapRenderer.d.ts +153 -0
- package/dist/types/lib/renderer/SitemapRenderer.d.ts.map +1 -0
- package/dist/types/lib/renderer/TextRenderer.d.ts +83 -0
- package/dist/types/lib/renderer/TextRenderer.d.ts.map +1 -0
- package/dist/types/lib/renderer/constants.d.ts.map +1 -0
- package/dist/types/lib/renderer/index.d.ts +7 -0
- package/dist/types/lib/renderer/index.d.ts.map +1 -0
- package/dist/types/lib/renderer/types.d.ts +161 -0
- package/dist/types/lib/renderer/types.d.ts.map +1 -0
- package/dist/types/lib/server/index.d.ts +3 -0
- package/dist/types/lib/server/index.d.ts.map +1 -0
- package/dist/types/lib/server/serveStream.d.ts +41 -0
- package/dist/types/lib/server/serveStream.d.ts.map +1 -0
- package/dist/types/lib/server/serveString.d.ts +37 -0
- package/dist/types/lib/server/serveString.d.ts.map +1 -0
- package/package.json +32 -17
- package/dist/esm/renderer/Extractor.js +0 -127
- package/dist/esm/renderer/Extractor.js.map +0 -1
- package/dist/esm/renderer/JSXRenderer.js +0 -168
- package/dist/esm/renderer/JSXRenderer.js.map +0 -1
- package/dist/esm/renderer/constants.js.map +0 -1
- package/dist/esm/renderer/index.js +0 -4
- package/dist/esm/renderer/index.js.map +0 -1
- package/dist/esm/renderer/types.js +0 -2
- package/dist/esm/renderer/types.js.map +0 -1
- package/dist/esm/server/index.js +0 -2
- package/dist/esm/server/index.js.map +0 -1
- package/dist/esm/server/serve-express.js +0 -58
- package/dist/esm/server/serve-express.js.map +0 -1
- package/dist/esm/server/serve.js +0 -41
- package/dist/esm/server/serve.js.map +0 -1
- package/dist/types/renderer/Extractor.d.ts +0 -68
- package/dist/types/renderer/Extractor.d.ts.map +0 -1
- package/dist/types/renderer/JSXRenderer.d.ts +0 -71
- package/dist/types/renderer/JSXRenderer.d.ts.map +0 -1
- package/dist/types/renderer/constants.d.ts.map +0 -1
- package/dist/types/renderer/index.d.ts +0 -5
- package/dist/types/renderer/index.d.ts.map +0 -1
- package/dist/types/renderer/types.d.ts +0 -35
- package/dist/types/renderer/types.d.ts.map +0 -1
- package/dist/types/server/index.d.ts +0 -2
- package/dist/types/server/index.d.ts.map +0 -1
- package/dist/types/server/serve-express.d.ts +0 -3
- package/dist/types/server/serve-express.d.ts.map +0 -1
- package/dist/types/server/serve.d.ts +0 -30
- package/dist/types/server/serve.d.ts.map +0 -1
- /package/dist/esm/{renderer → lib/renderer}/constants.js +0 -0
- /package/dist/types/{renderer → lib/renderer}/constants.d.ts +0 -0
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"JSXRenderer.js","sourceRoot":"","sources":["../../../src/renderer/JSXRenderer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EAEL,sBAAsB,EACtB,cAAc,GACf,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,SAAS,MAAM,gBAAgB,CAAC;AASvC;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IAWT;IACA;IACA;IATX,SAAS,CAAwB;IAE3C;;;OAGG;IACH,YACqB,SAAqB,EACrB,cAA2B,EAAiB,EAC5C,UAA2B,EAAE;QAF7B,cAAS,GAAT,SAAS,CAAY;QACrB,gBAAW,GAAX,WAAW,CAAiC;QAC5C,YAAO,GAAP,OAAO,CAAsB;QAEhD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU;YACtC,CAAC,CAAC,IAAI,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC;YACxC,CAAC,CAAC,SAAS,CAAC;IAChB,CAAC;IAED;;;OAGG;IACI,SAAS;QACd,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,IAAI,IAAI,CAAC;IAC5C,CAAC;IAED;;OAEG;IACO,iBAAiB;QACzB,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,SAA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G,EAAE,CAAC;IACZ,CAAC,CAAC;CACH"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/renderer/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/renderer/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/renderer/types.ts"],"names":[],"mappings":""}
|
package/dist/esm/server/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { parseArgs } from "node:util";
|
|
4
|
-
import express from "express";
|
|
5
|
-
import { serveStream } from "./serve.js";
|
|
6
|
-
const { values, positionals } = parseArgs({
|
|
7
|
-
args: process.argv.slice(2),
|
|
8
|
-
options: {
|
|
9
|
-
port: {
|
|
10
|
-
type: "string",
|
|
11
|
-
alias: "p",
|
|
12
|
-
default: "5173",
|
|
13
|
-
},
|
|
14
|
-
staticFilepath: {
|
|
15
|
-
type: "string",
|
|
16
|
-
alias: "s",
|
|
17
|
-
default: "dist/client/assets",
|
|
18
|
-
},
|
|
19
|
-
staticRoute: {
|
|
20
|
-
type: "string",
|
|
21
|
-
alias: "r",
|
|
22
|
-
default: "assets",
|
|
23
|
-
},
|
|
24
|
-
streaming: {
|
|
25
|
-
type: "boolean",
|
|
26
|
-
default: false,
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
strict: true,
|
|
30
|
-
allowPositionals: true,
|
|
31
|
-
});
|
|
32
|
-
const port = values.port || 5173;
|
|
33
|
-
const cwd = process.cwd();
|
|
34
|
-
const rendererFilePath = path.join(cwd, positionals[0]);
|
|
35
|
-
const staticDir = path.join(cwd, values.staticFilepath || "assets");
|
|
36
|
-
const staticRoute = values.staticRoute || "assets";
|
|
37
|
-
if (!rendererFilePath) {
|
|
38
|
-
console.error("Usage: node server.js <renderer-path>");
|
|
39
|
-
process.exit(1);
|
|
40
|
-
}
|
|
41
|
-
const handler = await import(rendererFilePath).then((module) => {
|
|
42
|
-
if (values.streaming) {
|
|
43
|
-
return module.default.renderToStream;
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
return module.default.renderToString;
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
if (typeof handler !== "function") {
|
|
50
|
-
throw new Error("Renderer file must default-export a renderer function.");
|
|
51
|
-
}
|
|
52
|
-
const app = express();
|
|
53
|
-
app.use(`/${staticRoute}`, express.static(staticDir));
|
|
54
|
-
app.use(serveStream(handler));
|
|
55
|
-
app.listen(port, () => {
|
|
56
|
-
console.log(`Server started on http://localhost:${port}/`);
|
|
57
|
-
});
|
|
58
|
-
//# sourceMappingURL=serve-express.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serve-express.js","sourceRoot":"","sources":["../../../src/server/serve-express.ts"],"names":[],"mappings":";AAEA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,SAAS,CAAC;IACxC,IAAI,EAAE,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C;QACN,OAAO,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC;IACvC,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;IAClC,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;AAC5E,CAAC;AAED,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;AAEtB,GAAG,CAAC,GAAG,CAAC,IAAI,WAAW,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;AACtD,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC;AAE9B,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;IACpB,OAAO,CAAC,GAAG,CAAC,sCAAsC,IAAI,GAAG,CAAC,CAAC;AAC7D,CAAC,CAAC,CAAC"}
|
package/dist/esm/server/serve.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Execute a request handler that renders a React component to a stream, then streams it back to the client.
|
|
3
|
-
* This function should be used as a request controller in an HTTP server.
|
|
4
|
-
* @param handler A request handler that renders a React component to a stream.
|
|
5
|
-
* @throws Error if the handler throws an error during rendering.
|
|
6
|
-
* @returns The stream generated by the handler
|
|
7
|
-
* @example ```ts
|
|
8
|
-
* import express from "express";
|
|
9
|
-
* import { serveStream } from "@canonical/react-ssr/server";
|
|
10
|
-
* import { JSXRenderer } from "@canonical/react-ssr/renderer";
|
|
11
|
-
* // htmlString is created by some build process that bundles the client code
|
|
12
|
-
* import htmlString from "../../dist/client/index.html?raw";
|
|
13
|
-
* import EntryServer from "./entry-server.js";
|
|
14
|
-
*
|
|
15
|
-
* // `EntryServer` is an instance of `@canonical/react-ssr/renderer/ServerEntrypoint`
|
|
16
|
-
* const Renderer = new JSXRenderer(EntryServer, {
|
|
17
|
-
* htmlString,
|
|
18
|
-
* });
|
|
19
|
-
*
|
|
20
|
-
* const ssrHandler = Renderer.render;
|
|
21
|
-
*
|
|
22
|
-
* const app = express();
|
|
23
|
-
*
|
|
24
|
-
* app.use("/(assets|public)", express.static("dist/client/assets"));
|
|
25
|
-
* app.use(serveStream(ssrHandler));
|
|
26
|
-
*/
|
|
27
|
-
export function serveStream(handler) {
|
|
28
|
-
return (req, res) => {
|
|
29
|
-
try {
|
|
30
|
-
res.setHeader("Content-Type", "text/html");
|
|
31
|
-
res.setHeader("Transfer-Encoding", "chunked");
|
|
32
|
-
handler(req, res);
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
console.error("Error during rendering:", error);
|
|
36
|
-
res.statusCode = 500;
|
|
37
|
-
res.end("Internal server error");
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=serve.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serve.js","sourceRoot":"","sources":["../../../src/server/serve.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,WAAW,CAAC,OAAsB;IAChD,OAAO,CAAC,GAAoB,EAAE,GAAmB,EAAE,EAAE;QACnD,IAAI,CAAC;YACH,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;YAC3C,GAAG,CAAC,SAAS,CAAC,mBAAmB,EAAE,SAAS,CAAC,CAAC;YAC9C,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACpB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAC;YAChD,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;YACrB,GAAG,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;QACnC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { type Document, type Element } from "domhandler";
|
|
2
|
-
import React from "react";
|
|
3
|
-
/**
|
|
4
|
-
* Parses an HTML string to extract and convert the <head> tags to React.createElement calls.
|
|
5
|
-
* The tags extracted are:
|
|
6
|
-
* - title
|
|
7
|
-
* - style
|
|
8
|
-
* - meta
|
|
9
|
-
* - link
|
|
10
|
-
* - script
|
|
11
|
-
* - base
|
|
12
|
-
*/
|
|
13
|
-
declare class Extractor {
|
|
14
|
-
/**
|
|
15
|
-
* A document object representing the DOM of a page.
|
|
16
|
-
*/
|
|
17
|
-
protected readonly document: Document;
|
|
18
|
-
/**
|
|
19
|
-
* Creates an Extractor object for a given HTML string.
|
|
20
|
-
*/
|
|
21
|
-
constructor(html: string);
|
|
22
|
-
/**
|
|
23
|
-
* Searches elements with the specified tag in the document.
|
|
24
|
-
*
|
|
25
|
-
* @remark The method uses the parsed {@link Extractor.document | document} to navigate the
|
|
26
|
-
* whole DOM (usinig a stack) and checks for the elements with the tag name that matches
|
|
27
|
-
* the given parameter.
|
|
28
|
-
*/
|
|
29
|
-
protected getElementsByTagName(tagName: string): Element[];
|
|
30
|
-
/**
|
|
31
|
-
* Converts HTML keys to React keys.
|
|
32
|
-
*
|
|
33
|
-
* @remark There are some HTML attributes that don't map exactly to React with the same name.
|
|
34
|
-
* For example, class -> className.
|
|
35
|
-
*/
|
|
36
|
-
protected convertKeyToReactKey(key: string): string;
|
|
37
|
-
/**
|
|
38
|
-
* Converts a parsed {@link domhandler#Element | DOM Element} into a {@link react#React.ReactElement | ReactElement}.
|
|
39
|
-
*
|
|
40
|
-
* @remark The method takes into account the attributes of the parsed {@link domhandler#Element | Element}
|
|
41
|
-
* and passes them as props when creating the {@link react#React.ReactElement | ReactElement}.
|
|
42
|
-
* It only handles children of type "text".
|
|
43
|
-
*/
|
|
44
|
-
protected convertToReactElement(element: Element, index: number): React.ReactElement;
|
|
45
|
-
/**
|
|
46
|
-
* Finds all <link> elements in the {@link Extractor.document | document} and converts them
|
|
47
|
-
* into {@link react#React.ReactElement | ReactElements}.
|
|
48
|
-
*
|
|
49
|
-
* @remark The list of elements returned will be in order of appearance in the DOM.
|
|
50
|
-
*/
|
|
51
|
-
getLinkElements(): React.ReactElement[];
|
|
52
|
-
/**
|
|
53
|
-
* Finds all <script> elements in the {@link Extractor.document | document} and converts them
|
|
54
|
-
* into {@link react#React.ReactElement | ReactElements}.
|
|
55
|
-
*
|
|
56
|
-
* @remark The list of elements returned will be in order of appearance in the DOM.
|
|
57
|
-
*/
|
|
58
|
-
getScriptElements(): React.ReactElement[];
|
|
59
|
-
/**
|
|
60
|
-
* Finds all the <head> elements which are not "script" or "link" in the {@link Extractor.document | document}
|
|
61
|
-
* and converts them into {@link react#React.ReactElement | ReactElements}.
|
|
62
|
-
*
|
|
63
|
-
* @remark The list of elements returned will be in order of appearance in the DOM.
|
|
64
|
-
*/
|
|
65
|
-
getOtherHeadElements(): React.ReactElement[];
|
|
66
|
-
}
|
|
67
|
-
export default Extractor;
|
|
68
|
-
//# sourceMappingURL=Extractor.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Extractor.d.ts","sourceRoot":"","sources":["../../../src/renderer/Extractor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,OAAO,EAAoB,MAAM,YAAY,CAAC;AAE3E,OAAO,KAAK,MAAM,OAAO,CAAC;AAS1B;;;;;;;;;GASG;AACH,cAAM,SAAS;IACb;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAEtC;;OAEG;gBACS,IAAI,EAAE,MAAM;IAIxB;;;;;;OAMG;IACH,SAAS,CAAC,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,EAAE;IAwB1D;;;;;OAKG;IACH,SAAS,CAAC,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAKnD;;;;;;OAMG;IACH,SAAS,CAAC,qBAAqB,CAC7B,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,MAAM,GACZ,KAAK,CAAC,YAAY;IAiBrB;;;;;OAKG;IACI,eAAe,IAAI,KAAK,CAAC,YAAY,EAAE;IAO9C;;;;;OAKG;IACI,iBAAiB,IAAI,KAAK,CAAC,YAAY,EAAE;IAOhD;;;;;OAKG;IACI,oBAAoB,IAAI,KAAK,CAAC,YAAY,EAAE;CAQpD;AAED,eAAe,SAAS,CAAC"}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { type RenderToPipeableStreamOptions } from "react-dom/server";
|
|
2
|
-
import Extractor from "./Extractor.js";
|
|
3
|
-
import type { RendererOptions, RenderHandler, ServerEntrypoint, ServerEntrypointProps } from "./types.js";
|
|
4
|
-
/**
|
|
5
|
-
* This class is responsible for rendering a React JSX component and sending it as response to a client.
|
|
6
|
-
* It offers 2 ways of doing it:
|
|
7
|
-
* - As string
|
|
8
|
-
* - As stream
|
|
9
|
-
* Each way has its advantages and inconveniences. You can read more about them in the package README.
|
|
10
|
-
*/
|
|
11
|
-
export default class JSXRenderer<TComponent extends ServerEntrypoint<InitialData>, InitialData extends Record<string, unknown>> {
|
|
12
|
-
protected readonly Component: TComponent;
|
|
13
|
-
protected readonly initialData: InitialData;
|
|
14
|
-
protected readonly options: RendererOptions;
|
|
15
|
-
protected extractor: Extractor | undefined;
|
|
16
|
-
/**
|
|
17
|
-
* Creates a renderer instance which can be used to write Server Side Rendered HTML
|
|
18
|
-
* into a {@link node:http#ServerResponse | ServerResponse}.
|
|
19
|
-
*/
|
|
20
|
-
constructor(Component: TComponent, initialData?: InitialData, options?: RendererOptions);
|
|
21
|
-
/**
|
|
22
|
-
* Gets the locale to be used for the rendered page.
|
|
23
|
-
* Default if there was no locale passed as option is "en".
|
|
24
|
-
*/
|
|
25
|
-
getLocale(): string;
|
|
26
|
-
/**
|
|
27
|
-
* Gets the props needed to render the component.
|
|
28
|
-
*/
|
|
29
|
-
protected getComponentProps(): ServerEntrypointProps<InitialData>;
|
|
30
|
-
/**
|
|
31
|
-
* Gets a list of all the "src" attributes of the given scripts that match the passed type.
|
|
32
|
-
*/
|
|
33
|
-
protected getScriptSourcesByType(scripts: React.ReactElement[], type: "module" | "classic"): string[];
|
|
34
|
-
/**
|
|
35
|
-
* Adds some properties to the options that are passed to {@link react-dom#renderToPipeableStream | renderToPipeableStream}.
|
|
36
|
-
*
|
|
37
|
-
* @remark The options that are added are:
|
|
38
|
-
* - bootstrapScriptContent: includes the initial data passed as prop to the component in a <script> so that it
|
|
39
|
-
* is available when rendering the page in the browser (to avoid hydration mismatches).
|
|
40
|
-
* - bootstrapScripts: classic scripts which react strips out of the page. The only way to add them is to include them
|
|
41
|
-
* in this property.
|
|
42
|
-
* - bootstrapModules: module scripts which react also strips out of the page and need to be added like this.
|
|
43
|
-
*/
|
|
44
|
-
protected enrichRendererOptions(props: ServerEntrypointProps<InitialData>): RenderToPipeableStreamOptions;
|
|
45
|
-
/**
|
|
46
|
-
* This function is responsible for rendering a React component and sending it to the client through
|
|
47
|
-
* a pipeable stream.
|
|
48
|
-
*
|
|
49
|
-
* @remark See the README to understand the difference between rendering options.
|
|
50
|
-
*
|
|
51
|
-
* The streaming might improve the time taken for the page to be rendered and interactive
|
|
52
|
-
* (at least in part), using React's Suspense/lazy API and pipeable streams.
|
|
53
|
-
*
|
|
54
|
-
* CAUTION: The resulting HTML rendered this way is not cacheable.
|
|
55
|
-
*/
|
|
56
|
-
renderToStream: RenderHandler;
|
|
57
|
-
/**
|
|
58
|
-
* Renders this renderer's JSX component as a string and writes it to the given
|
|
59
|
-
* {@link node:http#ServerResponse | ServerResponse}.
|
|
60
|
-
*
|
|
61
|
-
* @remark See the README to understand the difference between rendering options.
|
|
62
|
-
*
|
|
63
|
-
* Rendering to string means all <Suspense> components are loaded synchronously and the response
|
|
64
|
-
* won't be sent to the client until all components have finished loading data and processing.
|
|
65
|
-
*
|
|
66
|
-
* renderToString is useful in Vite Dev mode, as the HMR doesn't work well with Suspense
|
|
67
|
-
* and the Pipeable Stream rendering. Also if the resulting document needs to be cached.
|
|
68
|
-
*/
|
|
69
|
-
renderToString: RenderHandler;
|
|
70
|
-
}
|
|
71
|
-
//# sourceMappingURL=JSXRenderer.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"JSXRenderer.d.ts","sourceRoot":"","sources":["../../../src/renderer/JSXRenderer.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,6BAA6B,EAGnC,MAAM,kBAAkB,CAAC;AAE1B,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EAEb,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,YAAY,CAAC;AAEpB;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW,CAC9B,UAAU,SAAS,gBAAgB,CAAC,WAAW,CAAC,EAChD,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IASzC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,UAAU;IACxC,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,WAAW;IAC3C,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,eAAe;IAT7C,SAAS,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,CAAC;IAE3C;;;OAGG;gBAEkB,SAAS,EAAE,UAAU,EACrB,WAAW,GAAE,WAA+B,EAC5C,OAAO,GAAE,eAAoB;IAOlD;;;OAGG;IACI,SAAS,IAAI,MAAM;IAI1B;;OAEG;IACH,SAAS,CAAC,iBAAiB,IAAI,qBAAqB,CAAC,WAAW,CAAC;IAUjE;;OAEG;IACH,SAAS,CAAC,sBAAsB,CAC9B,OAAO,EAAE,KAAK,CAAC,YAAY,EAAE,EAC7B,IAAI,EAAE,QAAQ,GAAG,SAAS,GACzB,MAAM,EAAE;IAsBX;;;;;;;;;OASG;IACH,SAAS,CAAC,qBAAqB,CAC7B,KAAK,EAAE,qBAAqB,CAAC,WAAW,CAAC,GACxC,6BAA6B;IA6BhC;;;;;;;;;;OAUG;IACH,cAAc,EAAE,aAAa,CAqD3B;IAEF;;;;;;;;;;;OAWG;IACH,cAAc,EAAE,aAAa,CAW3B;CACH"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/renderer/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,qBAAqB,CAAC"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export * from "./constants.js";
|
|
2
|
-
export { default as Extractor } from "./Extractor.js";
|
|
3
|
-
export { default as JSXRenderer } from "./JSXRenderer.js";
|
|
4
|
-
export type { RendererOptions, RenderHandler, RenderResult, ServerEntrypoint, ServerEntrypointProps, } from "./types.js";
|
|
5
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/renderer/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC1D,YAAY,EACV,eAAe,EACf,aAAa,EACb,YAAY,EACZ,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,YAAY,CAAC"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import type * as React from "react";
|
|
3
|
-
import type { PipeableStream, RenderToPipeableStreamOptions } from "react-dom/server";
|
|
4
|
-
export interface RendererOptions {
|
|
5
|
-
defaultLocale?: string;
|
|
6
|
-
/** An HTML string to extract the head tags from */
|
|
7
|
-
htmlString?: string;
|
|
8
|
-
/**
|
|
9
|
-
* Options to pass to `react-dom/server.renderToPipeableStream`.
|
|
10
|
-
* See https://react.dev/reference/react-dom/server/renderToPipeableStream#parameters.
|
|
11
|
-
*/
|
|
12
|
-
renderToPipeableStreamOptions?: RenderToPipeableStreamOptions;
|
|
13
|
-
}
|
|
14
|
-
/** The props that the server entrypoint component will receive */
|
|
15
|
-
export interface ServerEntrypointProps<InitialData extends Record<string, unknown>> {
|
|
16
|
-
/** The language of the page. This is typically read from the request headers. */
|
|
17
|
-
lang?: string;
|
|
18
|
-
/** The script tags to include in the page */
|
|
19
|
-
scriptElements?: React.ReactElement[];
|
|
20
|
-
/** The link tags to include in the page */
|
|
21
|
-
linkElements?: React.ReactElement[];
|
|
22
|
-
/** Other head elements: title, base, style & meta */
|
|
23
|
-
otherHeadElements?: React.ReactElement[];
|
|
24
|
-
/**
|
|
25
|
-
* Initial data used in the server to render the React application, which needs to be
|
|
26
|
-
* embedded in the resulting HTML so that the hydration in the client matches that of the server.
|
|
27
|
-
*/
|
|
28
|
-
initialData?: InitialData;
|
|
29
|
-
}
|
|
30
|
-
export type ServerEntrypoint<InitialData extends Record<string, unknown>> = React.ComponentType<ServerEntrypointProps<InitialData>>;
|
|
31
|
-
/** The result of rendering a React component */
|
|
32
|
-
export type RenderResult = PipeableStream;
|
|
33
|
-
/** A function that renders a React component */
|
|
34
|
-
export type RenderHandler = (req: IncomingMessage, res: ServerResponse) => void;
|
|
35
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/renderer/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AACpC,OAAO,KAAK,EACV,cAAc,EACd,6BAA6B,EAC9B,MAAM,kBAAkB,CAAC;AAE1B,MAAM,WAAW,eAAe;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,mDAAmD;IACnD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,6BAA6B,CAAC,EAAE,6BAA6B,CAAC;CAC/D;AAED,kEAAkE;AAClE,MAAM,WAAW,qBAAqB,CACpC,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAE3C,iFAAiF;IACjF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,6CAA6C;IAC7C,cAAc,CAAC,EAAE,KAAK,CAAC,YAAY,EAAE,CAAC;IACtC,2CAA2C;IAC3C,YAAY,CAAC,EAAE,KAAK,CAAC,YAAY,EAAE,CAAC;IACpC,qDAAqD;IACrD,iBAAiB,CAAC,EAAE,KAAK,CAAC,YAAY,EAAE,CAAC;IACzC;;;OAGG;IACH,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,MAAM,MAAM,gBAAgB,CAAC,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IACtE,KAAK,CAAC,aAAa,CAAC,qBAAqB,CAAC,WAAW,CAAC,CAAC,CAAC;AAI1D,gDAAgD;AAChD,MAAM,MAAM,YAAY,GAAG,cAAc,CAAC;AAC1C,gDAAgD;AAChD,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,KAAK,IAAI,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serve-express.d.ts","sourceRoot":"","sources":["../../../src/server/serve-express.ts"],"names":[],"mappings":""}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import type { RenderHandler } from "../renderer/index.js";
|
|
3
|
-
/**
|
|
4
|
-
* Execute a request handler that renders a React component to a stream, then streams it back to the client.
|
|
5
|
-
* This function should be used as a request controller in an HTTP server.
|
|
6
|
-
* @param handler A request handler that renders a React component to a stream.
|
|
7
|
-
* @throws Error if the handler throws an error during rendering.
|
|
8
|
-
* @returns The stream generated by the handler
|
|
9
|
-
* @example ```ts
|
|
10
|
-
* import express from "express";
|
|
11
|
-
* import { serveStream } from "@canonical/react-ssr/server";
|
|
12
|
-
* import { JSXRenderer } from "@canonical/react-ssr/renderer";
|
|
13
|
-
* // htmlString is created by some build process that bundles the client code
|
|
14
|
-
* import htmlString from "../../dist/client/index.html?raw";
|
|
15
|
-
* import EntryServer from "./entry-server.js";
|
|
16
|
-
*
|
|
17
|
-
* // `EntryServer` is an instance of `@canonical/react-ssr/renderer/ServerEntrypoint`
|
|
18
|
-
* const Renderer = new JSXRenderer(EntryServer, {
|
|
19
|
-
* htmlString,
|
|
20
|
-
* });
|
|
21
|
-
*
|
|
22
|
-
* const ssrHandler = Renderer.render;
|
|
23
|
-
*
|
|
24
|
-
* const app = express();
|
|
25
|
-
*
|
|
26
|
-
* app.use("/(assets|public)", express.static("dist/client/assets"));
|
|
27
|
-
* app.use(serveStream(ssrHandler));
|
|
28
|
-
*/
|
|
29
|
-
export declare function serveStream(handler: RenderHandler): (req: IncomingMessage, res: ServerResponse) => void;
|
|
30
|
-
//# sourceMappingURL=serve.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../../../src/server/serve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,IACxC,KAAK,eAAe,EAAE,KAAK,cAAc,UAWlD"}
|
|
File without changes
|
|
File without changes
|