Class: Wurk::Engine::AssetMount

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/engine.rb

Overview

Rack::Files doesn’t strip the URL prefix before file lookup, so ‘/wurk-assets/assets/foo.js` would resolve under `vendor/assets/dashboard/ wurk-assets/assets/foo.js` — a path that doesn’t exist. AssetMount rewrites PATH_INFO to drop the ‘/wurk-assets` prefix before delegating to Rack::Files, then falls through to the next middleware on 404 so host-app routes outside the mount keep working.

Constant Summary collapse

PREFIX =
'/wurk-assets'

Instance Method Summary collapse

Constructor Details

#initialize(app, root:) ⇒ AssetMount

Returns a new instance of AssetMount.



28
29
30
31
# File 'lib/wurk/engine.rb', line 28

def initialize(app, root:)
  @app   = app
  @files = ::Rack::Files.new(root)
end

Instance Method Details

#call(env) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/wurk/engine.rb', line 33

def call(env)
  path = env[::Rack::PATH_INFO]
  return @app.call(env) unless path == PREFIX || path.start_with?("#{PREFIX}/")

  inner = env.dup
  stripped = path.delete_prefix(PREFIX)
  inner[::Rack::PATH_INFO] = stripped.empty? ? '/' : stripped
  response = @files.call(inner)
  response[0] == 404 ? @app.call(env) : response
end