Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assess the guidance in the 'Location of <script> tags' section #34371

Open
guardrex opened this issue Dec 14, 2024 · 2 comments
Open

Assess the guidance in the 'Location of <script> tags' section #34371

guardrex opened this issue Dec 14, 2024 · 2 comments

Comments

@guardrex
Copy link
Collaborator

guardrex commented Dec 14, 2024

Description

The current guidance was provided by the PU in the 8.0 era, but per @hakenr ...

... it’s not the difference between interactive and static rendering that makes the difference.

It’s due to the page lifecycle and the nature of navigation in such scenarios.

Even with static SSR, when enhanced navigation is enabled, the behavior of dynamically added, removed, or modified scripts gets tricky:

  • The onLoad event doesn’t work as expected.
  • Blazor events like webStart and enhanceNavigation need to be used instead.

I think it’s only safe to use the <script> tag in the statically rendered root component (App.razor), where the script tag itself isn’t expected to change, appear, or disappear. 😇

We'll try to get a PU engineer, probably Javier, on in January to help improve the section. The important bit on the compile-time error going away is true. Javier did remove that at 8.0 on dotnet/razor#8744. The summary from his PR that supports the current language is (emphasis added) ...

  • Removes the check that we have to produce an error when you include a script tag inside a Razor file.
  • We used to have this check to preclude people accidentally adding script tags to their components, since in the context of interactive rendering it does not do what they expect.
  • As we are rendering components statically in .NET 8.0, this warning/errors creates more problems than it solves, and there is no reason for us to keep it around, and instead, we will let the customers do as they see fit.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/location-of-javascript?view=aspnetcore-9.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/javascript-interoperability/location-of-javascript.md

Document ID

af240267-89c0-f192-7807-cf426001ad8d

Article author

@guardrex

Related Issues

Copy link
Contributor

🏂🎁 Happy Holidays! ❄️⛄

Stand-by! ... A green dinosaur 🦖 will be along shortly to assist.

@guardrex guardrex changed the title Assess the guidance in the *Location of <script> tags* section Assess the guidance in the 'Location of <script> tags' section Dec 14, 2024
@guardrex guardrex added Pri1 doc-enhancement and removed Source - Docs.ms Docs Customer feedback via GitHub Issue labels Dec 14, 2024
@guardrex guardrex moved this from Triage to P0/P1 - High Priority in Blazor.Docs Dec 14, 2024
@hakenr
Copy link
Member

hakenr commented Dec 14, 2024

@guardrex Thanks for creating the issue.

I think it’s only safe to use the <script> tag in the statically rendered root component (App.razor), where the script tag itself isn’t expected to change, appear, or disappear.

To be honest, my simplification isn’t entirely accurate. When enhanced navigation kicks in, the behavior is affected, and events like onLoad might not work as expected, even in App.razor.

I think we could resolve this by adding a warning about how scripts might behave in different scenarios, with references to the appropriate sections where the details are already explained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: P0/P1 - High Priority
Development

No branches or pull requests

2 participants