dotnet/AspNetCore.Docs · error · Error
Must provide a non-empty value for the "src" attribute.
Error message
Must provide a non-empty value for the "src" attribute.
What it means
JavaScript guard throw inside registerPageScriptElement: if the 'src' argument is falsy (undefined, empty string, null) it throws because the page-script reference-counting map keys modules by src and cannot register an entry without a unique script URL. Used by the Blazor PageScript custom element helper that manages JS modules across navigations.
Source
Thrown at aspnetcore/blazor/includes/js-interop/blazor-page-script.md:6
```javascript
const pageScriptInfoBySrc = new Map();
function registerPageScriptElement(src) {
if (!src) {
throw new Error('Must provide a non-empty value for the "src" attribute.');
}
let pageScriptInfo = pageScriptInfoBySrc.get(src);
if (pageScriptInfo) {
pageScriptInfo.referenceCount++;
} else {
pageScriptInfo = { referenceCount: 1, module: null };
pageScriptInfoBySrc.set(src, pageScriptInfo);
initializePageScriptModule(src, pageScriptInfo);
}
}
function unregisterPageScriptElement(src) {
if (!src) {
return;
}
View on GitHub (pinned to c67a80103a)
Solutions
- Always supply a non-empty, resolvable script path on the PageScript src attribute.
- Validate src before binding: only render the PageScript element when the URL is present.
- Default src to a known module path when configuration is absent.
- Add a unit/runtime assertion that src is a non-empty string before calling register.
Example fix
// before
function registerPageScriptElement(src) {
if (!src) {
throw new Error('Must provide a non-empty value for the "src" attribute.');
}
// ...
}
// after — caller-side guard before rendering the element
@if (!string.IsNullOrWhiteSpace(ScriptSrc))
{
<PageScript Src="@ScriptSrc" />
}
else
{
<div class="text-warning">Script source not configured.</div>
} Defensive patterns
Strategy: validation
Validate before calling
// Guard before calling register
if (typeof src !== 'string' || src.trim() === '') {
console.warn('PageScript src is empty; skipping registration.');
return;
} Type guard
function isValidSrc(src) { return typeof src === 'string' && src.trim().length > 0; } Try / catch
try { registerPageScriptElement(src); }
catch (e) {
console.error('Failed to register page script:', e.message);
} Prevention
- Always bind a concrete, non-empty src on PageScript elements.
- Conditionally render the element only when src is set.
- Default src from configuration when missing.
- Validate src at the call site before registering.
When it happens
Trigger: A <script type="text/javascript" src=""> or a PageScript element whose src attribute resolves to empty; calling registerPageScriptElement('') programmatically; an attribute binding that evaluates to undefined at render time.
Common situations: Dynamic src binding in a Razor template that is sometimes empty; copy/paste of the PageScript element with the src omitted; build/bundler stripping the src; attribute set from a configuration value that is missing.
Related errors
- QR code size must be less than {MaxQrSize}.
- Unknown command: ${e.data.command}
- Validation failed. Status Code: {response.StatusCode}
- When calling ko.fromJS, pass the object you want to convert.
- When calling ko.mapping.toJS, pass the object you want to co
AI-assisted analysis of dotnet/AspNetCore.Docs@c67a80103a (2026-08-13).
Data as JSON: /api/errors/f20f953f3ca69657.
Report an issue: GitHub.