Skip to content

Commit 9606cbf

Browse files
committed
html-to-pdf: work around minified application.{css,js}
When Hugo minifies resources, it attaches an `integrity` attribute. This is usually very good! However, when we're trying to generate a `.pdf` from a HTML file that contains hrefs that contain absolute paths, as we do when deploying to GitHub Pages, we have to work around that by re-routing the files. Combined with an `integrity` attribute, this will fail, and there is no way around it other than editing out that attribute. So let's do just that. Hacky, but works, and we can proceed activities that are substantially more fun than battling well-intentioned-but-sometimes-really-unhelpful security measures without escape hatches (there is no way to tell Chromium that yes, this is intentional, but we need to ignore those integrity checks in this context, thank you for your cooperation). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent df355ea commit 9606cbf

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

script/html-to-pdf.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ const htmlToPDF = async (htmlPath, options) => {
4747
// this script runs before deployment, on a file:/// URL, where we need to
4848
// be a bit clever to give the browser the file it needs.
4949
const original = req.url()
50+
if (original === htmlPathURL) {
51+
// Work around rerouted `.css` and `.js` files... Symptom: "has an
52+
// integrity attribute, but the resource requires the request to be CORS
53+
// enabled to check the integrity, and it is not. The resource has been
54+
// blocked because the integrity cannot be enforced."
55+
const body =
56+
fs.readFileSync(htmlPath, "utf-8")
57+
// strip out the `integrity="sha256-..."` attributes
58+
.replace(/(\/application\.[^"/]+") integrity="sha256-[^"]+"/g, "$1")
59+
await route.fulfill({ headers: { "Content-Type": "text/html" }, body })
60+
return
61+
}
62+
5063
const url =
5164
original.startsWith(baseURLPrefix)
5265
? original

0 commit comments

Comments
 (0)