I’m running Hugo on Netlify, and I’ve stumbled on a weird bug with assets optimization.
I’m using Hugo’s built-in function which allows to use an integrity check attribute on my JavaScript assets. I also have assets optimization set to ON in my Netlify dashboard. When using absolute URLs in my website, everything is working fine. But when I use relative URLs, it seems that Netlify injects Cloudflare’s URLs and this breaks the integrity check, therefor creating a CORS problem (and blocking my site’s JavaScript).
The issue is probably not the trailing slash, but the leading slash. /posts points to the posts subdirectory at the top level of your content, or posts.html – depending on your set up. posts (without the leading slash) points to the posts directory or posts.html in whatever subdirectory you happen to be in, as mentioned above by @perry.
@gregraven the problem comes from the JavaScript assets and their integrity check, not the pages. When using Netlify’s assets optimization, they mess things with Hugo’s fingerprint, thus triggering a CORS violation.
Yup - when we optimize, we move to cloudfront, which won’t match your domain name. For most folks’ sites this isn’t a big deal, but clearly your settings are special. I use hugo with optimization myself and it’s no problem, but I use a BaseURL of / and presumably some other settings are different as well.
At any rate, since we won’t be changing how we process assets in the foreseeable future, you’ll want to not use our asset optimization in case you have strict requirements about URL’s. There is no version of our processing intended or available today that does NOT rewrite to cloudfront. If you’re trying to turn it off please make sure that “disable processing” is checked, rather than just unchecking the other boxes in that asset optimization section of our UI. Those are different - we’ll still rewrite some URL’s if you don’t completely disable our optimization!
Nothing we do is very fancy, you could do it yourself easily using npm modules that are potentially better than the ones we use. The workflow we use is something like this:
bundle CSS and JS appropriately (we make one bundle per “set” of files included in any other file - so it’s not “one bundle for all” but “commons.js + styles.js for most pages, commons.js+styles.js+special-for-one-page.js for the one-page, and no bundle if there is only one JS file used”)
if you want Pretty URL’s, we create redirects from /page/index.html to /page/ and also rewrite the referring html, js, and css to point to /page/ directly (in case you had links to /page/index.html)
Thanks a lot @fool for taking the time to write this thorough explanation. I’ll mark my problem as solved as it’s clearly a conflict between my Hugo settings and what Netlify is doing. In the end, no one’s to “blame”, it’s just something that’s a bit of an edge case. In fact, it might be useful to add this to Hugo’s documentation.
Anyways thanks again everyone for your help everyone!