Skip to content

Commit

Permalink
Use hydrate iff ssr is enabled; also fix #245.
Browse files Browse the repository at this point in the history
  • Loading branch information
dicky authored and dicky committed May 25, 2020
1 parent a7debeb commit 44167e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions DevApp/src/dotnetify/react/dotnetify-react.router.ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function getSsrState(iVMId: string) {
}

export default function enableSsr() {
dotnetify["ssrEnabled"] = true;

// Override dotnetify.react.connect for server-side render.
const connect = dotnetify.react.connect;
dotnetify.react.connect = function(iVMId: string, iComponent: any, iOptions: IConnectOptions) {
Expand Down
12 changes: 8 additions & 4 deletions DevApp/src/dotnetify/react/dotnetify-react.vm-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export default class DotnetifyReactVMRouter extends DotnetifyVMRouter {
const vmId = this.vm ? this.vm.$vmId : "";

const mountViewFunc = () => {
if (typeof iComponent !== "function" && (typeof iComponent !== "object" || iComponent.name == null)) {
let reactElement = null;
try {
reactElement = React.createElement(iComponent, iReactProps);
} catch (e) {
console.error(`[${vmId}] failed to load view '${iComponent}' because it's not a valid React element.`);
reject();
return;
Expand All @@ -96,8 +99,9 @@ export default class DotnetifyReactVMRouter extends DotnetifyVMRouter {
this.unmountView(iTargetSelector);

try {
var reactElement = React.createElement(iComponent, iReactProps);
ReactDOM.hydrate(reactElement, document.querySelector(iTargetSelector));
const args = [reactElement, document.querySelector(iTargetSelector)];
if (vm.$dotnetify["ssrEnabled"] === true) ReactDOM.hydrate(...args);
else ReactDOM.render(...args);
} catch (e) {
console.error(e);
}
Expand All @@ -108,7 +112,7 @@ export default class DotnetifyReactVMRouter extends DotnetifyVMRouter {
if (iJsModuleUrl == null || this.vm.$dotnetify["ssr"] === true) mountViewFunc();
else {
// Load all javascripts first. Multiple files can be specified with comma delimiter.
var getScripts = iJsModuleUrl.split(",").map((i) => $.getScript(i));
var getScripts = iJsModuleUrl.split(",").map(i => $.getScript(i));
$.when.apply($, getScripts).done(mountViewFunc);
}
});
Expand Down

0 comments on commit 44167e7

Please sign in to comment.