Skip to main content

JavascriptOncePerFile

Identifies files that contain more than one {% javascript %} tag. Theme Check reports the second and later JavaScript tags because {% javascript %} can appear only once per file.

Each section or block file should keep its scoped JavaScript in a single JavaScript tag.


The following examples contain code snippets that either fail or pass this check.

In the following example, sections/hero.liquid contains two {% javascript %} tags:

{% javascript %}
customElements.define('hero-banner', HeroBanner);
{% endjavascript %}

{% javascript %}
customElements.define('hero-video', HeroVideo);
{% endjavascript %}

In the following example, sections/hero.liquid contains one {% javascript %} tag:

{% javascript %}
customElements.define('hero-banner', HeroBanner);
customElements.define('hero-video', HeroVideo);
{% endjavascript %}

The following example contains the default configuration for this check:

JavascriptOncePerFile:
enabled: true
severity: error
ParameterDescription
enabledWhether this check is enabled.
severityThe severity of the check.

Anchor to Disabling this checkDisabling this check

Disabling this check isn't recommended. If you can't avoid violating the rule, then you should disable the check using the comment syntax. This ensures that you intentionally disable the check for each instance.

{% # theme-check-disable JavascriptOncePerFile %}
{% javascript %}
customElements.define('hero-banner', HeroBanner);
{% endjavascript %}

{% javascript %}
customElements.define('hero-video', HeroVideo);
{% endjavascript %}
{% # theme-check-enable JavascriptOncePerFile %}

Was this page helpful?