Sha256: 8131a2cf14af771eceafb700af2c9ca274ebfb4f4c442a27e37b78dc1861aaa5

Contents?: true

Size: 1.27 KB

Versions: 11

Compression:

Stored size: 1.27 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
    CDN_ROOT = "https://cdn.shopify.com/"

    def on_element(node)
      return unless TAGS.include?(node.name)

      resource_url = node.attributes["src"] || node.attributes["href"]
      return if resource_url.nil? || resource_url.empty?

      # Ignore if URL is Liquid, taken care of by AssetUrlFilters check
      return if resource_url.start_with?(CDN_ROOT)
      return if resource_url =~ ABSOLUTE_PATH
      return if resource_url =~ RELATIVE_PATH
      return if url_hosted_by_shopify?(resource_url)

      # Ignore non-stylesheet link tags
      rel = node.attributes["rel"]
      return if node.name == "link" && rel != "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

11 entries across 11 versions & 1 rubygems

Version Path
theme-check-1.8.0 lib/theme_check/checks/remote_asset.rb
theme-check-1.7.2 lib/theme_check/checks/remote_asset.rb
theme-check-1.7.1 lib/theme_check/checks/remote_asset.rb
theme-check-1.7.0 lib/theme_check/checks/remote_asset.rb
theme-check-1.6.2 lib/theme_check/checks/remote_asset.rb
theme-check-1.6.1 lib/theme_check/checks/remote_asset.rb
theme-check-1.6.0 lib/theme_check/checks/remote_asset.rb
theme-check-1.5.2 lib/theme_check/checks/remote_asset.rb
theme-check-1.5.1 lib/theme_check/checks/remote_asset.rb
theme-check-1.5.0 lib/theme_check/checks/remote_asset.rb
theme-check-1.4.0 lib/theme_check/checks/remote_asset.rb