Class: Inertia::Frontend
- Inherits:
-
Object
- Object
- Inertia::Frontend
- Defined in:
- lib/inertia/frontend.rb
Overview
Manages frontend asset integration for Inertia responses.
Class Method Summary collapse
-
.dist ⇒ Pathname
Returns the directory containing built frontend assets.
-
.package_runner ⇒ String
Returns the command prefix for executing npm packages.
-
.render_layout(data) ⇒ String
Renders the HTML layout with the Inertia page object embedded.
-
.root ⇒ Pathname
Returns the root directory of the frontend application.
-
.version ⇒ String
Returns a version identifier for the frontend assets.
Class Method Details
.dist ⇒ Pathname
Returns the directory containing built frontend assets.
Uses Configuration#build_path if set, otherwise defaults to the
dist directory inside the frontend root.
36 37 38 |
# File 'lib/inertia/frontend.rb', line 36 def dist @dist ||= Inertia.config.build_path || root.join("dist") end |
.package_runner ⇒ String
Returns the command prefix for executing npm packages.
Detects the package manager by checking for lock files and returns the appropriate command to run package binaries.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/inertia/frontend.rb', line 61 def package_runner @package_runner ||= if root.join("package-lock.json").exist? "npx" elsif root.join("pnpm-lock.yaml").exist? "pnpm exec" elsif root.join("bun.lockb").exist? || root.join("bun.lock").exist? "bun x --bun" elsif root.join("yarn.lock").exist? "yarn" elsif root.join("deno.lock").exist? "deno x" else raise "No supported package manager detected" end @package_runner end |
.render_layout(data) ⇒ String
Renders the HTML layout with the Inertia page object embedded.
In development, it fetches the layout from the Vite dev server and rewrites relative asset paths to absolute URLs. In production, it reads the pre-built layout from disk and caches it.
87 88 89 90 91 92 93 |
# File 'lib/inertia/frontend.rb', line 87 def render_layout(data) if Rage.env.development? build_dynamic_layout(data) else build_static_layout(data) end end |
.root ⇒ Pathname
Returns the root directory of the frontend application.
Uses Configuration#frontend_path if set, otherwise searches for a Vite config file in common locations to determine where the frontend source lives.
20 21 22 23 24 25 26 27 |
# File 'lib/inertia/frontend.rb', line 20 def root @root ||= Inertia.config.frontend_path || begin vite_config = Rage.root.glob(["*/vite.config.{js,ts,mjs,mts}", "app/*/vite.config.{js,ts,mjs,mts}"]).first raise "Vite config not found" unless vite_config vite_config.dirname end end |
.version ⇒ String
Returns a version identifier for the frontend assets.
Computes an MD5 hash of the Vite manifest or index.html to detect when assets have changed, enabling Inertia's asset versioning.
47 48 49 50 51 52 |
# File 'lib/inertia/frontend.rb', line 47 def version @version ||= begin manifest = dist.glob([".vite/manifest.json", "index.html"]).first Digest::MD5.file(manifest.to_s).hexdigest if manifest end end |