Comments (7)
import React from "react"
import { Provider } from "react-redux"
import { renderToNodeStream } from "react-dom/server"
import { StaticRouter } from "react-router-dom"
import { getLoadableState } from "loadable-components/server"
import Helmet from "react-helmet"
import I18n from "redux-i18n"
export default async (req, res) => {
const store = configureStore({
i18nState: {
lang: "en",
translations: {},
},
})
const context = {}
const appWithRouter = (
<Provider store={store}>
<I18n translations={{}} fallbackLang="en" useReducer>
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
</I18n>
</Provider>
)
if (context.url) {
res.redirect(context.url)
return
}
let loadableState = {}
store.runSaga(sagas).done.then(() => {
const headAssets = Helmet.rewind()
res.status(200).write(renderHeader(headAssets))
const initialState = store.getState()
const htmlSteam = renderToNodeStream(appWithRouter)
htmlSteam.pipe(res, { end: false })
htmlSteam.on("end", () => {
res.write(renderFooter(loadableState, initialState))
return res.send()
})
})
// Trigger sagas for component to run
// https://github.com/yelouafi/redux-saga/issues/255#issuecomment-210275959
loadableState = await getLoadableState(appWithRouter)
// Dispatch a close event so sagas stop listening after they're resolved
store.close()
}
loadable-components
has allowed me to use renderToNodeStream
I got everything to work thanks to https://github.com/alexisjanvier/universal-react
and https://marmelab.com/blog/2017/10/17/code-splitting.html
Edit: Forget this. I noticed before going to prod that meta tags were inconsistent and had to change back to renderToString
from react-head.
Edit: Forget this. I noticed before going to prod that meta tags were inconsistent and had to change back to renderToString
Thanks for looking into this @shizpi -- they were inconsistent in what way?
from react-head.
Haven't considered that use case yet, but presumably instead of pushing to an Array we could use an Observable. Do you have any thoughts on this?
from react-head.
Sorry I'd no idea too @tizmagik. But observable is good idea though.
from react-head.
Actually hmm, now that I think about it, it probably doesn’t make sense for this library to actually do anything when rendering to node stream on the server since once the head is sent over the wire, it can’t change. The good news though is that on the client the head should be updated correctly with any head tags rendered deeper in the tree after minting.
Does that make sense? What do you think?
from react-head.
yes, I also think it's impossible, and the same issue is still open on the react-helmet side.
My plan is to integrate this lib into https://github.com/antonybudianto/cra-universal, but looks like I must prepare both renderToString and renderToNodeStream versions where only renderToString can support document head SSR
Also there's possible solution in nfl/react-helmet#296, but still no idea how to implement it
from react-head.
Well the good news is that this lib should work fine whether you renderToString
or renderToNodeStream
. The only issue is that the SSR head tags coming in from the server when rendering to node stream won't be immediately the right values, but will eventually be correct once the client mounts. In either case the code would look the same and you shouldn't need to do much conditional logic, if at all, to handle the different rendering modes.
That cra-universal
project looks really cool!! I'm glad you've considered this lib for it. Let me know if I can be of any assistance. Good luck!
from react-head.
Related Issues (20)
- Link canonical cascade HOT 1
- Expose HeadTag? HOT 2
- Question: Does it work without ssr for SEO, on fb and twitter ? HOT 1
- Children prop type missing in HeadProvider HOT 7
- Is this library still maintained? HOT 1
- Suggestion: use renderToStaticMarkup instead of renderToString HOT 4
- Some tags not collected HOT 4
- Invariant failed in HeadTags HOT 9
- Development server not working HOT 2
- does not support ie9? HOT 2
- Proposal: headTagsToElement(headTags) HOT 1
- Element.remove() not supported in IE HOT 3
- Document usage with SSR streaming HOT 3
- TypeScript support HOT 3
- react-head supports meta update on lazy load HOT 3
- itemProp support on <Meta /> HOT 1
- canonical link is repeated HOT 1
- Title component doesn't override default <title> tag provided via HTML template HOT 10
- Entities in computed attribute values? Suggested approach? HOT 5
- RFC: Managing tags without extra markup HOT 14
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 react-head.