When deploying a Hugo site (specifically using the Blowfish theme) to Cloudflare Pages, you might encounter an issue where the site loads without any styling (CSS). The browser console often shows an error related to Subresource Integrity (SRI) check failure.
The Problem #
The site looks “garbled” or unstyled. Checking the browser’s developer console (F12) reveals errors like:
Failed to find a valid digest in the ‘integrity’ attribute for resource ‘…’ with computed SHA-512 integrity ‘…’. The resource has been blocked.
The Cause #
This happens because of Cloudflare’s Auto Minify feature.
- Hugo generates the CSS/JS files and calculates their cryptographic hash (SRI), adding it to the
integrityattribute in the HTML. - Cloudflare Pages (if Auto Minify is enabled) modifies these files to reduce their size.
- The Browser downloads the modified file, calculates its hash, and compares it with the one in the HTML. Since the file changed, the hashes don’t match, and the browser blocks the resource for security reasons.
The Solution #
Since Cloudflare deprecated the Auto Minify feature in August 2024, the previous recommendation of disabling it is no longer applicable.
The primary solution now is to remove the integrity attributes from your Hugo templates. This prevents the browser from blocking the resources due to hash mismatches caused by Cloudflare’s edge optimization or compression.
Remove Integrity Attributes #
You need to override the theme’s template to remove the integrity attribute from CSS and JS tags.
Steps:
-
Copy the Template: Copy the
head.htmlpartial from the theme to your project’s layouts folder:themes/blowfish/layouts/partials/head.html->layouts/partials/head.html -
Edit the File: Open the new
layouts/partials/head.htmlfile. -
Remove Integrity Attributes: Find all
<link>and<script>tags that have anintegrityattribute and remove theintegrity="{{ ... }}"part.Example (CSS):
Change this:
<link type="text/css" rel="stylesheet" href="{{ $bundleCSS.RelPermalink }}" integrity="{{ $bundleCSS.Data.Integrity }}">To this:
<link type="text/css" rel="stylesheet" href="{{ $bundleCSS.RelPermalink }}">Example (JS):
Change this:
<script src="{{ $js.RelPermalink }}" integrity="{{ $js.Data.Integrity }}"></script>To this:
<script src="{{ $js.RelPermalink }}"></script> -
Rebuild: Rebuild your site. The resources will now load without integrity checks, resolving the issue.