Class: Inertia::Frontend

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia/frontend.rb

Overview

Manages frontend asset integration for Inertia responses.

Class Method Summary collapse

Class Method Details

.distPathname

Returns the directory containing built frontend assets.

Uses Configuration#build_path if set, otherwise defaults to the dist directory inside the frontend root.

Returns:

  • (Pathname)

    path to the build output directory

Raises:

  • (RuntimeError)

    if no Vite config file is found



36
37
38
# File 'lib/inertia/frontend.rb', line 36

def dist
  @dist ||= Inertia.config.build_path || root.join("dist")
end

.package_runnerString

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.

Returns:

  • (String)

    command prefix (e.g., "npx", "pnpm exec", "yarn")

Raises:

  • (RuntimeError)

    if no supported package manager is detected



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.

Parameters:

  • data (Hash)

    the Inertia page object to embed

Returns:

  • (String)

    HTML document with page data injected



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

.rootPathname

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.

Returns:

  • (Pathname)

    path to the frontend root directory

Raises:

  • (RuntimeError)

    if no Vite config file is found



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

.versionString

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.

Returns:

  • (String)

    MD5 hex digest of the manifest file

Raises:

  • (RuntimeError)

    if no Vite config file is found



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