-
Notifications
You must be signed in to change notification settings - Fork 1
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
eba6967
commit dd45617
Showing
9 changed files
with
142 additions
and
39 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createStore, applyMiddleware } from 'redux'; | ||
|
||
const getRibbitStore = (combinedReducers, ...middleware) => { | ||
let store; | ||
// check if rendering is happening on the client or the server. | ||
|
||
if (typeof window !== 'undefined') { | ||
// get state from window | ||
const preloadedState = window.RIBBIT_PRELOADED_STATE; | ||
delete window.RIBBIT_PRELOADED_STATE; | ||
|
||
// pass state into redux store | ||
store = createStore(combinedReducers, preloadedState, applyMiddleware(...middleware)); | ||
} else { | ||
store = createStore(combinedReducers, applyMiddleware(...middleware)); | ||
} | ||
return store; | ||
}; | ||
|
||
export default getRibbitStore; |
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,13 @@ | ||
function preloadPush() { | ||
fetch('http://localhost:5000/preload-push') | ||
.then(() => console.log('preload push')) | ||
.catch(err => console.log(err)); | ||
} | ||
|
||
function preloadPop() { | ||
fetch('http://localhost:5000/preload-pop') | ||
.then(() => console.log('preload pop')) | ||
.catch(err => console.log(err)); | ||
} | ||
|
||
module.exports = { preloadPush, preloadPop }; |
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,23 @@ | ||
const { preloadPush } = require('./preloadActions'); | ||
|
||
const ribbitRequestFactory = () => { | ||
const fetched = {}; | ||
|
||
function ribbitPreload(cb, component) { | ||
// only perform fetch if running on the server | ||
if (typeof window === 'undefined') { | ||
// dont let component continue to send requests after initial preload | ||
if (!fetched[component]) { | ||
fetched[component] = true; | ||
cb(); | ||
preloadPush(); | ||
} | ||
} | ||
} | ||
|
||
return ribbitPreload; | ||
}; | ||
|
||
const ribbitPreload = ribbitRequestFactory(); | ||
|
||
export default ribbitPreload; |
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,15 @@ | ||
const { preloadPop } = require('./preloadActions'); | ||
// re-render page with updated state on the server | ||
const ribbitStore = routes => { | ||
routes.forEach(route => { | ||
fetch(`http://localhost:5000${route}`) | ||
.then(response => response.json()) | ||
.then(() => { | ||
console.log('Re-render success!'); | ||
preloadPop(); | ||
}) | ||
.catch(err => console.log(err)); | ||
}); | ||
}; | ||
|
||
export default ribbitStore; |
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
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