Thursday, 4 July 2019

Asp-fallback and (custom) dependend scripts

I am using Asp.Net Core 2.2 and would like to load common js libraries from a cdn, and fallback to local resources in intranet situations where the cdn is not reachable.

The fallback itself works, but I have custom scripts that use jquery and fail when the cdn resource fails, although jquery gets loaded from the local source. This are the script tags I used:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
  asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
  asp-fallback-test="window.jQuery"
  crossorigin="anonymous"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=">
</script>

<script src="~/js/myScript.js" asp-append-version="true"></script>

My script fails with ReferenceError: $ is not defined. I assume that is because the failback resource loaded later than my script, because typing $ in the prompt yields function().

Additional info: I also get errors in the js log stating that the integrity hash is incorrect. This does not bother me (yet), since the fallback libraries work.

Also it takes a very long time before the page noticed that the cdn was not reachable and loaded the local fallback (almost 2 minutes). To block the cdn I added a temporary entry in my local dns file (hosts in windows).

What do I have to do to make my own scripts work with the fallback?



from Asp-fallback and (custom) dependend scripts

No comments:

Post a Comment