Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor][feat] add custom renderer support for SSR. #1865

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});