Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Add AssetPreload check #603

Closed
wants to merge 1 commit into from
Closed

Conversation

krzksz
Copy link
Contributor

@krzksz krzksz commented Jul 12, 2022

Hey 👋

The goal of this check is to discourage people from preloading assets using plain HTML in cases when it is possible to use Liquid filters instead.

It is sometimes the case that <link rel="preload" ...> is positioned late in the <head>, preventing the browser from discovering it early. In addition, this manual approach is not automatically converted to Link header so Shopify is not able to forward it to Early Hints response. These issues are not present when using Liquid filters mentioned above.

@krzksz krzksz requested review from macournoyer and charlespwd July 12, 2022 10:52
Copy link
Contributor

@charlespwd charlespwd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely love this 👍

Minor nits about the language used in the errors, but this beautiful stuff 👏

### &#x2717; Fail

```liquid
<link href="style.css" rel="preload" as="style">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: a storefront would typically use <link href="{{ 'style.css' | asset_url }}" rel="preload" as="style"> here. You can't really have assets as relative paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅

return if node.attributes["rel"]&.downcase != "preload"
case node.attributes["as"]&.downcase
when "style"
add_offense("Prefer preload argument on stylesheet_tag filter", node: node)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit:

For better performance, prefer using the preload argument of the stylesheet_tag filter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and something similar for the other ones)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅

@krzksz krzksz force-pushed the asset-preload-check branch from be6420b to 3a31dff Compare July 14, 2022 08:14
@krzksz krzksz requested a review from charlespwd July 14, 2022 08:18
END
)
assert_offenses(<<~END, offenses)
Prefer preload argument on stylesheet_tag filter at templates/index.liquid:1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests should be failing considering the error message change, shouldn't they?

@charlespwd
Copy link
Contributor

charlespwd commented Jul 14, 2022

Hmm why are the unit tests not running? 🤔

Oh. @krzksz, it's because you haven't used the Shopify remote when pushing your changes (you used a fork) and we probably have setup this repo to run unit tests only by employees. We should change that... or figure out a better way.

@krzksz
Copy link
Contributor Author

krzksz commented Jul 14, 2022

Closing in favour of #605

@krzksz krzksz closed this Jul 14, 2022
@krzksz krzksz deleted the asset-preload-check branch July 14, 2022 14:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants