I’ve been struggling with this for some months now. When I’m generating content for blog posts I’d like to set draft: true in the frontmatter so I can preview the content locally or on netlify using --buildDrafts.
However, this results in a build error for any post content were the draft is set to true:
This error does not occur if draft: false on the same content and the --buildDrafts flag excluded. It renders correctly with the hero image etc (where the above message suggest there may be an issue).
My workaround has been to set the publish date to some time in the future and use --buildFuture instead. But that’s caused the inevitable issue I had today - some content was inadvertently published… The date wasn’t far enough in the future!
Any ideas what might cause this or where to look? I guess it’s a theme issue but I can’t understand how draft: false works fine and draft: true doesn’t when --buildDrafts is used.
Thanks - yeah that’s what I thought, but the image is there and shows/renders as expected when draft=false and --buildDraft flag isn’t used.
So how can doing the alternative (draft=true & --buildDraft) break things? I don’t understand what, in the template, would be processed differently with those two options flipped.
Further thinking out loud - I deleted all other content files that had draft: true so that just one post existed. I also built using the debug loglevel flag, trying to see if the location of the error changed.
This was trying to rule out another draft bit of content existing with an image that didn’t exist.
No change. So it builds post by post, reaches the one with draft:true and fails. Setting to draft: false builds fine.
neither of these suggestions directly solve your problem, but might be a workaround:
set your date in the future and draft: false, then use --buildFuture instead for the preview.
use a CI that allows for the non-master branches to be previewed, for example the Cloudflare Pages integration with Hugo gives you a unique pages.dev link for every commit.
Yeah - that’s what I’ve been doing which led to the problem (inadvertently posting something via an automated, regular CI build once the date changed to the preset future published date), and hence trying to get a proper solution where the draft status would have prevented that.
I’ve a non-master branch for all preview/dev builds, and do a PR to master once all looking good and scheduled appropriately. The non-master builds are the ones where I want --buildFuture and --buildDrafts to work as intended.
Yeah, I will do - if it is indeed a theme issue. Although the theme has been heavily customized and extended - included the partial thrown up in the error…
I still don’t understand why it’s only the --buildDrafts flag that causes the issue. I don’t see how something in a partial within a theme would build fine non-draft, but not when marked draft and using --buildDrafts. Nothing changes bar one line of frontmatter and the hugo command option.
But typing this made me want to go and check if there’s a “if not production” region of that partial. Is there a “if draft” construct?
I’m not a big fan of guessing. To avoid guessing, I need access to the project repository. But if the terms of your commerical theme purchase preclude you from sharing souce, I’m not sue where that leaves us.
Which is why I encouraged you to contact the vendor.
In parallel I’ll try and make a MWE using the new ‘basic site’ thing I read about, and grafting on the relevant templates / partials. Doing that may expose somewhere I’ve introduced an error.
The line {{ $background_image = $background_image.Fill $cropAndAnchor }} is indicated in the error. And from reading around the nil pointer could suggest the line above isn’t returning the image location when in draft?
Following up after some time to close this off - the issue turned out to be some legacy content (posts or pages marked as draft) amongst the numerous ‘new’ content files that had frontmatter values which broke the build when building the draft content.
Deleting the legacy content resolved the issue. Perhaps a lesson in keeping your repo clean and tidy…