From c70553f54deeeea26b2c97ff0ba3b18a68b51847 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Wed, 3 Dec 2025 11:24:55 +0300 Subject: [PATCH 1/6] Add use_script_element_for_initial_page config option --- app/views/inertia.html.erb | 2 +- docs/guide/client-side-setup.md | 3 + docs/guide/configuration.md | 56 +++++++++++++ docs/guide/responses.md | 76 +++++++++++++++-- docs/guide/the-protocol.md | 27 ++++++ .../inertia/install/templates/initializer.rb | 1 + lib/inertia_rails/configuration.rb | 6 ++ lib/inertia_rails/helper.rb | 14 ++++ spec/inertia/rendering_spec.rb | 83 +++++++++++++++++++ 9 files changed, 262 insertions(+), 6 deletions(-) diff --git a/app/views/inertia.html.erb b/app/views/inertia.html.erb index e52113b8..8eeec0fc 100644 --- a/app/views/inertia.html.erb +++ b/app/views/inertia.html.erb @@ -1 +1 @@ -
+<%= inertia_root(page: page) %> diff --git a/docs/guide/client-side-setup.md b/docs/guide/client-side-setup.md index 01732666..dd18245b 100644 --- a/docs/guide/client-side-setup.md +++ b/docs/guide/client-side-setup.md @@ -292,3 +292,6 @@ createInertiaApp({ // ... }) ``` + +> [!NOTE] +> Make sure the [`root_dom_id`](/guide/configuration#root_dom_id) configuration option matches the `id` property in your client-side setup. diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 0ed0a138..a9c0ff3c 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -151,3 +151,59 @@ The default value will be changed to `true` in the next major version. Specifies the base controller class for the internal `StaticController` used to render [Shorthand routes](/guide/routing#shorthand-routes). 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). + +### `root_dom_id` + +**Default**: `'app'` +**ENV**: `INERTIA_ROOT_DOM_ID` + +@available_since rails=master + +Specifies the DOM element ID used for the root Inertia.js element. + +```ruby +InertiaRails.configure do |config| + config.root_dom_id = 'inertia-app' +end +``` + +> [!NOTE] +> Make sure your client-side Inertia setup uses the same ID when calling `createInertiaApp`. + +### `use_script_element_for_initial_page` + +**Default**: `false` +**ENV**: `INERTIA_USE_SCRIPT_ELEMENT_FOR_INITIAL_PAGE` + +@available_since rails=master core=master + +When enabled the initial page data is rendered in a ` +
+``` + +> [!NOTE] +> When using this option make sure your client-side Inertia setup is configured to read the page data from the ` +
+``` + +See the [`use_script_element_for_initial_page`](/guide/configuration#use_script_element_for_initial_page) configuration option to enable this behavior. + 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). ## Inertia responses diff --git a/lib/generators/inertia/install/templates/initializer.rb b/lib/generators/inertia/install/templates/initializer.rb index 83ef4b25..c8430fd1 100644 --- a/lib/generators/inertia/install/templates/initializer.rb +++ b/lib/generators/inertia/install/templates/initializer.rb @@ -4,4 +4,5 @@ config.version = ViteRuby.digest config.encrypt_history = true config.always_include_errors_hash = true + config.use_script_element_for_initial_page = true end diff --git a/lib/inertia_rails/configuration.rb b/lib/inertia_rails/configuration.rb index 482802c3..e8603b9d 100644 --- a/lib/inertia_rails/configuration.rb +++ b/lib/inertia_rails/configuration.rb @@ -34,6 +34,12 @@ class Configuration # Whether to include empty `errors` hash to the props when no errors are present. always_include_errors_hash: nil, + + # Whether to use `') + end + + it 'renders div without data-page attribute' do + expect(response.body).to include('
') + end + end + + context 'when true with custom root_dom_id' do + before do + InertiaRails.configure do |c| + c.use_script_element_for_initial_page = true + c.root_dom_id = 'inertia-app' + end + get component_path + end + + it 'renders script element with custom data-page attribute' do + expect(response.body).to include('