Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vanity url support for embedded URLs #1862

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/theme.css" />
</head>
</html>
HTML
expected_html = <<~HTML
<html>
<head>
<link rel="stylesheet" href="/assets/theme.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