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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/inertia/frontend.rb', line 81

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



130
131
132
133
134
135
136
# File 'lib/inertia/frontend.rb', line 130

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

.runtimeString

Returns the JavaScript runtime command.

Detects the runtime by checking for lock files and returns the appropriate command to execute scripts.

Returns:

  • (String)

    runtime command (e.g., "node", "bun", "deno run")

Raises:

  • (RuntimeError)

    if no supported runtime is detected



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/inertia/frontend.rb', line 106

def runtime
  @runtime ||= if root.join("package-lock.json").exist?
    "node"
  elsif root.join("pnpm-lock.yaml").exist?
    "node"
  elsif root.join("bun.lockb").exist? || root.join("bun.lock").exist?
    "bun"
  elsif root.join("yarn.lock").exist?
    "yarn node"
  elsif root.join("deno.lock").exist?
    "deno run --allow-net --allow-env"
  else
    raise "No supported JavaScript runtime detected"
  end
end

.ssr_distPathname

Returns the directory containing built SSR assets.

Uses Configuration::Ssr#build_path if set, otherwise defaults to dist/ssr inside the frontend root.

Returns:

  • (Pathname)

    path to the build output directory

Raises:

  • (RuntimeError)

    if no Vite config file is found



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/inertia/frontend.rb', line 47

def ssr_dist
  @ssr_dist ||= begin
    path = Inertia.config.ssr.build_path || dist.join("ssr")
    public_path = Rage.root.join("public")

    if path.to_s.start_with?(public_path.to_s)
      raise ArgumentError, "SSR build path cannot be inside public/; configure it explicitly via config.ssr.build_path"
    end

    path
  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



67
68
69
70
71
72
# File 'lib/inertia/frontend.rb', line 67

def version
  @version ||= begin
    manifest = dist.glob([".vite/manifest.json", "index.html"]).first
    Digest::MD5.file(manifest.to_s).hexdigest if manifest
  end
end