How to add a custom JavaScript file to Shopify in Liquid?

Tweet LinkedIn

In this tutorial, I’m going to walk you through a cleaner, more organized approach to customizing your Shopify theme.

While Shopify themes generally allow the insertion of custom JavaScript via built-in sections in the theme editor, I find this method a bit messy.

It’s not the most streamlined way to integrate custom styles into your theme.

Plus, when you hand over a Shopify store to a client who isn’t tech-savvy, embedding code directly through the theme customizer can lead to accidental mishaps or bugs.

That’s why I prefer and recommend a different strategy.

We’ll be linking an external JavaScript file to your Shopify theme, consolidating all your custom scripts neatly in one place.

Let’s dive in and see how this can be done for a more efficient and safer customization process.

For the purpose of this tutorial I’m going to use the default Shopify theme which is ā€œDawnā€.

Create the JavaScript file

Let’s start by creating a JavaScript file on your local machine.

This file will hold all those custom code you’ve been thinking about for your Shopify store.

Grab your favorite text editor for this task.

I’m a big fan of VS Code, personally.

Make sure to save your file with a ā€œ.jsā€ extension to signify it’s a JavaScript file.

A good name for this file is ā€œtheme-custom.jsā€.

Why this name?

Well, it’s pretty straightforward and it helps distinguish your custom work from the standard ā€œtheme.jsā€ file that most themes use.

Think of this naming convention as my little trick.

It’s super helpful for anyone who might take over your theme’s development down the line, especially if they don’t have access to the theme’s original repository.

Also, in Shopify themes with sections, you’ll often find JavaScript files linked to individual sections.

This setup means the scripts only load when their specific section does.

A prime example of this is the Dawn theme, which I’ll use in this tutorial.

But hey, if setting up a local file sounds like a hassle, don’t sweat it.

Shopify’s got your back with its cloud code editor, where you can create and manage files directly in the cloud.

You do not need to minimize the JavaScript file, Shopify will do that automatically when the file is requested by the browser.

Upload the file

After you’ve created the file, you need to upload the JavaScript file to your Shopify store.

Go to the Shopify admin, login inside and go to the ā€œOnline Storeā€ menu item (marked with 1 in the image below), the ā€œThemesā€ menu item (marked with 2 in the image below) will be auto selected:

Shopify themes page.
Shopify themes page.

Go to the ā€œ…ā€(3 dots) button (marked with 3 in the image above) and select ā€œEdit codeā€ (marked with 4 in the image above).

Now the Shopify code editor will open. Look for the “Assets” folder in the left sidebar like in the image below.

In order to see the files inside the ā€œAssetsā€ folder click on it and click again on the ā€œAdd a new assetā€ link (marked with 2 in the image below):

Shopify cloud code editor.
Shopify cloud code editor.

This action will open a pop-up that will allow you to upload the JavaScript file created above to Shopify servers. 

You have 2 options here: ā€œUpload a fileā€ (marked with 1 in the image below)  and ā€œCreate a blank fileā€ (marked with 2 in the image below).

Upload a file

If you have the file created you can upload it by clicking the ā€œAdd fileā€ button (marked with 3 in the image below):

Add a new file pop-up in the cloud code editor.
Add a new file pop-up in the cloud code editor.

Create the JavaScript file

If you didn’t create the file you have the option to do it here on the Shopify code editor by clicking the ā€œCreate a blank fileā€ (marked with 1 in the image below):

Create a new JavaScript file in the Shopify cloud code editor.
Create a new JavaScript file in the Shopify cloud code editor.

In order to create the JavaScript file select the extension which in this case should be ā€œJavaScriptā€ (marked with 2 in the image above) and name the file ā€œtheme-custom.jsā€ (marked with 3 in the image above).

When done click on the ā€œDoneā€ button (marked with 4 in the image above) and you’re done.

The new JavaScript file is created.
The new JavaScript file is created.

Now you can add any custom JavaScript code as you please.

You can do it here inside the Shopify editor or pull the file inside a local environment via Shopify CLI in order to edit the file with a code editor like VS Code.

Do not close the Shopify code editor since we are not done yet.

You have the JavaScript file created but for it to work and for Shopify to know that a custom JavaScript file needs to be served you need to tell that to Shopify.

This is why now, you need to link the JavaScript file to Shopify.

To do that we need to find the theme index file which is the “theme.liquid”.

Inside the Shopify code editor go to the ā€œLayoutā€ folder (marked with 1 in the image below) and click on the ā€œtheme.liquidā€ file to open it.

Add the following code (marked with 3 in the image below):

{{ 'theme-custom.js' | asset_url | script_tag }}

I recommend adding the code below the rest of the JavaScript code links inside the body tag to improve page speed.

In the example below I’ve put my custom JavaScript file exactly above the closing </body> tag.

When all is done click the ā€œSaveā€ button to save your changes.

Integration of the JavaScript tag inside the Shopify theme.
Integration of the JavaScript tag inside the Shopify theme.

Test the JavaScript file

Test the file to make sure that Shopify loads it in the front-end.

Go to your store front-end and open the developer tools by pressing ⌘  + Option + I on MAC or F12 or CTRL + ⇧ + I on Windows or Linux. These shortcuts are for Chrome browser.

After you’ve open the dev tools, reload the page by pressing ⌘ + ⇧ + R ( on MacOS ) or CTRL + F5 or CTRL + ⇧ + R (on Windows or Linux ) to do a hard reload.

Now go the ā€œNetworkā€ tab (marked with 1 in the image below), select the JavaScript profile (marked with 2 in the image below) and type in the ā€œFilterā€ box (marked with 3 in the image below) the name of your JavaScript file which in this case is ā€œtheme-custom.jsā€.

JavaScript file verification in the Chrome Developer Tools.
JavaScript file verification in the Chrome Developer Tools.

You should see the file (marked with 4 in the image above) but that doesn’t mean the file loaded correctly. 

This is why you need to check the ā€œStatusā€. If the status is 200 (marked with 5 in the image above) then your file is loaded and whatever styles you add will work correctly.

Conclusion

In conclusion, integrating custom JavaScript into your Shopify store doesn’t have to be a daunting task.

By creating a separate ā€œ.jsā€ file, such as ā€œtheme-custom.jsā€, and uploading it directly to your store, you maintain a clean, organized codebase.

This approach not only keeps your theme customization neat but also makes it easier for any future developers who might work on your site.

Remember, the key is to stay organized and follow best practices to ensure a smooth, bug-free experience for both you and your clients.