It’s indeed a URL problem.
Hugo renders my CSS file at http://localhost:1313/assets/css/post-hugo.css
.
In that CSS file I have:
/*# sourceMappingURL=post-hugo.css.map */
But that source map (http://localhost:1313/post-hugo.css.map
) gives a 404.
I can find the right one at http://localhost:1313/assets/css/post-hugo.css.map
in the browser (it shows a JSON file with the source files and more).
The Scratch variables figure out which CSS file to render based on the type of content:
<!-- Index CSS -->
{{ if .IsHome }}
{{ .Scratch.Set "scssPath" "assets/css/index.scss" }}
<!-- Page CSS -->
{{ else if eq .Section "" }}
{{ .Scratch.Set "scssPath" "assets/css/page.scss" }}
<!-- Category pages CSS -->
{{ else if .IsNode }}
{{ .Scratch.Set "scssPath" "assets/css/category.scss" }}
<!-- Post css -->
{{ else }}
{{ .Scratch.Set "scssPath" (print "assets/css/post-" .Section ".scss") }}
{{ end }}
{{ if .Site.IsServer }}
{{ $styles := resources.Get (.Scratch.Get "scssPath") | toCSS (dict "enableSourceMap" true) }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}">
{{ else }}
{{ $styles := resources.Get (.Scratch.Get "scssPath") | toCSS | minify | fingerprint "md5" }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}">
{{ end }}
I have those SCSS files in themes/theme2018/assets/assets/css/
. I deliberately used the /assets/
folder twice here. That way the CSS files appear at website.com/assets/css/
when rendered, which better matches my server setup.
Edit: In the Console tab of the Developer tools I get this warning message:
Source map error: SyntaxError: JSON.parse: bad escaped character at line 6 column 21 of the JSON data
Resource URL: http://localhost:1313/assets/css/post-hugo.css
Source Map URL: post-hugo.css.map[Learn More]
If I browse in the browser to the source map at http://localhost:1313/assets/css/post-hugo.css.map
, I see there the following:
{
"version": 3,
"file": "post-hugo.css",
"sourceRoot": "C:/site",
"sources": [
"themes\theme2018\assets\assets\css\post-hugo.scss",
"themes/theme2018/assets/scss/meta/_mixins.scss",
"themes/theme2018/assets/scss/meta/_variables.scss",
"themes/theme2018/assets/scss/parts/_reset.scss",
"themes/theme2018/assets/scss/parts/_general.scss",
"themes/theme2018/assets/scss/parts/_header.scss",
"themes/theme2018/assets/scss/parts/_content.scss",
"themes/theme2018/assets/scss/parts/_footer.scss",
"themes/theme2018/assets/scss/parts/_responsive.scss",
"themes/theme2018/assets/scss/contenttypes/_post.scss",
"themes/theme2018/assets/scss/syntax/_syntax.scss"
],
...
}
The line number that the console warns about contains this:
"themes\theme2018\assets\assets\css\post-hugo.scss",
I don’t know why the slashes in that URL are different from the other URLs listed under "sources"
in the JSON source map.
I also don’t know what the warning means. It looks like the browser can find the source map after all, but has a problem reading it.
Edit: If I change the slashes in the .scss
path the other way around, the problem is not fixed. So this does not help:
{{ .Scratch.Set "scssPath" (print "assets\\css\\post-" .Section ".scss") }}
(I’m on Windows by the way, if that’s relevant for how Hugo generates the source map paths.)