-
Notifications
You must be signed in to change notification settings - Fork 364
Cannot reference js.liquid files in theme.liquid #720
Comments
I've been dealing with this same issue all day. If you put JS in any of the Slate v0 was simple, but v1 has been throwing me for a loop. Have you figured out how to insert JS files into the templates yet? |
Is there any reason you're trying to add an additional JS file manually instead of letting Webpack handle it? I don't think there are many reason to manually want to include a JS file like this in Slate v1. If it's a case of only wanting to load it under certain conditions the Slate Webpack build handles this with lazy loading. If you'd like me to elaborate on this, let me know. |
Hi Dan, Is it possible to elaborate a little further? I basically need the javascript to fire on Collections and Index Templates. Should I be creating a separate modules folder within assets? Cheers |
You can create a new file in This means this file will only load when on the collections page so you have a smaller initial footprint for your users. If you could show me the contents of your |
@dan-gamble Usually I build websites using CodeKit frameworks stored on my local machine. Ideally, I'd like to continue pulling files from the same location as my previous framework -- just don't know how via Webpack. With Slate v0, I was able to use CodeKit to compile JS files to the
|
Thanks for the help! Essentially it would probably look something like this:
So also making use of jquery for Ajax calls. |
Hey @johnytotheg, So what your import $ from 'jquery';
function getFilterProducts(tag){
$.ajax({
type: 'GET',
url: '/products.json',
dataType: 'json',
success: function(res){
filterProducts(res.products, tag)
},
error: function(status){
alert(status);
}
})
}
function filterProducts(products, tag) {
var filteredProducts = products.filter(function(product){
return product.tags.indexOf(tag) > -1;
});
}
$(function(){
$('.js-collection-filter').on('change', function(){
var tag = $(this).val()
getFilterProducts(tag)
});
}); This should then work, only thing worth nothing is adding this file before you run If you could add that file and then try |
Slate v1 has embraced the current Javascript ecosystem which unfortunately is a little harder to understand and get working but reaps so many rewards so it's definitely worth getting up to speed with it. Depending on what your Anything else hit me up 😊 |
Ah thank you! I hadn't tried re-running yarn start. I'll give this a go 👍 |
@dan-gamble Ok. My tests are showing that page.js is getting compiled to any template / subtemplate of page.liquid. That is, How is Webpack better than simply using liquid if then statements like this? My best guess is that it has something to do with being able to bundle page-specific files, instead of including a bunch of smaller ones. However, Codekit was already capable of doing that with Slate v0:
I've got a few questions related to that:
P.S. Thanks for all your help, by the way. The documentation is as clear as mud on a lot of this stuff. |
One of the benefits is that your Javascript is all coming from a single source and is using modules. When you're using separate files like the above any kind of sharing will come from global state. The way the separate files are also all done automatically for you by just placing a file in a directory instead of having to maintain them in liquid via if statements and then having the file as well. You could in theory change a name in 1 place and forget to change it in the other, a slight benefit. The above are just references to the way Javascript is included on your page. Webpack as a whole offers tons of benefits over a traditional "include a script on a page Javascript" approach, too many to list here.
The way i approach this is a bit like the dynamic entrypoints but it doesn't actually create a new entry point. In my Array.from(document.querySelectorAll('[data-module]')).forEach(el => {
const name = el.getAttribute('data-module')
const Module = require(`../modules/${name}`).default
new Module(el)
}) And then in my liquid files i would add I'd then add export default {
constructor (el) {
this.el = el
}
} And then any code i wanted would go in this file. Then for each location you wanted it to run you'd just add the Again, hope this helps! |
Sorry Stephen, my code should have read: export default class TestModule {
constructor (el) {
this.el = el
}
} This is purely my own idea on how i've just worked with Webpack before, it's not anything to do with Slate so it makes sense for it not to have been documented. Could be helpful to have just some generic Javascript help pointed to somewhere as v1 is a very big jump from v0 but i'm sure it will come over time. Remember Slate is still very much in beta. |
Assets have been simplified in #850 and the change is now available in v1.0.0-beta.12 Docs that have also been updated: |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Problem
Referencing js.liquid files in theme.liquid returns a 404 (Not Found) error. From my understanding javascript files are supposed to be served from localhost:8080 but the script tag in page source is attempting to fetch from //cdn.shopify.com/s/files/1/0027/8672/1904/t/2/assets/product-filter.js?11934124183750268063 which returns a 404.
product-filter.js.liquid is showing in dist/assets.
Replication steps
I have referenced the file like so
<script src='{{ "../assets/static/product-filter.js" | asset_url }}'></script>
in theme .liquid. I have added the file 'product-filter.js.liquid' to src/assets/static.More Information
I have also tried referencing the file in the following ways:
<script src='{{ "product-filter.js" | asset_url }}'></script>
<script src='{{ '../assets/static/product-filter.js' | asset_url }}'></script>
<script src='{{ 'product-filter.js' | asset_url }}'></script>
The text was updated successfully, but these errors were encountered: