Skip to content

asp-fallback CSS tag helper affected by Application Insights JavaScript #8889

@paulirwin

Description

@paulirwin

Describe the bug

If you have the Application Insights js code in your <head> tag ahead of <link> tags (such as bootstrap CSS) with the asp-fallback-* tag helpers, the first stylesheet with the tag helper will always fall back. The reason for this is that the AI js code is calling setTimeout and injecting a <script> tag in the head, effectively creating a race condition with the fallback code, such that when the fallback code tries to find its own script tag, it gets the AI-injected tag instead, and calling previousElementSibling returns the fallback script tag instead of the <meta> tag, which does not have the desired styles, causing the fallback to occur unnecessarily. Subsequent <link> tags are not affected, just the first one.

To Reproduce

Steps to reproduce the behavior:

  1. In an ASP.NET Core web app with Bootstrap CDN CSS in the <head> tag and the asp-fallback-* tag helpers in place, add the Application Insights JavaScript tracking code before the <link> tag.
  2. Run the app with the Network inspector open in Firefox/Chrome
  3. Notice that the CDN version loads successfully, and then the fallback CSS loads. Alternatively, inspect the DOM and notice that this is true.

Expected behavior

The CDN version, assuming it returns 200 OK and the tag helper test attributes are correct, should not fall back.

Screenshots

image

Additional context

A workaround for this issue is to move the AI code after the stylesheet. However, it's worth noting that it may not be just AI that is an issue; any JavaScript that injects script tags in the head could potentially cause this problem. I would suggest investigating if a unique id could be applied to the meta tag (or script tag, but that would still be somewhat fragile if using previousElementSibling) to find it with document.getElementById instead of using the current approach of getElementsByTagName. If it is necessary to continue with the previousElementSibling approach, at least add validation to ensure that it is the meta tag that is expected, and warn and not fall back if it is an unexpected element.

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-very-fewThis issue impacts very few customersarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesbugThis issue describes a behavior which is not expected - a bug.feature-mvc-razor-viewsFeatures related to the Razor view engine for Razor pages and MVC viewshelp wantedUp for grabs. We would accept a PR to help resolve this issueseverity-blockingThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions