1
votes

I have a view layout (main.cshtml) where I am calling an external javascript file. I have renderings (another cshtml files) which are included as placeholders to this layout(main.cshtml). example: two pages: 1) http://localhost/home/ has two renderings for Body placeholder 2)http://localhost/about/ has two renderings for Body placeholder

both home and about pages uses same main.cshtml, I don't want to load externalJS.js every time I navigate from home to about or vice versa. i.e;the externalJS.js should load once for entire application. Can I achieve it?

    <!DOCTYPE html>
<html>
<head>
    <title>Main</title>
</head>
<body>
    <div data-role="page" class="pageWrapper">
        <header data-role="header" class="header">
            @Html.Sitecore().Placeholder("Header")
        </header>
            <div class="wrapper" data-role="main">
            @Html.Sitecore().Placeholder("Body")
        </div>
        <div data-role="footer" role="contentinfo" class="ui-footer ui-bar-inherit">
            @Html.Sitecore().Placeholder("Footer")
        </div>
    </div>
  <script src="../../js/externalJS.js"></script>
 </body>
</html>
2

2 Answers

3
votes

If you are worried about the bandwidth usage and load on your servers from users downloading exernalJS.js each time they visit one of your pages, you're worries might already be solved by web browser caching. Basically the web browser saves a copy of html, css, js, image files, etc locally and reloads those if it needs to, rather than jumping back out to the network.

On the other hand, if the initial processing of externalJS.js is what you want to avoid, then something like Ajax (Asynchronous Javascript and Xml) is what you want. The idea behind Ajax is that you write Javascript to handle fetching new content from the network. So rather than the user clicking on an anchor and having their browser download an entirely new page, you would set up something for them to click on and Javascript would then send a request to the server, which would return some xml (or html, or json) and Javascript would then insert the new data into the existing page without the browser reloading anything or changing pages. Note that you may want to use Javascript to add the change to the browser's history since that won't happen by default. See here for an Ajax tutorial.

0
votes

The technique to implement with Sitecore you may consider is called bundling as it does the job exactly as you want, and even more. It is a feature of ASP.NET which Sitecore is built on.

Most of the current major browsers limit the number of simultaneous connections per each hostname to six. That means that while six requests are being processed, additional requests for assets on a host will be queued by the browser. Bundles can unite several styles / scripts into one "file" to address this issue.

Pay attention to v parameter with a long hash stamp - that is version stamp so that it remains the same for the same combination of scripts / style in you bundle. As soon as it remains the same - it is cached by a browser and is normally requested just once for the first time, regardless of which page is called by.

<link href="/Styles/css?v=Za3K5TEVXIEtbh2FEp3kzkdlpWT7iVdGKSiEj6SVOaI1" rel="stylesheet"/>

There is also a technique called minification that comes along with bundles - you may not only "clue" several scripts to one combining file with a specific version stamp, but also minimize (compress) that file to take less bandwidth to transfer - quite handy on a high-traffic websites.

Here are useful links that will explain how to implement bundling and minification with Sitecore:

http://sitecorefootsteps.blogspot.co.uk/2014/01/implementing-bundling-and-minification.html

https://himynameistim.wordpress.com/2014/12/05/bundling-and-minification-with-sitecore/

http://jockstothecore.com/bundling-with-sitecore-mvc/

http://techblog.lbigroup.be/2013/08/21/adding-bundling-and-minification-to-a-sitecore-project/