2

In order to make our application SPA, we have used a third party library Page.js along with ASP.NET MVC.

Page.js Reference: https://visionmedia.github.io/page.js/

We perform basically two tasks by making use of Page.js library.

  1. First, we hit some controller/action (partial view) on MVC application & then inject the returned HTML into DOM.
  2. Secondly, at the same time, we change the URL of the browser using this library.

All the Partial Views which we are fetching through Page.js also has the reference to <script/> tag. So whenever we inject the returned HTML into DOM, we start seeing the deprecation issue in browser's console tab.

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

Application Flow:

Below picture explains the flow of our application.

enter image description here

Currently, all the child partial views are also having their own script & that script gets executed as soon as they are injected in DOM.

Attempted approach as a solution to fix this Deprecation issue:

On each partial view(not the child one), that is injected via Page.js library, we are writing below code to load all the dependencies asynchronously. And in the callback function (once all the dependencies are loaded), we are executing the page-specific javascript code like below.

HTML of the Page that is injected into DOM via Ajax (i.e. Page Partial View):

<div>
    ......
    ......
</div>

<!-- This part causes issue -->
<!--<script type="application/javascript" src="/script1.js"></script>
<script type="application/javascript" src="/script2.js"></script>-->

<script>
$(function () {
    $.getMultiScripts(["@scriptFile1", "@scriptFile2"]).done(function () {
        onPageLoad();
    });

    function onPageLoad() {
        // 1. Defer kendo scripts
        @Html.Kendo().DeferredScripts(false);

        // 2. Initialize partial view 1
        partialView1.init();

        // 2. Initialize partial view 2
        partialView2.init();

        // 3. Initialize partial view 3
        partialView3.init();
    }
});
</script>

Important: $.getMultipleScripts was taken from here - How to include multiple js files using jQuery $.getScript() method

The script at the end of child Partial View looks like below:

<script>
    window.partialView1 = (function() {

        var init = function() {

            // 1. Initialize partial control
            tabControl.initControl();

            // 2. Initialize partial control
            common.bindEvents();
        };

        return {
            init: init
        };
    })();
</script>

Note: For this, we have also stopped child partial views to execute the javascript code as soon as they are loaded. In fact, we are now just providing the javascript definitions (in the form of module pattern) in partial views & controlling the whole execution from the main page (parent partial view).

I know that this is not a good approach. We could have used some frontend framework. But at this stage, we can't take that decision to make the entire framework shift.

From this post, I just wanted to know, whether this approach is good or there could be a better approach to resolve this issue. If yes, please share.

Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
  • We are using the jQuery library for Ajax. – Ankush Jain Aug 09 '19 at 12:16
  • No, actually the content which we load via ajax & inject into DOM has the script tags. That further makes sync request to load page specific javascript files. That eventually causes this issue. – Ankush Jain Aug 09 '19 at 12:20
  • exactly. those are normal script tags that reference to page specific javascript file. – Ankush Jain Aug 09 '19 at 12:23
  • I have provided details about $.getMultiScripts in the above question. – Ankush Jain Aug 09 '19 at 12:26
  • Yes. I know. $.getMultiScripts is the solution to load the scripts asynchronously. As I mentioned in the question, that this is the approach we have tried and working fine. But we are looking for other better approach (if available). – Ankush Jain Aug 09 '19 at 12:29
  • I just updated the question. Added the part you requested that is actually causing the issue. These are those scripts tags which are injected into DOM once the HTML Response is received from the ajax request. – Ankush Jain Aug 09 '19 at 12:36
  • try ctrl + f "This part causes issue" on this page. – Ankush Jain Aug 09 '19 at 12:37
  • It is the script tag which is causing the issue. You can have a look here as well - https://stackoverflow.com/a/28478146/1273882 – Ankush Jain Aug 09 '19 at 12:41
  • So how can we fix this issue? Because we can't ask jQuery to load these file asynchronously, because their order is very important. – Ankush Jain Aug 09 '19 at 12:44
  • Yes exactly, that's why, as of now, we are going with the above approach to deal with this warning. By the way, thanks for your time & help. – Ankush Jain Aug 09 '19 at 12:51

0 Answers0