Skip to content

Commit

Permalink
Fix deployment
Browse files Browse the repository at this point in the history
caused by next-redux-wrapper
  • Loading branch information
HaNdTriX committed Feb 25, 2017
1 parent df32da9 commit 02a8bf6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -15,7 +15,6 @@
"isomorphic-fetch": "^2.2.1",
"lusca": "^1.4.1",
"next": "^2.0.0-beta.23",
"next-redux-wrapper": "^1.0.0",
"node-sass": "^4.5.0",
"nodemailer": "^2.7.0",
"now-logs": "0.0.7",
Expand Down
4 changes: 2 additions & 2 deletions pages/clock.js
Expand Up @@ -6,10 +6,10 @@
* - pages/clock.js
* - components/clock.js
* - components/clock-store.js
* - next-redux-wrapper
* - redux.js
*/
import React from 'react'
import withRedux from 'next-redux-wrapper'
import withRedux from '../redux'
import { initStore, startClock } from '../components/clock-store'
import Clock from '../components/clock'

Expand Down
102 changes: 102 additions & 0 deletions redux.js
@@ -0,0 +1,102 @@
// Forked from next-redux-wrapper
// since the deployment fails on original
// package
var React = require('react')
var ReactRedux = require('react-redux')

var connect = ReactRedux.connect
var Provider = ReactRedux.Provider

var memoizedStore
var _Promise
var _debug = false
var skipMerge = ['initialState', 'initialProps', 'isServer', 'store']

function initStore (makeStore, req, initialState) {
// Always make a new store if server
if (!!req && typeof window === 'undefined') {
if (!req._store) {
req._store = makeStore(initialState)
}
return req._store
}

// Memoize store if client
if (!memoizedStore) {
memoizedStore = makeStore(initialState)
}

return memoizedStore
}

module.exports = function (createStore) {
var connectArgs = [].slice.call(arguments).slice(1)

return function (Cmp) {
// Since provide should always be after connect we connect here
var ConnectedCmp = (connect.apply(null, connectArgs))(Cmp)

function WrappedCmp (props) {
props = props || {}

var initialState = props.initialState
var initialProps = props.initialProps
var hasStore = props.store && props.store.dispatch && props.store.getState
var store = hasStore
? props.store
: initStore(createStore, {}, initialState) // client case, no store but has initialState

if (_debug) console.log(Cmp.name, '- 4. WrappedCmp.render', (hasStore ? 'picked up existing one,' : 'created new store with'), 'initialState', initialState)

// Fix for _document
var mergedProps = {}
Object.keys(props).forEach(function (p) { if (!~skipMerge.indexOf(p)) mergedProps[p] = props[p] })
Object.keys(initialProps).forEach(function (p) { mergedProps[p] = initialProps[p] })

return React.createElement( // FIXME This will create double Provider for _document case
Provider,
{store: store},
React.createElement(ConnectedCmp, mergedProps)
)
}

WrappedCmp.getInitialProps = function (ctx) {
return new _Promise(function (res) {
ctx = ctx || {}

if (_debug) console.log(Cmp.name, '- 1. WrappedCmp.getInitialProps wrapper', (ctx.req && ctx.req._store ? 'takes the req store' : 'creates the store'))

ctx.isServer = !!ctx.req
ctx.store = initStore(createStore, ctx.req)

res(_Promise.all([
ctx.isServer,
ctx.store,
ctx.req,
Cmp.getInitialProps ? Cmp.getInitialProps.call(Cmp, ctx) : {}
]))
}).then(function (arr) {
if (_debug) console.log(Cmp.name, '- 3. WrappedCmp.getInitialProps has store state', arr[1].getState())

return {
isServer: arr[0],
store: arr[1],
initialState: arr[1].getState(),
initialProps: arr[3]
}
})
}

return WrappedCmp
}
}

module.exports.setPromise = function (Promise) {
_Promise = Promise
}

module.exports.setDebug = function (debug) {
_debug = debug
}

module.exports.setPromise(Promise)
4 changes: 0 additions & 4 deletions yarn.lock
Expand Up @@ -3256,10 +3256,6 @@ negotiator@0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"

next-redux-wrapper@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/next-redux-wrapper/-/next-redux-wrapper-1.0.0.tgz#8ddc993f6609ba85958d21afba8f5926339ce7b6"

next@^2.0.0-beta.23:
version "2.0.0-beta.26"
resolved "https://registry.yarnpkg.com/next/-/next-2.0.0-beta.26.tgz#e86302f6adc010709b8670f1f0748c595bacb45f"
Expand Down

0 comments on commit 02a8bf6

Please sign in to comment.