Skip to content

Commit

Permalink
[minor][feat] add custom renderer support for SSR. \n (#1865)
Browse files Browse the repository at this point in the history
This is to support use cases like css in js e.g. Rendering Styled Components on server
  • Loading branch information
ashuverma authored Dec 1, 2021
1 parent fe6bf39 commit 31dcc0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/subapp-react/lib/framework-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class FrameworkLib {
}

renderTo(element, options) {
const { subAppServer } = this.ref;

if (typeof subAppServer.renderer === "function") {
return subAppServer.renderer(element, options);
}

if (options.useStream) {
assert(!options.suspenseSsr, "useStream and suspense SSR together are not supported");
if (options.hydrateServerData) {
Expand Down
27 changes: 26 additions & 1 deletion packages/subapp-react/test/spec/ssr-framework.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const url = require("url");
const React = require("react"); // eslint-disable-line
const ReactDOMServer = require("react-dom/server");
const lib = require("../../lib");
const { withRouter } = require("react-router");
const { Route, Switch } = require("react-router-dom"); // eslint-disable-line
Expand Down Expand Up @@ -189,7 +190,6 @@ describe("SSR React framework", function () {
expect(res).contains("Hello foo bar");
});


it("should render Component from subapp with initial props from server's prepare while using attachInitialState", async () => {
const framework = new lib.FrameworkLib({
subApp: {
Expand Down Expand Up @@ -422,4 +422,29 @@ describe("SSR React framework", function () {
const res = await framework.handleSSR();
expect(res).contains("Hello test path<");
});

it("should render subapp with custom renderer e.g. css in js solution", async () => {
const Component = () => {
return <div>Hello test path</div>;
};

const framework = new lib.FrameworkLib({
subApp: {
Component
},
subAppServer: {
renderer: (element, options) => {
return "<h1>Rendered via Custom renderer</h1>";
}
},
options: { serverSideRendering: true },
context: {
user: {
request: { url: url.parse("http://localhost/test") }
}
}
});
const res = await framework.handleSSR();
expect(res).contains("Rendered via Custom renderer");
});
});

0 comments on commit 31dcc0d

Please sign in to comment.