Search code examples
azureasp.net-coretag-helpersazure-cdn

CDN css and JS in Azure with https and version not working


I'm using ASP.NET Core. I have configured a CDN for CSS and JS files.

This my code in the TagHelper in my HTML:

<script src="https://mycdn.azureedge.net/dist/web.bundle.js"
                asp-append-version="true"
                asp-fallback-src="~/dist/web.bundle.js"
                asp-fallback-test="window.jQuery">
</script> 

My problem is that the redirect is done hover http and I need https.

This is the error in my browser:

Mixed Content: The page at 'https://www.myweb.com' was loaded over HTTPS, but requested an insecure stylesheet 'http://www.myweb.com/dist/web.bundle.js'. This request has been blocked; the content must be served over HTTPS.

Another thing is that my CSS and JS is not appending the version param.

<script src="https://mycdn.azureedge.net/dist/web.bundle.js"></script>

Thanks!!


Solution

  • Don't encode the scheme when linking to external files on CDN.

    Instead of src="https://mycdn.azureedge.net/dist/web.bundle.js" use src="//mycdn.azureedge.net/dist/web.bundle.js". Then the browser will use the current scheme, https or http depending on which one is used.

    Edit:

    Now that I am home I can also reply the second part of your question :P

    As I suspected, the asp-append-version tag helper is only for relative urls, which makes sense. Using it for CDN makes little sense, because your application would have to download it every time to recalculate it's hashsum to add it to the url (the version is calculated based on the file contents and each time the file changes, the version changes and the browser is forced to fetch a new version).

    You can see that on GitHub here.

    Uri uri;
    if (Uri.TryCreate(resolvedPath, UriKind.Absolute, out uri) && !uri.IsFile)
    {
        // Don't append version if the path is absolute.
        return path;
    }