Skip to content

StaticScript

Inherits: StaticComponent HTML Target: <script static-script>

StaticScript is a tag helper that targets <script> elements with the static-script attribute. It provides advanced script management features: teleporting scripts to a consolidated location in the page and rendering scripts only once even when a component is used multiple times.

TechGems.StaticComponents
<script static-script>
// Your JavaScript here
</script>

The static-script attribute is required to activate this tag helper on a <script> element.

Without any additional attributes, the script renders inline at the component’s location:

<script static-script>
console.log("Hello from the component");
</script>

Move the script to the location of <render-static-scripts /> (typically near the end of the <body>):

<script static-script teleport-script>
console.log("Rendered at the bottom of the page");
</script>

Ensure the script renders only a single time, even if the component appears multiple times on the page:

<script static-script render-once="@Model">
console.log("Only rendered once");
</script>

Teleport and render once:

<script static-script teleport-script render-once="@Model">
// Teleported to the bottom and only rendered once
</script>
[HtmlAttributeNotBound]
public HttpContext HttpContext { get; }

Provides direct access to the current HttpContext. Derived from ViewContext.HttpContext. This property is not settable from HTML attributes.

[HtmlAttributeName("teleport-script")]
public bool TeleportScript { get; set; }

When true, the script is not rendered inline. Instead, it is stored in HttpContext.Items and rendered later at the location of a <render-static-scripts /> tag helper.

AttributeValue
HTML attributeteleport-script
Defaultfalse
[HtmlAttributeName("render-once")]
public object? RenderOnce { get; set; }

When set, the script will only render once per page for the given component type, regardless of how many times the component is used. The value should be an instance of the component type — its type name is used as the deduplication key.

AttributeValue
HTML attributerender-once
Defaultnull (renders every time)
[HtmlAttributeName("disable-render")]
public bool DisableRender { get; set; }

When true, the script is not rendered at all. Useful for conditionally suppressing scripts, for example during AJAX partial responses where the script is not needed.

AttributeValue
HTML attributedisable-render
Defaultfalse
TeleportScriptRenderOnceDisableRenderResult
falsenullfalseScript renders inline at the component location.
truenullfalseScript is teleported to <render-static-scripts />.
falsesetfalseScript renders inline, but only on the first occurrence.
truesetfalseScript is teleported, but only on the first occurrence.
anyanytrueScript is not rendered.