Skip to content

Commit

Permalink
Skip portal rendering during SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Frampton authored and Robert-Frampton committed Jan 5, 2018
1 parent 219f429 commit 6bd1caa
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/metal-component/src/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,11 @@ class Component extends EventEmitter {
portalElement = 'body';
}

if (isServerSide()) {
this.portalElement = true;
return;
}

portalElement = toElement(portalElement);

if (portalElement) {
Expand Down
5 changes: 5 additions & 0 deletions packages/metal-incremental-dom/src/render/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isDef,
isDefAndNotNull,
isFunction,
isServerSide,
isString,
object,
} from 'metal';
Expand Down Expand Up @@ -537,6 +538,10 @@ function renderSubComponent_(tagOrCtor, config, owner) {
config.key = parentData.config.key;
}

if (comp.portalElement && isServerSide()) {
return comp;
}

if (!comp.portalElement) {
comp.getRenderer().renderInsidePatch(comp);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/metal-isomorphic/test/fixtures/Portal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Component from 'metal-component';
import Soy from 'metal-soy';
import './ChildComponent.soy.js';

import templates from './Portal.soy.js';

class Portal extends Component {
}

Portal.STATE = {
message: {}
};

Soy.register(Portal, templates);

export default Portal;
10 changes: 10 additions & 0 deletions packages/metal-isomorphic/test/fixtures/Portal.soy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{namespace Portal}

/**
* @param? message
*/
{template .render}
<div class="portal">
Portal: {$message}
</div>
{/template}
16 changes: 16 additions & 0 deletions packages/metal-isomorphic/test/fixtures/PortalParent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Component from 'metal-component';
import Soy from 'metal-soy';
import './Portal.soy.js';

import templates from './PortalParent.soy.js';

class PortalParent extends Component {
}

PortalParent.STATE = {
message: {}
};

Soy.register(PortalParent, templates);

export default PortalParent;
15 changes: 15 additions & 0 deletions packages/metal-isomorphic/test/fixtures/PortalParent.soy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{namespace PortalParent}

/**
* @param? message
*/
{template .render}
<div class="portal-parent">
Parent: {$message}

{call Portal.render}
{param message: $message /}
{param portalElement: true /}
{/call}
</div>
{/template}
13 changes: 12 additions & 1 deletion packages/metal-isomorphic/test/isomorphic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import Component from 'metal-component';
import MyComponent from './fixtures/MyComponent';
import MyJSXComponent from './fixtures/MyJSXComponent';
import ParentComponent from './fixtures/ParentComponent';
import {assert} from 'chai';
import PortalParent from './fixtures/PortalParent';
import jsdomGlobal from 'jsdom-global';
import {assert} from 'chai';

describe('Isomorphic Rendering', () => {
it('should render soy component to string', () => {
Expand Down Expand Up @@ -36,6 +37,16 @@ describe('Isomorphic Rendering', () => {
assert.equal(htmlString, '<div><div>Child: Hello, World!</div></div>');
});

it('should skip subcomponents that have portalElement defined', () => {
assert.ok(!global.document);

const htmlString = Component.renderToString(PortalParent, {
message: 'Hello, World!'
});

assert.equal(htmlString, '<div class="portal-parent">Parent: Hello, World!</div>');
});

describe('JSDom', () => {
let cleanup;

Expand Down

0 comments on commit 6bd1caa

Please sign in to comment.