Skip to content

Commit

Permalink
Merge pull request #1886 from Shopify/jloppert/fix-vanity-url-stable-…
Browse files Browse the repository at this point in the history
…3-45

only replace font urls when content type is not present or it is a text
  • Loading branch information
isaacroldan authored May 2, 2023
2 parents d10f484 + 4be9dd7 commit b071592
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-terms-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

fixes theme preview for new shopify cdn url format
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ def call(env)

# Proxy the request, and replace the URLs in the response
status, headers, body = @app.call(env)
body = replace_font_urls(body)
[status, headers, body]
# Use Rack::Response to get the content type header regardless of case
response = Rack::Response.new(nil, status, headers)
content_type = response.get_header("Content-Type")
if content_type.nil? || content_type == "" || content_type&.start_with?("text/")
Rack::Response.new(replace_font_urls(body), status, headers).finish
else
Rack::Response.new(body, status, headers).finish
end
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,28 @@ def test_404_on_missing_cdn_fonts
assert_equal("Not found", response.body)
end

def test_do_not_replace_when_content_type_does_not_match_text
CdnFonts.any_instance.expects(:replace_font_urls).never
original_input = "https://fonts.shopifycdn.com/font.woff2"
response = serve(original_input, path: "/cdn/shop/products/tan-colored-hat-on-monochrome-background.jpg",
content_type: "image/jpeg")

assert_equal(original_input, response.body)
end

def test_replace_when_content_type_does_match_text
original_input = "https://fonts.shopifycdn.com/font.woff2"
response = serve(original_input, path: "/cdn/shop/products/style.css", content_type: "text/css")
response_body = response.body
refute_equal(original_input, response_body)
assert_equal("/fonts/font.woff2", response_body)
end

private

def serve(response_body = "", path: "/")
def serve(response_body = "", path: "/", content_type: "text/html")
app = lambda do |_env|
[200, {}, [response_body]]
[200, { "Content-Type" => content_type }, [response_body]]
end
stack = CdnFonts.new(app, theme: theme)
request = Rack::MockRequest.new(stack)
Expand Down

0 comments on commit b071592

Please sign in to comment.