datadesk/baker

Watch: "Templates failed to compile" every time I update HTML

Closed this issue · 7 comments

Hello! I'm trying out baker and really enjoy it, so far, but I'm running into a problem.

When I run npm run start I get a normal successful dev server. When I update any of my templates, I am greeted with the templates failed to compile message. So my baker workflow is forced to be: make change, kill dev server, restart dev server. No errors happen when I run the npm run build, only the watcher change. This is the error:

Templates failed to compile.

(/Users/brandon/Desktop/data_scrape/code/baker-example-page-template/_layouts/county.html) [Line 16, Column 26]
  attempted to output null or undefined value
  15 |     <h1>{{ meta.headline }}</h1>
  16 |     <h2 class="headline">{{ county.name }}</h2>
> 17 |     <p class="description">
     |                           ^

I've tried to narrow down the issue and it looks like it's related to the outputFile function being called without a localContext upon watcher fire. Does the dev server watcher work with createPages?

So I was able to get around this by hacking in a custom watcher on the NunjucksEngine class that basically does what build does but just for the changed path in question. Then calling that instead of this.nunjucks.watch in lib/index.js.

It looks like this.nunjucks.watch there actually just runs the standard template watcher which doesn't pass in any of the context needed to render the page. So baker, upon build, it's building the createPages templated files fine, but then when the watcher fires, it's handling it like a normal template without any context. It's still very unclear to me if this is due to some environment error, a baker config issue or some other issue on my end.

You've put your finger on a key problem users encounter here. I'd love any patches or proposals you have for sorting things out.

So I have this branch (one commit) that I've been working with. I don't know enough about the baker codebase yet to be confident this would be appropriate for a PR, but maybe you could let me know of any immediate pitfalls of what I'm doing are?

main...brandonrobertz:baker:createpages-dev-fix

I'm gonna keep testing this as I build things out. I guess I could also pull some example repos and see how those work too.

It scans like an interesting attempt at solution to me. Let's try it!

In addition to running the server locally, you can try running our test suite against it. I'd also suggest experimenting with an error case where the data values being supplied to the template are wrong. That's the biggest pain point I hear about from users.

Great, thanks for the input! I noticed the rollup engine also uses a custom watch routine, so I can use that as a reference for a custom watch on nunjucks. I'll see if I can't get a test case going that demonstrates some of this. I think that's a good idea.

Opened a PR on this #1580

I've released the patch for this ticket with version 0.45.0.

https://www.npmjs.com/package/@datagraphics/baker

Give it a shot and please report any bugs you encounter.

Thank you to @brandonrobertz for the fix.