Class: ThemeCheck::RemoteAsset

Inherits:
HtmlCheck show all
Defined in:
lib/theme_check/checks/remote_asset.rb

Constant Summary collapse

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/"

Constants inherited from HtmlCheck

HtmlCheck::START_OR_END_QUOTE, HtmlCheck::VARIABLE

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

Instance Method Summary collapse

Methods included from ChecksTracking

#inherited

Methods inherited from Check

#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_theme?

Methods included from JsonHelpers

#format_json_parse_error

Instance Method Details

#on_element(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/theme_check/checks/remote_asset.rb', line 14

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.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 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