Comments (16)
@msvalina that's outdated documentation, and is no longer supported because the passing of express request and response objects was unconventional for redux middleware and was incompatible with other middleware that were expecting dispatch and state parameters. if you are attempting to build an authentication system, i recommend using headers. the JWT example shows a way of doing that.
from universal-redux.
Could you at least pass req object in the renderer.js method html(config, tools.assets(), store, res._headers); for example like this:
html(config, tools.assets(), store, req, res._headers) or html(config, tools.assets(), store, req, res._headers, root)?
I need this to get the locale and userAgent from headers and dispatch these values to the store before initial render...
from universal-redux.
@bdefore yeah I understand the argument that it is unconventional for middleware to be passed anything other than dispatch and state, but that raises a question should this fetcher be middleware or by just a regular helper. What was rationale for putting in it middleware?
from universal-redux.
@msvalina the motivation was mostly historical, in departing from the RRUHE project. as is, it does get you some help in streamline your actions, but it's possible that redux middleware is not the best place to accomplish that.
from universal-redux.
@pristas-peter that's an interesting approach. on the one hand, the Html helper is getting a lot of parameters already, but that may be the most reasonable way to get access to request (and response) objects outside of redux middleware. could you file a new issue/PR?
from universal-redux.
I've got better idea, why not to dispatch action: {
type: INIT_SERVER,
meta: req
}
after creating the store...
all the reducers could do whatever they want with the request
from universal-redux.
@pristas-peter If I'm understanding you correctly, a base Html (or Head, Body) component of universal-redux could itself be connected to props populated from a store that reacted to INIT_SERVER
actions? Keep in mind that all of those components are currently serverside rendered.
from universal-redux.
Actually, there is one, "@@redux/INIT", but it's dispatched on both server and client, so need to check which cookie-setting side-effect to use (server-side or client-side).
from universal-redux.
@bdefore I didn't mean it to use it in html component, rather that universal redux would export action name (type) which you could import in your own reducer and process the request object on server, or something else on client => there should be dispatched two actions, one on server (INIT_SERVER) and one on client (INIT_CLIENT), or maybe better naming like STORE_DID_CREATE
from universal-redux.
@pristas-peter As far as we don't have side-effects inside reducers, putting the cookie-reading code, like any other storage-reading and storage-writing code, into a reducer looks wrong to me, its place now is in action creators' thunks.
Reading cookies
I think it would better be an action COOKIES_INITIALIZED
with the cookie values as its payload (but this way you'll have to list the cookie names somewhere in the configuration). This way would even allow you to test your reducers with different initial cookie values.
Writing cookies
This is an impure side-effect, so there is no place for it in the reducer. Some code capable of doing side-effects, like a React component, or a middleware, or an action creator thunk, should be introduced.
from universal-redux.
yes, you are right, however we are using redux-saga for side effects
from universal-redux.
@pristas-peter OK, then you'll have cookie-reading and cookie-setting side-effects in a saga, if I understand correctly? I haven't used redux-saga but I'm a bit familiar with its concept. Please show an example code how you see it with sagas.
from universal-redux.
+1 I am using radium
and material-ui
in a project and they both need userAgent for serverside rendering. It would be good to find a way to provide access to the userAgent
to setup configurations
from universal-redux.
any update on this. @bdefore your jwt example doesn't work with SSR auth. If you login, disable JS and refresh the code received from SSR is with logged out state which is invalid
In the current implementation of the starter there is no way to pass the cookies (nor headers nor anything else) from req
to superagent
and therefore can't load the user data on the server
from universal-redux.
Just coming back to this issue and catching up. If I gather what's being proposed, there's two options forward that I can see:
- Where
headers
are currently passedto thehtml
component (or custom root components if they are configured), change this to passreq
andres
and leave it up to the component to parse what they need (headers, userAgent, cookies, etc.) - Trigger an action of
INIT_SERVER
orINIT_CLIENT
that containsreq
andres
, post first render.
The latter approach looks cleaner to me, but implementation could be tricky: there is currently no concept of a redux lifecycle within UR. It just ferries along the redux middleware you'd like into the store creation and does not have access to the dispatcher.
How does everyone feel about the former approach?
from universal-redux.
I actually needed that functionality and went ahead and implemented it in a fork:
https://github.com/sageapp/universal-redux/commit/26a2bd9d0e22acdd907666a4c0ab565702984c26
So the former works out really well - there is probably a better way of doing it, but that gets the job done
from universal-redux.
Related Issues (20)
- onEnter can't redirect HOT 1
- How to access external server api (rails or laravel ... etc) from universal-redux?
- [webpack-isomorphic-tools] (waiting for the first Webpack build to finish)
- how the server side API requests handle the jwt token?
- Move to redux-connect HOT 1
- Help required?
- Question about waiting for Server Requests HOT 1
- Question about βAdd routes, reducer and rootClientComponent aliases so that client has access to themβ HOT 5
- Setting scroll behavior for the router
- SVG Loading Configuration
- Different CSS idents cause server render to be discarded HOT 1
- Using npm dependencies listed in Universal-redux on the project level HOT 3
- [webpack-isomorphic-tools] asset not found HOT 9
- Question: how to alter the JS loader HOT 4
- Passing Express request headers to Html component?
- @@router/UPDATE_LOCATION fired after first dispatched action HOT 3
- Server-side React render was discarded HOT 5
- Use redux-devtools-extension HOT 1
- integrate HappyPack
- should user other way to compile es6-7 with babel
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from universal-redux.