JavaScript (JS)
JavaScript is used to add interactivity to your template, including custom behaviors for blocks, sections, and pages.
Guidelines
- NPM packages are not supported. Only local JS files included in your template can be used.
- Include JS files in
config.jsonunder thejssection to ensure they are compiled and loaded properly. - Scripts can be loaded async or deferred to improve page performance.
Best Practices
- Keep block-specific JS in separate files corresponding to each block or section.
- Avoid polluting the global namespace; use modules or closures where possible.
- Use vanilla JS or lightweight libraries already included in the template.
- Test scripts on all pre-defined pages, including cart, profile, and product pages, to ensure compatibility.
- Inline JS in your Twig templates will not work; Only rely on JS files compiled through the template system.