Skip to content

Static Assets and CDN Paths

Mike Christensen edited this page Aug 28, 2026 · 1 revision

Static assets and CDN paths

Serve local files with ASP.NET Core static-file middleware before Imp:

app.UseStaticFiles();
app.UseImp(/* ... */);

CDN-aware attributes

In an Imp template, prefix an attribute name with Cdn.:

<link rel="stylesheet" Cdn.href="/styles/site.css" />
<script Cdn.src="/scripts/site.js"></script>

The compiler removes Cdn. and transforms the value. With:

.CdnPrefix("https://cdn.example.com")

the rendered attributes use https://cdn.example.com/styles/site.css and the equivalent script URL.

The prefix is concatenated literally. Choose trailing/leading slashes consistently and test the result.

Final path callback

OnBuildCdnPath receives every CDN-aware path after prefixing:

var version = typeof(Program).Assembly.GetName().Version;

.OnBuildCdnPath(path => $"{path}?v={version}")

Use it for cache versions, content hashes, tenant paths, or another deterministic rewrite. It is called during template compilation, so values are effectively fixed for the process lifetime of that compiled page type.

The Todo sample deliberately uses an empty prefix plus a version callback. Assets stay local while demonstrating the same template syntax.

Security and correctness

  • Configure CDN origins; do not derive them directly from request input.
  • Use HTTPS.
  • Apply an appropriate Content Security Policy.
  • Preserve existing query strings when appending versions.
  • URL-encode dynamic URL components.
  • Set long cache lifetimes only for versioned/immutable assets.
  • Remember that Cdn.* rewrites template-authored attributes, not URLs written by dynamic methods.

Static files bypass Imp

Because UseStaticFiles runs first, successful file requests never reach Imp. Missing file requests continue and may become an Imp 404 page. This is expected pipeline behavior.

Clone this wiki locally