-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eeec795
commit dedead3
Showing
8 changed files
with
90 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as React from 'react'; | ||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'; | ||
import AsyncRenderer from '../components/AsyncRenderer'; | ||
import removeDuplicateModules from '../utils/removeDuplicateModules'; | ||
|
||
const renderPass = (context, element, staticMarkup = false) => { | ||
context.callback = () => { | ||
if (context.finishedLoadingModules && !context.statesRenderPass) { | ||
context.statesRenderPass = true; | ||
context.renderResult = renderPass(context, element, staticMarkup); | ||
if (context.fetchingStates <= 0 && context.modulesLoading <= 0) { | ||
context.resolve({ | ||
html: context.renderResult, | ||
state: context.fetchStateResults === undefined ? null : context.fetchStateResults, | ||
modules: context.modules === undefined ? null : removeDuplicateModules(context.modules) | ||
}); | ||
} | ||
} else if (context.finishedLoadingModules && context.statesRenderPass || !context.hasModules) { | ||
context.renderResult = renderPass(context, element, staticMarkup); | ||
if (context.fetchingStates <= 0 && context.modulesLoading <= 0) { | ||
context.resolve({ | ||
html: context.renderResult, | ||
state: context.fetchStateResults === undefined ? null : context.fetchStateResults, | ||
modules: context.modules === undefined ? null : removeDuplicateModules(context.modules) | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
let component = ( | ||
<AsyncRenderer context={context}> | ||
{element} | ||
</AsyncRenderer> | ||
) | ||
let result; | ||
if (staticMarkup) { | ||
result = renderToStaticMarkup(component); | ||
} else { | ||
result = renderToString(component ); | ||
} | ||
|
||
if (!context.hasModules && !context.hasStates) { | ||
context.resolve({ | ||
html: result, | ||
state: context.fetchStateResults === undefined ? null : context.fetchStateResults, | ||
modules: context.modules === undefined ? null : removeDuplicateModules(context.modules) | ||
}); | ||
} | ||
return result; | ||
}; | ||
|
||
export default renderPass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import renderPass from './renderPass'; | ||
|
||
export default (element) => { | ||
return new Promise((resolve, reject) => { | ||
const context = { | ||
resolve, reject, | ||
modulesLoading: 0, | ||
fetchingStates: 0 | ||
}; | ||
renderPass(context, element, true); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import renderToStaticMarkup from '../../../src/renderer/renderToStaticMarkup'; | ||
import App from './includes/App'; | ||
|
||
describe('renderToStaticMarkup', () => { | ||
it('should do render to static markup', done => { | ||
renderToStaticMarkup(<App/>) | ||
.then(({ html, state, modules }) => { | ||
expect(html).to.equal('<div><div>foobar</div></div>'); | ||
expect(state).to.deep.equal({ '1': { message: 'foobar' }}); | ||
expect(modules).to.have.lengthOf(1); | ||
done(); | ||
}) | ||
}); | ||
}); |