Description
This addresses a usability issue. We often hear of people trying to put <script>
elements inside components and being surprised about the effects.
It's almost always a mistake to put a <script>
element inside a component, because fundamentally, components are about adding and removing things dynamically inside the DOM. <script>
elements don't work that way, in that once they are added, their effects can never be removed. Similarly, trying to use C# expressions inside a <script>
content will not work as expected (we can't replace the effects of already-registered JS once the expression changes value). The only scenario I can think of where a <script>
element does something vaguely useful inside a component would be for some kind of debug logging, e.g., <script>console.log('rendering now')</script>
, but that's not what anyone's been doing AFAIK. What developers really should do is put <script>
elements into their index.html
, where they will behave as expected.
Proposed solution
We'll make it a compiler error to have <script>
inside a Blazor component. The error message will say something like:
Script tags should not be placed inside components because they cannot be updated dynamically. To fix this, move the script tag to the 'index.html' file or another static location. For more information see http://some/link
... and the help docs that we linked to will also say something like:
If you need to override this, add an attribute named "suppress-error" with value "BL9992" to the <script> tag, but only if you know what you are doing, as in most cases it would be a mistake to do this.