-
Notifications
You must be signed in to change notification settings - Fork 34
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
Server rendering methods other than renderToString(headTags) #14
Comments
Thanks @Maarten88 this is a great write up. I appreciate you taking the time! I think this might be related to #15 — I will dig into this more tomorrow and should hopefully have a built in fix soon. Thanks again! |
I proposed a solution to this here, #15 (comment) would love to hear your thoughts @Maarten88 |
Great, your proposal is exactly the same change that I made (except for the name of the attribute). It did not work without any attribute when I tried that. If you want, I could push my updated version to github for comparison? I ported your code typescript and made a small change so it works for IE. It has been running without problems for a few weeks now, (on a site with with very low traffic however). |
@Maarten88 Great solution! |
I implemented react-head in a small project, and got it working, great!
I did have some trouble however before I got it working, and had to make some modifications, I wonder if this may help others.
Because my server setup is somewhat different, I wanted to render the complete html using a react template that includes the complete head element. But I found that the HeadTag component only works correctly when the collected elements are rendered using
renderToString(headTags)
, as shown in the docs/sample. HeadTag selects elements for client-side replacement by querying with[data-reactroot=""]
, which is added only to root elements by the renderToString function.Doing it any other way breaks it. Rendering the headTags using a plain
<head>{headTags}</head>
does not insert thedata-reactroot=""
attribute, causing duplicate elements. Using<head>{renderToString(headTags)}</head>
escapes it. Removing the[data-reactroot=""]
selector breaks the clientside. In the end, I solved it by adding a specificdata-ssr=""
attribute on the server component and selecting on that. This makes HeadTag independent from the way it is rendered on the server. I also added a key to the client-side version, to get rid of some React warnings.The result is this (it's also in typescript):
It's only three small changes, but server rendering is much more flexible, now I can render like this:
The text was updated successfully, but these errors were encountered: