lib/serviceworker/route.rb in serviceworker-rails-0.5.5 vs lib/serviceworker/route.rb in serviceworker-rails-0.6.0

- old
+ new

@@ -1,33 +1,46 @@ # frozen_string_literal: true + module ServiceWorker class Route attr_reader :path_pattern, :asset_pattern, :options - RouteMatch = Struct.new(:path, :asset_name, :headers) do + RouteMatch = Struct.new(:path, :asset_name, :headers, :options) do def to_s asset_name end end + def self.webpacker?(options) + options.key?(:pack) && Handlers.webpacker? + end + + def self.sprockets?(options) + options.key?(:asset) + end + def initialize(path_pattern, asset_pattern = nil, options = {}) if asset_pattern.is_a?(Hash) options = asset_pattern asset_pattern = nil end @path_pattern = path_pattern - @asset_pattern = asset_pattern || options[:asset] || path_pattern + @asset_pattern = if self.class.webpacker?(options) + asset_pattern || options.fetch(:pack, path_pattern) + else + asset_pattern || options.fetch(:asset, path_pattern) + end @options = options end def match(path) raise ArgumentError, "path is required" if path.to_s.strip.empty? asset = resolver.call(path) or return nil - RouteMatch.new(path, asset, headers) + RouteMatch.new(path, asset, headers, options) end def headers @options.fetch(:headers, {}) end @@ -37,17 +50,17 @@ def resolver @resolver ||= AssetResolver.new(path_pattern, asset_pattern) end class AssetResolver - PATH_INFO = "PATH_INFO".freeze + PATH_INFO = "PATH_INFO" DEFAULT_WILDCARD_NAME = :paths - WILDCARD_PATTERN = %r{\/\*([^\/]*)} - NAMED_SEGMENTS_PATTERN = %r{\/([^\/]*):([^:$\/]+)} - LEADING_SLASH_PATTERN = %r{^\/} + WILDCARD_PATTERN = %r{\/\*([^\/]*)}.freeze + NAMED_SEGMENTS_PATTERN = %r{\/([^\/]*):([^:$\/]+)}.freeze + LEADING_SLASH_PATTERN = %r{^\/}.freeze INTERPOLATION_PATTERN = Regexp.union( /%%/, - /%\{(\w+)\}/, # matches placeholders like "%{foo}" + /%\{(\w+)\}/ # matches placeholders like "%{foo}" ) attr_reader :path_pattern, :asset_pattern def initialize(path_pattern, asset_pattern)