Nicolas Durán
Nicolas Durán

Reputation: 292

Load view-specific JavaScript in Node.js Express application

I have web application with multiple views that share the same JavaScript but every view also have its own Javascript. It looks something like:

<!-- html up here -->

<script src="/src/js/bundle.js"></script>

<script>
    // some view-specific Javascript
</script>

This ended up mixing my view file (HTML, etc) with my javascript and I'm not sure if creating and loading and external javascript file for each view is a better way.

What is the best approach to achieve do this?

Upvotes: 0

Views: 764

Answers (1)

Danny H
Danny H

Reputation: 366

A lot of it depends on how big your page specific js files are. If they are fairly small and most of the useful js is in your bundle, just make one bundle for all pages. If you have a lot of page specific js, you can either just include a second file along with your bundle, or you can create a different bundle for each page so that you only have on script tag on each page. Here is another thread on the issue, including a useful gulp script to automate the creation of multiple bundles

Upvotes: 1

Related Questions