Is there a way to have hugo --watch only rebuild certain files?

I like to visualize new posts on the browser as I write them. I found that hugo --watch and the live reloading is amazing for this! The only trouble is that my website has ~1,200 posts. While hugo is really fast, this still takes about 4 seconds for any changes to update on my current post.

Since I’m only concerned at live-reloading the current page I’m working on, I am wondering: is there a way to ignore / select a subset of pages for the live-reloading with --watch?

As far as I can tell, the only page that really changes when I edit the content of a single post is the index and the post page itself, so it would make sense to not regenerate the other thousand pages in my case.

If this feature doesn’t yet exist, I can move this thread to the “feature” forum for discussion.

We only load the changed file(s), so there are lots of “partial logic” in there. But we do render the entire site on every change. There are issues about this on GitHub. But it is a very hard problem to perfect. So until then I suggest you enjoy your 4 seconds turnaround. You may use the new metrics feature in the latest Hugo to tune your templates.

1 Like

@schollz, the parameter is --templateMetrics. I have almost 1,300 pieces of content, and live reloading happens faster than I can switch windows. I use a very minimal theme, so the difference between our sites is likely complex theme templates.

When Hugo 0.29 dropped I posted about it and included my metrics. Betcha can’t wait to see yours! :slight_smile:

1 Like

That is one fast theme, @maiki … The noHTTPCache is just relevant for the hugo dev server. I had this site with a rather big JSON search index which suddenly got very sluggish, and I noticed that Chrome downloaded the entire index on every page load instead of using the 304 Not Modified status.

1 Like

Thanks! What a useful flag! Here’s my results showing the culprit (testing on a Chromebook so its even slower than 4s):

Template Metrics:

     cumulative       average       maximum         
       duration      duration      duration  count  template
     ----------      --------      --------  -----  --------
  1m4.664290297s     192.749µs    186.9945ms   1244  theme/_default/single.html
  1m4.325399289s     -78.721µs  178.282125ms   1259  theme/partials/header.html
   1.010219877s   72.158562ms  198.443292ms     14  theme/_default/list.html
   622.267336ms    36.60396ms  217.511584ms     17  _internal/_default/rss.xml
   446.396124ms   29.759741ms  150.625416ms     15  theme/partials/middle.html
   193.062333ms  193.062333ms  193.062333ms      1  theme/index.html
   127.614958ms  127.614958ms  127.614958ms      1  _internal/_default/sitemap.xml
    16.275024ms      12.926µs    9.015708ms   1259  theme/partials/footer.html
      175.583µs     175.583µs     175.583µs      1  theme/404.html

Basically, I found that my theme/partials/header.html had a little for-loop where it iterated over one page and it had a bunch of meta tag stuff. I just removed all that. Now my metrics look like:

Template Metrics:

     cumulative       average       maximum         
       duration      duration      duration  count  template
     ----------      --------      --------  -----  --------
   772.794478ms     621.217µs    7.011375ms   1244  theme/_default/single.html
   528.447207ms   35.229813ms  187.487708ms     15  theme/partials/middle.html
   519.121459ms   30.536556ms  173.326125ms     17  _internal/_default/rss.xml
   367.224375ms   26.230312ms  189.066791ms     14  theme/_default/list.html
   170.164458ms  170.164458ms  170.164458ms      1  theme/index.html
   134.297042ms  134.297042ms  134.297042ms      1  _internal/_default/sitemap.xml
    64.269338ms      51.047µs    2.178167ms   1259  theme/partials/header.html
      6.57185ms       5.219µs    1.426541ms   1259  theme/partials/footer.html
      159.833µs     159.833µs     159.833µs      1  theme/404.html

So it got a 80x speedup from fixing my header (64 seconds to 0.8)! I expect the 4 seconds on my other computer will now take less than 50 milliseconds :slight_smile:

3 Likes