Class: Froxy::Proxy

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Benchmarkable
Defined in:
lib/froxy/proxy.rb

Constant Summary collapse

BUILD_PATH =
'public/froxy/build'
CLI =
File.expand_path('../../bin/froxy', __dir__)
FALLTHRU_TYPES =
/\.(png|gif|jpeg|jpg|svg|ico|webp|avif)$/i.freeze
FILE_EXT_MAP =
{
  '.jsx' => '.js'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Proxy

Returns a new instance of Proxy.



19
20
21
22
23
# File 'lib/froxy/proxy.rb', line 19

def initialize(app)
  @app = app
  @file_server = Rack::Files.new(Rails.root)
  @build_file_server = Rack::Files.new(Rails.root.join(BUILD_PATH))
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/froxy/proxy.rb', line 25

def call(env)
  req = Rack::Request.new(env)
  path_info = req.path_info

  if req.get? || req.head?
    # Let images through.
    return @file_server.call(env) if FALLTHRU_TYPES.match?(path_info)

    # Let JS sourcemaps through.
    return @build_file_server.call(env) if /\.js\.map$/i.match?(path_info)

    # Let esbuild handle JS and CSS.
    if /\.(js|jsx|css)$/i.match?(path_info)
      return unless (path = clean_path(path_info))
      return [404, {}, []] unless file_readable?(path)

      return @file_server.call(env) unless Rails.application.config.froxy.use_esbuild

      return benchmark logging_message(req) do
        build env, req, path
      end
    end
  end

  @app.call req.env
end