Skip to content

Commit 4aef4e5

Browse files
douglaraYukioArie
authored andcommitted
Install Inertia
1 parent e39870a commit 4aef4e5

File tree

16 files changed

+774
-7
lines changed

16 files changed

+774
-7
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ gem 'tailwindcss-rails', '~> 2.0'
128128
gem 'opentelemetry-exporter-otlp', '~> 0.26.1'
129129
gem 'opentelemetry-instrumentation-all', '~> 0.50.1'
130130
gem 'opentelemetry-sdk', '~> 1.3'
131+
132+
gem "inertia_rails", "~> 3.17"

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ GEM
300300
image_processing (1.13.0)
301301
mini_magick (>= 4.9.5, < 5)
302302
ruby-vips (>= 2.0.17, < 3)
303+
inertia_rails (3.17.0)
304+
railties (>= 6)
303305
io-console (0.8.0)
304306
irb (1.14.3)
305307
rdoc (>= 4.0.0)
@@ -835,6 +837,7 @@ DEPENDENCIES
835837
html2text
836838
htmlbeautifier
837839
image_processing
840+
inertia_rails (~> 3.17)
838841
jbuilder (~> 2.7)
839842
json_csv
840843
jsonb_accessor (= 1.3.2)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { createInertiaApp, type ResolvedComponent } from '@inertiajs/react'
2+
import { StrictMode } from 'react'
3+
import { createRoot } from 'react-dom/client'
4+
5+
void createInertiaApp({
6+
// Set default page title
7+
// see https://inertia-rails.dev/guide/title-and-meta
8+
//
9+
// title: title => title ? `${title} - App` : 'App',
10+
11+
// Disable progress bar
12+
//
13+
// see https://inertia-rails.dev/guide/progress-indicators
14+
// progress: false,
15+
16+
resolve: (name) => {
17+
const pages = import.meta.glob<{default: ResolvedComponent}>('../pages/**/*.tsx', {
18+
eager: true,
19+
})
20+
const page = pages[`../pages/${name}.tsx`]
21+
if (!page) {
22+
console.error(`Missing Inertia page component: '${name}.tsx'`)
23+
}
24+
25+
// To use a default layout, import the Layout component
26+
// and use the following line.
27+
// see https://inertia-rails.dev/guide/pages#default-layouts
28+
//
29+
// page.default.layout ||= (page: ReactNode) => (<Layout>{page}</Layout>)
30+
31+
return page
32+
},
33+
34+
setup({ el, App, props }) {
35+
createRoot(el).render(
36+
<StrictMode>
37+
<App {...props} />
38+
</StrictMode>
39+
)
40+
},
41+
42+
defaults: {
43+
form: {
44+
forceIndicesArrayFormatInFormData: false,
45+
},
46+
future: {
47+
useScriptElementForInitialPage: true,
48+
useDataInertiaHeadAttribute: true,
49+
useDialogForErrorModal: true,
50+
preserveEqualProps: true,
51+
},
52+
},
53+
}).catch((error) => {
54+
// This ensures this entrypoint is only loaded on Inertia pages
55+
// by checking for the presence of the root element (#app by default).
56+
// Feel free to remove this `catch` if you don't need it.
57+
if (document.getElementById("app")) {
58+
throw error
59+
} else {
60+
console.error(
61+
"Missing root element.\n\n" +
62+
"If you see this error, it probably means you loaded Inertia.js on non-Inertia pages.\n" +
63+
'Consider moving <%= vite_typescript_tag "inertia.tsx" %> to the Inertia-specific layout instead.',
64+
)
65+
}
66+
})

app/javascript/types/globals.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { FlashData, SharedProps } from '@/types'
2+
3+
declare module '@inertiajs/core' {
4+
export interface InertiaConfig {
5+
sharedPageProps: SharedProps
6+
flashDataType: FlashData
7+
errorValueType: string[]
8+
}
9+
}

app/javascript/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type FlashData = {
2+
notice?: string
3+
alert?: string
4+
}
5+
6+
export type SharedProps = {}

app/javascript/types/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

app/views/layouts/application.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title>Woofed CRM</title>
4+
<title data-inertia>Woofed CRM</title>
55
<meta name="viewport" content="width=device-width,initial-scale=1">
66
<%= csrf_meta_tags %>
77
<%= csp_meta_tag %>
8+
<%= vite_stylesheet_tag "application" %>
9+
<%= vite_react_refresh_tag %>
810
<%= vite_client_tag %>
11+
<%= vite_typescript_tag "inertia.tsx" %>
12+
<%= inertia_ssr_head %>
913
<%= vite_stylesheet_tag 'application', 'data-turbolinks-track': 'reload', media: 'all' %>
1014
<%= vite_javascript_tag 'application', 'data-turbolinks-track': 'reload' %>
1115
<%= render "layouts/shared/analytics_scripts" %>

bin/dev

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
#!/usr/bin/env sh
22

3-
if ! gem list foreman -i --silent; then
3+
export PORT="${PORT:-3000}"
4+
5+
if command -v overmind 1> /dev/null 2>&1
6+
then
7+
overmind start -f Procfile.dev "$@"
8+
exit $?
9+
fi
10+
11+
if command -v hivemind 1> /dev/null 2>&1
12+
then
13+
echo "Hivemind is installed. Running the application with Hivemind..."
14+
exec hivemind Procfile.dev "$@"
15+
exit $?
16+
fi
17+
18+
if gem list --no-installed --exact --silent foreman; then
419
echo "Installing foreman..."
520
gem install foreman
621
fi
722

8-
exec foreman start -f Procfile.dev "$@"
23+
foreman start -f Procfile.dev "$@"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
InertiaRails.configure do |config|
4+
config.version = ViteRuby.digest
5+
config.encrypt_history = true
6+
config.always_include_errors_hash = true
7+
config.use_script_element_for_initial_page = true
8+
config.use_data_inertia_head_attribute = true
9+
end

config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
require 'sidekiq/web'
22

33
Rails.application.routes.draw do
4+
if Rails.env.development?
5+
# Redirect to localhost from 127.0.0.1 to use same IP address with Vite server
6+
constraints(host: '127.0.0.1') do
7+
get '(*path)', to: redirect { |params, req| "#{req.protocol}localhost:#{req.port}/#{params[:path]}" }
8+
end
9+
end
410
mount Motor::Admin => '/motor_admin'
511
mount Sidekiq::Web => '/sidekiq'
612
mount GoodJob::Engine => 'good_job'

0 commit comments

Comments
 (0)