module Jekyll::Vite::Proxy

Internal: Extend the default servlet to add a Rack-based proxy in order to forward asset requests to the Vite.js development server.

Constants

STATUS_SERVE_ORIGINAL

Internal: Used to detect proxied requests since it's not a valid status code.

Public Class Methods

new(server, *args) click to toggle source
Calls superclass method
# File lib/jekyll/vite/proxy.rb, line 12
def initialize(server, *args)
  @server = server
  super
end

Public Instance Methods

service(req, res) click to toggle source

Override: Detect the special status set by the Proxy Servlet and use the default Jekyll response instead.

Calls superclass method
# File lib/jekyll/vite/proxy.rb, line 28
def service(req, res)
  proxy_servlet.service(req, res)
  if res.status == STATUS_SERVE_ORIGINAL
    res.status = 200
    super
  end
end
set_filename(req, res) click to toggle source

Override: Serve compiled Vite assets from the temporary folder as needed.

Calls superclass method
# File lib/jekyll/vite/proxy.rb, line 18
def set_filename(req, res)
  original_root = @root.dup
  if req.path_info.start_with?("/#{ ViteRuby.config.public_output_dir }/")
    @root = ViteRuby.config.root.join(ViteRuby.config.public_dir)
  end
  super.tap { @root = original_root }
end

Private Instance Methods

proxy_servlet() click to toggle source

Internal: A WEBRick servlet that uses a Rack proxy internally.

# File lib/jekyll/vite/proxy.rb, line 39
def proxy_servlet
  @proxy_servlet ||= begin
    # Called by the proxy if a request shouldn't be served by Vite.
    app = ->(_env) { [STATUS_SERVE_ORIGINAL, {}, []] }

    # Initialize the proxy which is a Rack app.
    proxy = ViteRuby::DevServerProxy.new(app)

    # Return a servlet compliant with WEBrick.
    Rack::Handler::WEBrick.new(@server, proxy)
  end
end