Class: Inertia::Middleware::Assets

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

Overview

Serves built Vite assets in production using x-sendfile headers.

Constant Summary collapse

EMPTY_BODY =
[].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Assets

Returns a new instance of Assets.



11
12
13
# File 'lib/inertia/middleware/assets.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/inertia/middleware/assets.rb', line 15

def call(env)
  method = env["REQUEST_METHOD"]
  return @app.call(env) unless method == "GET" || method == "HEAD"

  raw_path = env["PATH_INFO"]
  return @app.call(env) unless raw_path.start_with?("/assets/")

  [
    200,
    {
      "x-sendfile" => raw_path.delete_prefix("/assets"),
      "x-sendfile-root" => assets_root,
      "cache-control" => "public, max-age=31536000, immutable"
    },
    EMPTY_BODY
  ]
end