A CDN (Content Delivery Network) places copies of your static assets (images, stylesheets, scripts, fonts, and documents) on edge servers distributed around the world. When a visitor requests your page, those assets are served from the nearest edge location instead of from the origin server. The result is faster load times, lower server load, and a better experience for your users, no matter where they are.
The CDN is configured under Settings > System > Web & HTTP > CDN, with three settings:
- Enable img and script src replacement: turns the CDN on; nothing is rewritten while it is off
- Host: the CDN host for static files under
/Files/, e.g. stylesheets, scripts, images, and documents - Host (GetImage.ashx): the CDN host for image handler requests to
/Admin/Public/GetImage.ashx
Both hosts must start with https:// or http://.
DynamicWeb CDN packages
Dynamicweb offers two CDN packages for hosted solutions. Both are powered by Cloudflare's global edge network.
| Package | What you get | Best for |
|---|---|---|
| Accelerate (Static CDN) | Global edge delivery of all static assets (images, CSS, JS, documents, video). Every hosted solution gets its own endpoint: https://{siteid}.cdn.dynamicweb.cloud |
Every hosted solution, as the default performance layer |
| Accelerate + Protect (Secure CDN) | Everything from Accelerate, plus a Web Application Firewall (WAF), DDoS and bot protection, rate limiting, threat filtering, and custom security rules at the edge | High-traffic, regulated, or security-sensitive solutions, e.g. finance, government, healthcare, B2B portals |
Note
The Accelerate package is covered in detail below. To learn more about the Accelerate + Protect package, contact Dynamicweb.
How CDN works in Dynamicweb
When CDN is enabled, Dynamicweb automatically scans the rendered HTML output and prepends the CDN host URL to static asset paths. This happens after page rendering, so no template changes are needed for standard usage.
The two hosts are used for different URLs. You can use the same CDN URL for both, but you must fill in both fields: the Host never rewrites image handler URLs, so if Host (GetImage.ashx) is empty, resized images are still served from the origin server.
The rewriting runs once, on the finished page. It is not applied to feeds and JSON output, or to markup added afterwards by notification subscribers, e.g. Standard.Page.AfterOutput.
What gets rewritten automatically
| HTML element | Attribute | Path pattern | CDN host used |
|---|---|---|---|
<img> |
src |
/Files/... |
Host |
<link> |
href |
/Files/... |
Host |
<script> |
src |
/Files/... |
Host |
<img> |
src |
/Admin/Public/GetImage.ashx?... |
Host (GetImage.ashx) |
The match is case-insensitive and works with or without the leading slash, e.g. Files/.... /Files/ URLs are rewritten in both single and double quotes, but image handler URLs are only rewritten reliably in double quotes, so always write src="..." for GetImage.ashx images.
Before CDN:
<img src="/Files/Images/hero.jpg" />
<link href="/Files/Templates/Designs/mysite/css/style.css" rel="stylesheet" />
<script src="/Files/Templates/Designs/mysite/js/app.js"></script>
After CDN (with https://mysite.cdn.dynamicweb.cloud):
<img src="https://mysite.cdn.dynamicweb.cloud/Files/Images/hero.jpg" />
<link href="https://mysite.cdn.dynamicweb.cloud/Files/Templates/Designs/mysite/css/style.css" rel="stylesheet" />
<script src="https://mysite.cdn.dynamicweb.cloud/Files/Templates/Designs/mysite/js/app.js"></script>
What is NOT rewritten automatically
The automatic replacement only works on src and href attributes in the rendered page output. It does not cover:
- URLs inside CSS files: e.g.
background-image: url(/Files/Images/bg.jpg) - URLs inside JavaScript files: e.g. dynamically constructed image paths
srcset,styleattributes, and other attributes that do not end insrcorhref- Other elements, e.g.
<source>,<video>, and<picture> - More than one URL per element: only the first matching attribute in a tag is rewritten, so in
<img src="/Files/a.jpg" data-src="/Files/b.jpg">onlysrcchanges - URLs that already include a domain: external resources are left untouched
Attributes whose name ends in src or href, e.g. data-src, are rewritten when they are the first match in an <img>, <link>, or <script> tag. Do not rely on this; reference the CDN host explicitly as shown below.
See Manually referencing the CDN host in templates and Handling assets inside CSS and JavaScript below for how to handle these cases.
Manually referencing the CDN host in templates
In some cases, you may need to reference the CDN host directly in your Razor templates, for example, when using data-src for lazy-loading, srcset for responsive images, or inline style attributes.
Dynamicweb exposes the configured CDN hostnames through the Pageview object:
Pageview.CdnHostName: the general CDN host (includes a trailing slash)Pageview.CdnHostNameForImage: the GetImage-specific CDN host (includes a trailing slash)
Both return null if the CDN is not enabled, or if the host is empty or does not start with http:// or https://. Because the value already ends with a slash, append paths without a leading slash, as in the examples.
Examples
Lazy-loaded image with data-src:
@{
var cdnHost = Pageview.CdnHostName ?? "/";
}
<img data-src="@(cdnHost)Files/Images/hero.jpg" class="lazyload" alt="Hero image" />
Inline background image:
@{
var cdnHost = Pageview.CdnHostName ?? "/";
}
<div style="background-image: url('@(cdnHost)Files/Images/background.jpg')"></div>
Responsive image with srcset:
@{
var cdnImageHost = Pageview.CdnHostNameForImage ?? "/";
}
<img
src="@(cdnImageHost)Admin/Public/GetImage.ashx?Image=/Files/Images/photo.jpg&Width=800"
srcset="@(cdnImageHost)Admin/Public/GetImage.ashx?Image=/Files/Images/photo.jpg&Width=400 400w,
@(cdnImageHost)Admin/Public/GetImage.ashx?Image=/Files/Images/photo.jpg&Width=800 800w,
@(cdnImageHost)Admin/Public/GetImage.ashx?Image=/Files/Images/photo.jpg&Width=1200 1200w"
sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"
alt="Photo" />
Handling assets inside CSS and JavaScript
Since CDN replacement operates on the HTML output, URLs inside CSS or JavaScript files are not rewritten. This is a common source of errors, for example, a background-image in your stylesheet or a font @font-face URL will still point to the origin server.
Here are practical ways to handle this:
1. Use relative URLs in CSS files
If your CSS file is served from /Files/ (and therefore itself delivered via CDN), you can use relative paths inside it. Since the browser resolves relative URLs against the CSS file's own location, they will automatically point to the CDN:
/* style.css is at /Files/Templates/Designs/mysite/css/style.css */
/* This will resolve relative to the CDN host, since style.css itself is served from CDN */
.hero {
background-image: url(../images/hero.jpg);
}
@font-face {
font-family: 'MyFont';
src: url(../fonts/myfont.woff2) format('woff2');
}
Tip
This is the recommended approach. As long as the CSS file and the referenced assets are both under /Files/, relative URLs will "just work" through the CDN, with no extra configuration needed.
2. Write background images from the template
For background images that depend on content, e.g. a hero image editors choose, write the style attribute in the template. Inline styles are not rewritten automatically, so prepend the CDN host yourself:
@{
var cdnHost = Pageview.CdnHostName ?? "/";
}
<div style="background-image: url('@(cdnHost)Files/Images/hero.jpg')"></div>
Troubleshooting
| Symptom | Possible cause | Fix |
|---|---|---|
| CDN replacement not working | CDN host URL does not start with https:// or http:// |
Correct the URL format; it must include the protocol prefix |
| CDN replacement not working | Enable img and script src replacement is off | Enable it in the CDN settings |
| Resized images not served from CDN | Host (GetImage.ashx) is empty | Fill in the GetImage host; the main host does not cover image handler URLs |
| Some images not served from CDN | Images use srcset, <source>, or a second attribute such as data-src after src |
Use Pageview.CdnHostName in your template to manually prepend the CDN host |
| Background images still loading from origin | CSS url() references are not rewritten |
Use relative URLs in your CSS file, or set the CDN host via Razor (see above) |
| Mixed content warnings in the browser | CDN host uses http:// but the website uses https:// |
Change the CDN host URL to use https:// |
Links to files, e.g. PDFs in <a href>, point to the CDN |
The replacement starts at an <img>, <link>, or <script> tag whose own URL does not match, e.g. an external image, and continues to the next src or href in the markup |
Expected behavior of the replacement; make sure the CDN serves those files too, or reference them with an absolute origin URL |