You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/configuration.md
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -151,3 +151,59 @@ The default value will be changed to `true` in the next major version.
151
151
Specifies the base controller class for the internal `StaticController` used to render [Shorthand routes](/guide/routing#shorthand-routes).
152
152
153
153
By default, Inertia Rails creates a `StaticController` that inherits from `ApplicationController`. You can use this option to specify a different base controller (for example, to include custom authentication, layout, or before actions).
154
+
155
+
### `root_dom_id`
156
+
157
+
**Default**: `'app'`
158
+
**ENV**: `INERTIA_ROOT_DOM_ID`
159
+
160
+
@available_since rails=master
161
+
162
+
Specifies the DOM element ID used for the root Inertia.js element.
163
+
164
+
```ruby
165
+
InertiaRails.configure do |config|
166
+
config.root_dom_id ='inertia-app'
167
+
end
168
+
```
169
+
170
+
> [!NOTE]
171
+
> Make sure your client-side Inertia setup uses the same ID when calling `createInertiaApp`.
When enabled the initial page data is rendered in a `<script type="application/json">` element instead of the `data-page` attribute on the root `<div>`.
181
+
This provides two main benefits:
182
+
183
+
1.**Smaller page size**: JSON data doesn't require HTML entity encoding, reducing the overall HTML payload size.
184
+
2.**Faster parsing**: The browser can parse raw JSON directly from the script element, which is more efficient than parsing HTML-encoded JSON from an attribute.
185
+
186
+
```ruby
187
+
InertiaRails.configure do |config|
188
+
config.use_script_element_for_initial_page =true
189
+
end
190
+
```
191
+
192
+
When disabled (default), the HTML output looks like:
Copy file name to clipboardExpand all lines: docs/guide/responses.md
+71-5Lines changed: 71 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -87,19 +87,85 @@ Please note that if you manually provide a props hash in your render call, the i
87
87
88
88
## Root template data
89
89
90
-
There are situations where you may want to access your prop data in your ERB template. For example, you may want to add a meta description tag, Twitter card meta tags, or Facebook Open Graph meta tags. You can access this data via the `page` method.
90
+
There are situations where you may want to access your prop data in your ERB template. For example, you may want to add a meta description tag, Twitter card meta tags, or Facebook Open Graph meta tags.
91
+
92
+
Inertia Rails provides several view helpers for working with Inertia data in your templates:
93
+
94
+
### `inertia_page`
95
+
96
+
Returns the [page object](/guide/the-protocol#the-page-object) hash containing `component`, `props`, `url`, and other Inertia data. Use this to access page data in your root template.
Sometimes you may even want to provide data to the root template that will not be sent to your JavaScript page / component. This can be accomplished by passing the `view_data` option.
108
+
### `inertia_rendering?`
109
+
110
+
Returns `true` when the current request is rendering an Inertia response. This is useful for conditionally rendering content in shared layouts.
111
+
112
+
```erb
113
+
# app/views/layouts/application.html.erb
114
+
115
+
<% if inertia_rendering? %>
116
+
<%= yield %>
117
+
<% else %>
118
+
<div class="traditional-layout">
119
+
<%= yield %>
120
+
</div>
121
+
<% end %>
122
+
```
123
+
124
+
### `inertia_root`
125
+
126
+
Renders the root element for the Inertia app. This helper automatically respects the [`root_dom_id`](/guide/configuration#root_dom_id) and [`use_script_element_for_initial_page`](/guide/configuration#use_script_element_for_initial_page) configuration options.
127
+
128
+
```erb
129
+
<%= inertia_root %>
130
+
```
131
+
132
+
You can also pass a custom `id` or `page` object if needed:
Returns the SSR head content when [server-side rendering](/guide/server-side-rendering) is enabled. This should be included in your layout's `<head>` section to inject SSR-generated meta tags and other head elements.
141
+
142
+
```erb
143
+
# app/views/layouts/inertia.html.erb
144
+
145
+
<!DOCTYPE html>
146
+
<html>
147
+
<head>
148
+
<%= inertia_ssr_head %>
149
+
</head>
150
+
<body>
151
+
<%= yield %>
152
+
</body>
153
+
</html>
154
+
```
155
+
156
+
### `inertia_meta_tags`
157
+
158
+
Renders meta tags that were defined server-side using the `inertia_meta` configuration. This is useful for SEO when you want to manage meta tags from your Rails controllers. See the [Server-managed meta tags cookbook](/cookbook/server-managed-meta-tags) for a complete guide.
159
+
160
+
```erb
161
+
<head>
162
+
<%= inertia_meta_tags %>
163
+
</head>
164
+
```
165
+
166
+
### Passing additional data to the view
167
+
168
+
Sometimes you may want to provide data to the root template that will not be sent to your JavaScript page / component. This can be accomplished by passing the `view_data` option.
103
169
104
170
```ruby
105
171
defshow
@@ -120,7 +186,7 @@ You can then access this variable like a regular local variable.
As an alternative the page data can be rendered in a `<script type="application/json">` element instead of the `data-page` attribute.
35
+
This approach provides smaller page sizes (no HTML entity encoding required) and faster JSON parsing by the browser.
36
+
37
+
```html
38
+
<scriptdata-page="app"type="application/json">
39
+
{
40
+
"component":"Event",
41
+
"props": {
42
+
"errors": {},
43
+
"event": {
44
+
"id":80,
45
+
"title":"Birthday party",
46
+
"start_date":"2019-06-02",
47
+
"description":"Come out and celebrate Jonathan's 36th birthday party!"
48
+
}
49
+
},
50
+
"url":"/events/80",
51
+
"version":"c32b8e4965f418ad16eaebba1d4e960f"
52
+
}
53
+
</script>
54
+
<divid="app"></div>
55
+
```
56
+
57
+
See the [`use_script_element_for_initial_page`](/guide/configuration#use_script_element_for_initial_page) configuration option to enable this behavior.
58
+
32
59
While the initial response is HTML, Inertia does not server-side render the JavaScript page components. For information on server-side rendering, see the [SSR documentation](/guide/server-side-rendering).
0 commit comments