Skip to content

Commit

Permalink
Clarify hydration instructions.
Browse files Browse the repository at this point in the history
There should not be any spacing within the root dom. If there is, React will
fail to hydrate. We also remove an incorrect comment on `edit.html.erb` and
refer back to the docs for instructions.
  • Loading branch information
jho406 committed Oct 8, 2023
1 parent 17490eb commit 83ef72c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
15 changes: 5 additions & 10 deletions docs/recipes/server-side-rendering.md
Expand Up @@ -96,20 +96,15 @@ Add a line to your `package.json` like so:
+ "build:ssr": "node ./build-ssr.mjs"
```

Replace `<div id="app">` in your ERB templates with:
Replace `<div id="app"></div>` in your ERB templates `index.html.erb` with:

```erb
<% initial_state = controller.render_to_string(@virtual_path ,formats: [:json], locals: local_assigns, layout: true) %>
<script type="text/javascript">
window.SUPERGLUE_INITIAL_PAGE_STATE=<%= initial_state.html_safe %>;
</script>
<div id="app">
<%= Humid.render(initial_state).html_safe %>
</div>
<div id="app"><%= Humid.render(initial_state).html_safe %></div>
```

!> Do not render spacing above. If you do, React will not hydrate properly and
warn `Hydration failed because the initial UI does not match what was rendered on the server`

In `application.js` change this:

```
Expand Down
10 changes: 4 additions & 6 deletions superglue_rails/lib/generators/rails/templates/web/edit.html.erb
Expand Up @@ -4,9 +4,7 @@
window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;
</script>

<div id="app">
<%%# If you need SSR follow instructions at %>
<%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
<%%# and uncomment the following line %>
<%%#= Humid.render(initial_state).html_safe %>
</div>
<%%# If you need SSR follow instructions at %>
<%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
<div id="app"></div>

Expand Up @@ -4,4 +4,6 @@
window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;
</script>

<%%# If you need SSR follow instructions at %>
<%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
<div id="app"></div>
Expand Up @@ -4,4 +4,6 @@
window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;
</script>

<%%# If you need SSR follow instructions at %>
<%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
<div id="app"></div>
Expand Up @@ -4,4 +4,6 @@
window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;
</script>

<%%# If you need SSR follow instructions at %>
<%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
<div id="app"></div>

0 comments on commit 83ef72c

Please sign in to comment.