Sha256: 70a5034fc028355554ab6b56626dfe2691321973f2904d739e0a7d8485af030e
Contents?: true
Size: 1.18 KB
Versions: 4
Compression:
Stored size: 1.18 KB
Contents
# frozen_string_literal: true module ThemeCheck class RemoteAsset < HtmlCheck severity :suggestion categories :html, :performance doc docs_url(__FILE__) TAGS = %w[img script link source] PROTOCOL = %r{(https?:)?//} ABSOLUTE_PATH = %r{\A/[^/]}im RELATIVE_PATH = %r{\A(?!#{PROTOCOL})[^/\{]}oim def on_element(node) return unless TAGS.include?(node.name) resource_url = node.attributes["src"]&.value || node.attributes["href"]&.value return if resource_url.nil? || resource_url.empty? # Ignore if URL is Liquid, taken care of by AssetUrlFilters check return if resource_url =~ ABSOLUTE_PATH return if resource_url =~ RELATIVE_PATH return if url_hosted_by_shopify?(resource_url) # Ignore non-stylesheet rel tags rel = node.attributes["rel"] return if rel && rel.value != "stylesheet" add_offense( "Asset should be served by the Shopify CDN for better performance.", node: node, ) end private def url_hosted_by_shopify?(url) url.start_with?(Liquid::VariableStart) && AssetUrlFilters::ASSET_URL_FILTERS.any? { |filter| url.include?(filter) } end end end
Version data entries
4 entries across 4 versions & 1 rubygems