Adding new JS to theme

Hello guys,

maybe this is a bit novice question, but can somebody tell me how to check if my JS file is properly included inside the theme?

I’ve created a file, added it to JS folder of my theme, included it inside the Theme.php using this code:

protected $javascript = [
        ‘src/js/my-file.js’
];

However, when i load frontend i don’t see it inside the source code. Is that the right way to check if it’s loading or not?

 

Thank you

All javascript (and css) is going to be compiled into one js (and css) file. Therefore you won’t see your script as a seperate file in the source code.

Just add a

console.log('loaded');

to your script, to see in dev tools if it was loaded.

1 „Gefällt mir“

Hi @vsimovic‍,

yes, this is the best way!

Your JavaScript file will be packed in one file along with the other JavaScript files. Later, only one JavaScript file will be loaded.

Example:

/web/cache/1522314906_3c66ed1a002a3ceb873d4ad016155baa.js

Best regards

image
enerSpace Webhosting
Tel.: +49 511 - 219 568 76 | Web: https://www.enerspace.de

1 „Gefällt mir“

Hi @enerSpace‍ ,

yeah, all JS files are minified, but in that single JS file i can’t find my code, which is quite strange.

Is there a way to add JS code in footer of the website and tell Shopware not to include it into the minify file or to tell him not to include the JS file into the minify process, but to include it regurarly in footer?

 

Thanks

I’ve solved the issue. For some reason Shopware wasn’t recompiling theme files properly. Strange, but now it works fine, after i turned on „Disable compiler cache“ option.