Skip to content

Commit

Permalink
Add vanity url support for embedded URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuamsager committed Apr 27, 2023
1 parent cc5e617 commit 809b697
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Theme
class DevServer
class LocalAssets
THEME_REGEX = %r{//cdn\.shopify\.com/s/.+?/(assets/.+?\.(?:css|js))}
VANITY_THEME_REGEX = %r{//cdn/shop/.+?/(assets/.+?\.(?:css|js))}

class FileBody
def initialize(path)
Expand Down Expand Up @@ -44,12 +45,11 @@ def call(env)
private

def replace_asset_urls(body)
replaced_body = body.join.gsub(THEME_REGEX) do |match|
path = Regexp.last_match[1]
if @target.static_asset_paths.include?(path)
"/#{path}"
else
match
replaced_body = body.join
[THEME_REGEX, VANITY_THEME_REGEX].each do |regex|
replaced_body = replaced_body.gsub(regex) do |match|
path = Regexp.last_match[1]
@target.static_asset_paths.include?(path) ? "/#{path}" : match
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ def test_replace_local_assets_on_same_line
assert_equal(expected_html, serve(original_html).body)
end

def test_replace_local_assets_in_reponse_body_with_vanity_url
original_html = <<~HTML
<html>
<head>
<link rel="stylesheet" href="//cdn/shop/t/2/assets/footer.css" />
</head>
</html>
HTML
expected_html = <<~HTML
<html>
<head>
<link rel="stylesheet" href="/assets/footer.css" />
</head>
</html>
HTML
assert_equal(expected_html, serve(original_html).body)
end

def test_dont_replace_other_assets
original_html = <<~HTML
<html>
Expand Down

0 comments on commit 809b697

Please sign in to comment.