Skip to content

Commit 9791833

Browse files
authored
Update docs (#190)
* docs: Update dependencies * docs: Fix next page link for Introduction page * docs: Fix format * docs: Fix `setError()` typo * docs: Mention JsRoutes gem * docs: Mention SSR on the Protocol page * docs: Fix Svelte 5 tab rendered twice * docs: Add configuration page * docs: Add docs for `inertia_share` options
1 parent 256a3c9 commit 9791833

File tree

11 files changed

+727
-706
lines changed

11 files changed

+727
-706
lines changed

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export default defineConfig({
131131
items: [
132132
{ text: 'Asset versioning', link: '/guide/asset-versioning' },
133133
{ text: 'Code splitting', link: '/guide/code-splitting' },
134+
{ text: 'Configuration', link: '/guide/configuration' },
134135
{ text: 'Error handling', link: '/guide/error-handling' },
135136
{ text: 'Events', link: '/guide/events' },
136137
{ text: 'Progress indicators', link: '/guide/progress-indicators' },

docs/guide/configuration.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Configuration
2+
3+
Inertia Rails can be configured globally or in a specific controller (and subclasses).
4+
5+
## Global Configuration
6+
7+
If using global configuration, we recommend you place the code inside an initializer:
8+
9+
```ruby
10+
# config/initializers/inertia.rb
11+
12+
InertiaRails.configure do |config|
13+
# Example: force a full-reload if the deployed assets change.
14+
config.version = ViteRuby.digest
15+
end
16+
```
17+
18+
The default configuration can be found [here](https://github.com/inertiajs/inertia-rails/blob/master/lib/inertia_rails/configuration.rb#L5).
19+
20+
## Local Configuration
21+
22+
Use `inertia_config` in your controllers to override global settings:
23+
24+
```ruby
25+
class EventsController < ApplicationController
26+
inertia_config(
27+
version: "events-#{InertiaRails.configuration.version}",
28+
ssr_enabled: -> { action_name == "index" },
29+
)
30+
end
31+
```
32+
33+
## Configuration Options
34+
35+
### `component_path_resolver`
36+
37+
Use `component_path_resolver` to customize component path resolution when [`default_render`](#default_render) config value is set to `true`. The value should be callable and will receive the `path` and `action` parameters, returning a string component path. See [Automatically determine component name](/guide/responses#automatically-determine-component-name).
38+
39+
**Default**: `->(path:, action:) { "#{path}/#{action}" }`
40+
41+
### `deep_merge_shared_data`
42+
43+
When enabled, props will be deep merged with shared data, combining hashes
44+
with the same keys instead of replacing them.
45+
46+
**Default**: `false`
47+
48+
### `default_render`
49+
50+
Overrides Rails default rendering behavior to render using Inertia by default.
51+
52+
**Default**: `false`
53+
54+
### `encrypt_history`
55+
56+
When enabled, you instruct Inertia to encrypt your app's history, it uses
57+
the browser's built-in [`crypto` api](https://developer.mozilla.org/en-US/docs/Web/API/Crypto)
58+
to encrypt the current page's data before pushing it to the history state.
59+
60+
**Default**: `false`
61+
62+
### `ssr_enabled` _(experimental)_
63+
64+
Whether to use a JavaScript server to pre-render your JavaScript pages,
65+
allowing your visitors to receive fully rendered HTML when they first visit
66+
your application.
67+
68+
Requires a JS server to be available at `ssr_url`. [_Example_](https://github.com/ElMassimo/inertia-rails-ssr-template)
69+
70+
**Default**: `false`
71+
72+
### `ssr_url` _(experimental)_
73+
74+
The URL of the JS server that will pre-render the app using the specified
75+
component and props.
76+
77+
**Default**: `"http://localhost:13714"`
78+
79+
### `version` _(recommended)_
80+
81+
This allows Inertia to detect if the app running in the client is oudated,
82+
forcing a full page visit instead of an XHR visit on the next request.
83+
84+
See [assets versioning](https://inertiajs.com/asset-versioning).
85+
86+
**Default**: `nil`

docs/guide/csrf-protection.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Axios automatically checks for the existence of an `XSRF-TOKEN` cookie. If it's
5858
The easiest way to implement this is using server-side middleware. Simply include the `XSRF-TOKEN` cookie on each response, and then verify the token using the `X-XSRF-TOKEN` header sent in the requests from axios. (That's basically what `inertia_rails` does).
5959

6060
> [!NOTE]
61+
>
6162
> `X-XSRF-TOKEN` header only works for [Inertia requests](/guide/the-protocol#inertia-responses). If you want to send a normal request you can use `X-CSRF-TOKEN` instead.
6263
6364
## Handling mismatches

docs/guide/forms.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ return (
261261
)
262262
```
263263

264-
== Svelte 4|Svelte 5
264+
== Svelte 4
265265

266266
```svelte
267267
<script>
@@ -565,7 +565,7 @@ $form.clearErrors('field', 'anotherfield')
565565

566566
:::
567567

568-
If you're using a client-side input validation libraries or do client-side validation manually, you can set your own errors on the form using the `setErrors()` method.
568+
If you're using a client-side input validation libraries or do client-side validation manually, you can set your own errors on the form using the `setError()` method.
569569

570570
:::tabs key:frameworks
571571
== Vue

docs/guide/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
next:
3+
text: 'Demo Application'
4+
link: '/guide/demo-application'
5+
---
6+
17
# Introduction
28

39
Welcome to the documentation for [inertia_rails](https://github.com/inertiajs/inertia-rails) adapter for [Ruby on Rails](https://rubyonrails.org/) and [Inertia.js](https://inertiajs.com/).

docs/guide/responses.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ end
8585

8686
With this configuration, the `app/frontend/pages/events/index.(jsx|vue|svelte)` page component is rendered automatically, passing the `@events` instance variable as the `events` prop.
8787

88+
If the default component path doesn't match your convention, you can define a custom resolution method via the `component_path_resolver` config value. The value should be callable and will receive the path and action parameters, returning a string component path.
89+
90+
````ruby
91+
inertia_config(
92+
component_path_resolver: ->(path:, action:) do
93+
"Storefront/#{path.camelize}/#{action.camelize}"
94+
end
95+
)
96+
8897
## Root template data
8998

9099
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.
@@ -97,7 +106,7 @@ There are situations where you may want to access your prop data in your ERB tem
97106
<% end %>
98107
99108
<div id="app" data-page="<%= page.to_json %>"></div>
100-
```
109+
````
101110
102111
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.
103112

docs/guide/routing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ class UsersController < ApplicationController
3535
end
3636
```
3737

38-
Another option is to use the [`js_from_routes`](https://js-from-routes.netlify.app) gem, that makes named, server-side routes available on the client via a autogenerated helpers.
38+
Another option is to use [JsRoutes](https://github.com/railsware/js-routes) or [JS From Routes](https://js-from-routes.netlify.app) gems that make named server-side routes available on the client via autogenerated helpers.

docs/guide/shared-data.md

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,59 @@ Sometimes you need to access specific pieces of data on numerous pages within yo
44

55
## Sharing data
66

7-
Inertia's Rails adapter comes with the `shared_data` controller method. This method allows you to define shared data that will be automatically merged with the page props provided in your controller.
7+
The `inertia_share` method allows you to define data that will be available to all controller actions, automatically merging with page-specific props.
8+
9+
### Basic Usage
810

911
```ruby
1012
class EventsController < ApplicationController
11-
# share synchronously
12-
inertia_share app_name: env['app.name']
13+
# Static sharing: Data is evaluated immediately
14+
inertia_share app_name: Rails.configuration.app_name
1315

14-
# share lazily, evaluated at render time
16+
# Dynamic sharing: Data is evaluated at render time
1517
inertia_share do
16-
if logged_in?
17-
{
18-
user: logged_in_user,
18+
{
19+
user: current_user,
20+
notifications: current_user&.unread_notifications_count
21+
} if user_signed_in?
22+
end
23+
24+
# Alternative syntax for single dynamic values
25+
inertia_share total_users: -> { User.count }
26+
end
27+
```
28+
29+
### Conditional Sharing
30+
31+
You can control when data is shared using Rails-style controller filters. The `inertia_share` method supports these filter options:
32+
33+
- `only`: Share data for specific actions
34+
- `except`: Share data for all actions except specified ones
35+
- `if`: Share data when condition is true
36+
- `unless`: Share data when condition is false
37+
38+
```ruby
39+
class EventsController < ApplicationController
40+
# Share user data only when authenticated
41+
inertia_share if: :user_signed_in? do
42+
{
43+
user: {
44+
name: current_user.name,
45+
email: current_user.email,
46+
role: current_user.role
1947
}
20-
end
48+
}
2149
end
2250

23-
# share lazily alternate syntax
24-
inertia_share user_count: lambda { User.count }
51+
# Share data only for specific actions
52+
inertia_share only: [:index, :show] do
53+
{
54+
meta: {
55+
last_updated: Time.current,
56+
version: "1.0"
57+
}
58+
}
59+
end
2560
end
2661
```
2762

docs/guide/the-protocol.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Content-Type: text/html; charset=utf-8
3333
```
3434

3535
> [!NOTE]
36-
> While the initial response is HTML, Inertia does not server-side render the JavaScript page components.
36+
> While the initial response is HTML, Inertia does not server-side render the JavaScript page components by default (see [Server-side Rendering](http://localhost:5173/guide/server-side-rendering)).
3737
3838
## Inertia responses
3939

0 commit comments

Comments
 (0)