Skip to content

Commit 7f84110

Browse files
committed
docs: update readme to cover the new configuration options
1 parent 46626e4 commit 7f84110

File tree

2 files changed

+83
-32
lines changed

2 files changed

+83
-32
lines changed

README.md

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ Automatic component name is also opt in, you must set the `default_render` confi
6767

6868
### Layout
6969

70-
Inertia layouts use the rails layout convention and can be set or changed in the same way. The original `layout` config option is still functional, but will likely be deprecated in the future in favor
71-
of using rails layouts.
70+
Inertia layouts use the rails layout convention and can be set or changed in the same way.
7271

7372
```ruby
7473
class EventsController < ApplicationController
@@ -133,20 +132,14 @@ end
133132
}
134133
```
135134

136-
Deep merging can be set as the project wide default via the InertiaRails configuration:
135+
Deep merging can be configured using the `deep_merge_shared_data` configuration option.
137136

138-
```ruby
139-
# config/initializers/some_initializer.rb
140-
InertiaRails.configure do |config|
141-
config.deep_merge_shared_data = true
142-
end
143-
144-
```
145-
146-
If deep merging is enabled by default, it's possible to opt out within the action:
137+
If deep merging is enabled, you can still opt-out within the action:
147138

148139
```ruby
149140
class CrazyScorersController < ApplicationController
141+
inertia_config(deep_merge_shared_data: true)
142+
150143
inertia_share do
151144
{
152145
basketball_data: {
@@ -163,7 +156,7 @@ class CrazyScorersController < ApplicationController
163156
end
164157
end
165158

166-
# Even if deep merging is set by default, since the renderer has `deep_merge: false`, it will send a shallow merge to the frontend:
159+
# `deep_merge: false` overrides the default:
167160
{
168161
basketball_data: {
169162
points: 100,
@@ -187,34 +180,85 @@ If you don't need a controller to handle a static component, you can route direc
187180
inertia 'about' => 'AboutComponent'
188181
```
189182

190-
### SSR
183+
### SSR _(experimental)_
184+
185+
Enable SSR via the configuration options for `ssr_enabled` and `ssr_url`.
191186

192-
Enable SSR via the config settings for `ssr_enabled` and `ssr_url`.
187+
When using SSR, don't forget to add `<%= inertia_ssr_head %>` to the `<head>` of your layout (i.e. `application.html.erb`).
193188

194-
When using SSR, don't forget to add `<%= inertia_ssr_head %>` to the `<head>` of your `application.html.erb`.
189+
## Configuration ⚙️
195190

196-
## Configuration
191+
Inertia Rails can be configured globally or in a specific controller (and subclasses).
197192

198-
Inertia Rails has a few different configuration options that can be set anywhere, but the most common location is from within an initializer.
193+
### Global Configuration
194+
195+
If using global configuration, we recommend you place the code inside an initializer:
199196

200-
The default config is shown below
201197
```ruby
198+
# config/initializers/inertia.rb
199+
202200
InertiaRails.configure do |config|
203-
204-
# set the current version for automatic asset refreshing. A string value should be used if any.
205-
config.version = nil
206-
# enable default inertia rendering (warning! this will override rails default rendering behavior)
207-
config.default_render = true
208-
209-
# ssr specific options
210-
config.ssr_enabled = false
211-
config.ssr_url = 'http://localhost:13714'
201+
# Example: force a full-reload if the deployed assets change.
202+
config.version = ViteRuby.digest
203+
end
204+
```
212205

213-
config.deep_merge_shared_data = false
214-
206+
The default configuration can be found [here](https://github.com/inertiajs/inertia-rails/blob/master/lib/inertia_rails/configuration.rb#L5-L22).
207+
208+
### Local Configuration
209+
210+
Use `inertia_config` in your controllers to override global settings:
211+
212+
```ruby
213+
class EventsController < ApplicationController
214+
inertia_config(
215+
version: "events-#{InertiaRails.configuration.version}",
216+
ssr_enabled: -> { action_name == "index" },
217+
)
215218
end
216219
```
217220

221+
### Configuration Options
222+
223+
#### `version` _(recommended)_
224+
225+
This allows Inertia to detect if the app running in the client is oudated,
226+
forcing a full page visit instead of an XHR visit on the next request.
227+
228+
See [assets versioning](https://inertiajs.com/asset-versioning).
229+
230+
__Default__: `nil`
231+
232+
#### `deep_merge_shared_data`
233+
234+
When enabled, props will be deep merged with shared data, combining hashes
235+
with the same keys instead of replacing them.
236+
237+
__Default__: `false`
238+
239+
#### `default_render`
240+
241+
Overrides Rails default rendering behavior to render using Inertia by default.
242+
243+
__Default__: `false`
244+
245+
#### `ssr_enabled` _(experimental)_
246+
247+
Whether to use a JavaScript server to pre-render your JavaScript pages,
248+
allowing your visitors to receive fully rendered HTML when they first visit
249+
your application.
250+
251+
Requires a JS server to be available at `ssr_url`. [_Example_](https://github.com/ElMassimo/inertia-rails-ssr-template)
252+
253+
__Default__: `false`
254+
255+
#### `ssr_url` _(experimental)_
256+
257+
The URL of the JS server that will pre-render the app using the specified
258+
component and props.
259+
260+
__Default__: `"http://localhost:13714"`
261+
218262
## Testing
219263

220264
If you're using Rspec, Inertia Rails comes with some nice test helpers to make things simple.

lib/inertia_rails/configuration.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
module InertiaRails
44
class Configuration
55
DEFAULTS = {
6+
# Whether to combine hashes with the same keys instead of replacing them.
7+
deep_merge_shared_data: false,
8+
9+
# Overrides Rails default rendering behavior to render using Inertia by default.
610
default_render: false,
711

8-
# Let Rails decide which layout should be used based on the controller configuration.
12+
# DEPRECATED: Let Rails decide which layout should be used based on the
13+
# controller configuration.
914
layout: true,
1015

11-
deep_merge_shared_data: false,
16+
# SSR options.
1217
ssr_enabled: false,
1318
ssr_url: 'http://localhost:13714',
19+
20+
# Used to detect version drift between server and client.
1421
version: nil,
1522
}.freeze
1623

0 commit comments

Comments
 (0)