-
Notifications
You must be signed in to change notification settings - Fork 20
Add automatic SSR data hydration support #22
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
Open
abecks
wants to merge
14
commits into
cult-of-coders:master
Choose a base branch
from
abecks:ssr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e89dee
Add automatic SSR data hydration support
abecks 4ad37cd
Do not load static queries twice
abecks 72dbfa5
Add props to ReactiveQueryContainer
abecks 203a104
Fix component names in dev tools
abecks 5f00bef
Add prettier
abecks 5902caa
Add eslint
abecks 26efcf6
Prettier reformat
abecks 0aa5744
Add withUser
abecks 48df697
Add FastRender's cookie logic
abecks 81c34c6
Remove unused arg
abecks c0389b1
No need for promise here
abecks ba57e5e
Add constant for mime type
abecks ef65341
Add error handling
abecks 6e6fce8
Fix staticQuery on client
abecks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,29 @@ | ||
module.exports = { | ||
extends: [ | ||
'standard', | ||
'plugin:meteor/recommended', | ||
'plugin:react/recommended', | ||
'plugin:import/errors', | ||
'plugin:import/warnings', | ||
], | ||
plugins: ['react', 'meteor', 'import'], | ||
settings: { | ||
'import/resolver': { | ||
meteor: { | ||
extensions: ['.js', '.jsx'], | ||
}, | ||
}, | ||
}, | ||
rules: { | ||
// "import/no-duplicates": 0, | ||
// "react/display-name": 0, | ||
'comma-dangle': 0, | ||
'space-before-function-paren': 0, | ||
// "indent": [ | ||
// "error", | ||
// "tab" | ||
// ], | ||
semi: 0, | ||
// "meteor/no-session": 0 | ||
}, | ||
}; |
This file contains hidden or 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,5 @@ | ||
module.exports = { | ||
trailingComma: 'es5', | ||
singleQuote: true, | ||
tabWidth: 2, | ||
} |
This file contains hidden or 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,9 @@ | ||
{ | ||
"editor.tabSize": 2, | ||
"editor.formatOnSave": true, | ||
"eslint.enable": true, | ||
"eslint.provideLintTask": true, | ||
"eslint.autoFixOnSave": false, | ||
"javascript.validate.enable": false, | ||
"git.ignoreLimitWarning": true | ||
} |
This file contains hidden or 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 hidden or 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,4 +1,4 @@ | ||
export default { | ||
reactive: false, | ||
single: false, | ||
} | ||
reactive: false, | ||
single: false, | ||
}; |
This file contains hidden or 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,76 +1,80 @@ | ||
import React from 'react'; | ||
import {createContainer} from 'meteor/react-meteor-data'; | ||
import { createContainer } from 'meteor/react-meteor-data'; | ||
|
||
export default (query, component, options = {}) => { | ||
if (Meteor.isDevelopment) { | ||
console.warn('createQueryContainer() is deprecated, please use withQuery() instead') | ||
} | ||
if (Meteor.isDevelopment) { | ||
console.warn( | ||
'createQueryContainer() is deprecated, please use withQuery() instead' | ||
); | ||
} | ||
|
||
if (options.reactive) { | ||
return createContainer((props) => { | ||
if (props.params) { | ||
query.setParams(props.params); | ||
} | ||
if (options.reactive) { | ||
return createContainer(props => { | ||
if (props.params) { | ||
query.setParams(props.params); | ||
} | ||
|
||
const handler = query.subscribe(); | ||
const handler = query.subscribe(); | ||
|
||
return { | ||
query, | ||
loading: !handler.ready(), | ||
[options.dataProp]: options.single ? _.first(query.fetch()) : query.fetch(), | ||
...props | ||
} | ||
}, component); | ||
} | ||
return { | ||
query, | ||
loading: !handler.ready(), | ||
[options.dataProp]: options.single | ||
? _.first(query.fetch()) | ||
: query.fetch(), | ||
...props, | ||
}; | ||
}, component); | ||
} | ||
|
||
class MethodQueryComponent extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = { | ||
[options.dataProp]: undefined, | ||
error: undefined, | ||
loading: true | ||
} | ||
} | ||
class MethodQueryComponent extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = { | ||
[options.dataProp]: undefined, | ||
error: undefined, | ||
loading: true, | ||
}; | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
this._fetch(nextProps.params); | ||
} | ||
componentWillReceiveProps(nextProps) { | ||
this._fetch(nextProps.params); | ||
} | ||
|
||
componentDidMount() { | ||
this._fetch(this.props.params); | ||
} | ||
componentDidMount() { | ||
this._fetch(this.props.params); | ||
} | ||
|
||
_fetch(params) { | ||
if (params) { | ||
query.setParams(params); | ||
} | ||
_fetch(params) { | ||
if (params) { | ||
query.setParams(params); | ||
} | ||
|
||
query.fetch((error, data) => { | ||
const state = { | ||
error, | ||
[options.dataProp]: options.single ? _.first(data) : data, | ||
loading: false | ||
}; | ||
query.fetch((error, data) => { | ||
const state = { | ||
error, | ||
[options.dataProp]: options.single ? _.first(data) : data, | ||
loading: false, | ||
}; | ||
|
||
this.setState(state); | ||
}); | ||
} | ||
this.setState(state); | ||
}); | ||
} | ||
|
||
render() { | ||
const {state, props} = this; | ||
render() { | ||
const { state, props } = this; | ||
|
||
return React.createElement(component, { | ||
query, | ||
...state, | ||
...props | ||
}) | ||
} | ||
return React.createElement(component, { | ||
query, | ||
...state, | ||
...props, | ||
}); | ||
} | ||
} | ||
|
||
MethodQueryComponent.propTypes = { | ||
params: React.PropTypes.object | ||
}; | ||
MethodQueryComponent.propTypes = { | ||
params: React.PropTypes.object, | ||
}; | ||
|
||
return MethodQueryComponent; | ||
} | ||
return MethodQueryComponent; | ||
}; |
This file contains hidden or 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,34 @@ | ||
import { EJSON } from 'meteor/ejson'; | ||
import { generateQueryId, DATASTORE_MIME } from './utils.js'; | ||
|
||
export default { | ||
decodeData(data) { | ||
const decodedEjsonString = decodeURIComponent(data); | ||
if (!decodedEjsonString) return null; | ||
|
||
return EJSON.parse(decodedEjsonString); | ||
}, | ||
|
||
load() { | ||
// Retrieve the payload from the DOM | ||
const dom = document.querySelectorAll( | ||
`script[type="${DATASTORE_MIME}"]`, | ||
document | ||
); | ||
const dataString = dom && dom.length > 0 ? dom[0].innerHTML : ''; | ||
const data = this.decodeData(dataString) || {}; | ||
window.grapherQueryStore = data; | ||
|
||
return data; | ||
}, | ||
|
||
getQueryData(query) { | ||
const id = generateQueryId(query); | ||
const data = window.grapherQueryStore[id]; | ||
return data; | ||
}, | ||
|
||
destroy() { | ||
window.grapherQueryStore = null; | ||
}, | ||
}; |
This file contains hidden or 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,43 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { EJSON } from 'meteor/ejson'; | ||
import { generateQueryId, DATASTORE_MIME } from './utils.js'; | ||
export const SSRDataStoreContext = React.createContext(null); | ||
|
||
class DataStore { | ||
storage = {}; | ||
|
||
add(query, value) { | ||
const key = generateQueryId(query); | ||
this.storage[key] = value; | ||
} | ||
|
||
getData() { | ||
return this.storage; | ||
} | ||
} | ||
|
||
export default class SSRDataStore { | ||
constructor() { | ||
this.store = new DataStore(); | ||
} | ||
|
||
collectData(children) { | ||
return ( | ||
<SSRDataStoreContext.Provider value={this.store}> | ||
{children} | ||
</SSRDataStoreContext.Provider> | ||
); | ||
} | ||
|
||
encodeData(data) { | ||
data = EJSON.stringify(data); | ||
return encodeURIComponent(data); | ||
} | ||
|
||
getScriptTags() { | ||
const data = this.store.getData(); | ||
|
||
return `<script type="${DATASTORE_MIME}">${this.encodeData(data)}</script>`; | ||
} | ||
} |
This file contains hidden or 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,63 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Meteor } from 'meteor/meteor'; | ||
import { Accounts } from 'meteor/accounts-base'; | ||
import hoistNonReactStatic from 'hoist-non-react-statics'; | ||
import { withTracker } from 'meteor/react-meteor-data'; | ||
|
||
export const UserContext = React.createContext(null); | ||
|
||
class UserContextProvider extends React.Component { | ||
static propTypes = { | ||
user: PropTypes.object, | ||
children: PropTypes.node, | ||
}; | ||
|
||
render() { | ||
return ( | ||
<UserContext.Provider value={this.props.user}> | ||
{this.props.children} | ||
</UserContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
export const User = withTracker(props => { | ||
let user; | ||
if (Meteor.isServer) { | ||
if (props.token) { | ||
user = Meteor.users.findOne( | ||
{ | ||
'services.resume.loginTokens.hashedToken': Accounts._hashLoginToken( | ||
props.token | ||
), | ||
}, | ||
{ reactive: false } | ||
); | ||
} | ||
} else { | ||
user = Meteor.user(); | ||
} | ||
|
||
return { | ||
user, | ||
}; | ||
})(UserContextProvider); | ||
|
||
const withUser = function(Component) { | ||
const C = props => { | ||
return ( | ||
<UserContext.Consumer> | ||
{value => { | ||
return <Component {...props} user={value} />; | ||
}} | ||
</UserContext.Consumer> | ||
); | ||
}; | ||
|
||
C.displayName = `withUser(${Component.displayName || Component.name})`; | ||
|
||
hoistNonReactStatic(C, Component); | ||
return C; | ||
}; | ||
export default withUser; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
propper 4 space indentation please