This repository has been archived by the owner on Jan 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only hydrate if server-rendered; prevents client-server mistmatch in …
…dev mode CLOSES #50
- Loading branch information
Showing
1 changed file
with
13 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import React, { createElement as h } from 'react' | ||
import { hydrate } from 'react-dom' | ||
import ReactDOM from 'react-dom' | ||
import { BrowserRouter } from 'react-router-dom' | ||
import { loadComponents } from 'loadable-components' | ||
import { APP_ID, DATA_KEY } from '../shared/constants' | ||
|
||
function getSsrData () { | ||
return typeof window !== undefined ? window[DATA_KEY] : {} | ||
} | ||
export default function hydrate (App: React.ComponentType<any>) { | ||
const data = getSsrData() | ||
|
||
// only hydrate if server rendered (prevents client-server mistach in development) | ||
const mount = !data ? ReactDOM.hydrate : ReactDOM.render | ||
|
||
export default function hydrateApp (App: React.ComponentType<any>) { | ||
const props = getSsrData() | ||
return loadComponents().then(() => ( | ||
hydrate( | ||
h(BrowserRouter, {}, h(App, props)), | ||
mount( | ||
h(BrowserRouter, {}, h(App, data)), | ||
document.getElementById(APP_ID) | ||
) | ||
)) | ||
} | ||
|
||
|
||
function getSsrData () { | ||
return typeof window !== undefined ? window[DATA_KEY] : null | ||
} |