Reloading template files as they change

I was going to ask how this could be done, but managed to dot it myself after a bit of searching and fumbling with npm. :muscle:

The short version is, put this stuff in a package.json file in the folder where you run caddy. Set the template path and run npm install, then npm watch and npm serve

{
  "name": "caddy-livetemplate",
  "author": "Ville Vanninen",
  "description": "Reloads caddy templates any time they change. Run both `npm run watch` & `npm run serve`. Combining them to `npm start` caused caddy to panic, that's why the separate commands.",
  "watch": {
    "reload": "server/pumpula6/template.html"
  },
  "scripts": {
    "watch": "npm-watch",
    "reload": "cat caddy.pid | xargs -I pidnum kill -s USR1 pidnum",
    "serve": "caddy -pidfile caddy.pid"
  },
  "license": "CC0",
  "devDependencies": {
    "npm-watch": "^0.1.8"
  }
}

There’s probably some simpler way to do this, but hey, I got this working. :slight_smile:

  1. The serve task tells caddy to run and output a pidfile so we can command it later.
  2. npm-watch watches the template file and runs the reload task whenever the template changes.
  3. The reload task reads the pidfile and commands the matching caddy instance to reload configs.

I tried doing a "start": "npm run serve & npm run watch" task in the scripts section, but caddy paniced when running that way. Anyone know why that could be?

1 Like

Looks neat to me!

What’d Caddy output when you tried this?

Here’s the full log and my caddyfile.

Parhelia:server sakamies$ npm start

> caddy-livetemplate@ start /Users/sakamies/Dropbox/Projects/sites/pumpula.net/server
> npm run watch & npm run serve


> caddy-livetemplate@ serve /Users/sakamies/Dropbox/Projects/sites/pumpula.net/server
> caddy -pidfile caddy.pid


> caddy-livetemplate@ watch /Users/sakamies/Dropbox/Projects/sites/pumpula.net/server
> npm-watch

Activating privacy features... done.
[reload] [nodemon] 1.11.0
panic: runtime error: index out of range

goroutine 9 [running]:
panic(0x59a720, 0xc420012190)
	/usr/local/go/src/runtime/panic.go:500 +0x1a1
github.com/mholt/caddy.trapSignalsPosix.func1()
	/tmp/custombuild_1790_518258033/src/github.com/mholt/caddy/sigtrap_posix.go:52 +0x79a
created by github.com/mholt/caddy.trapSignalsPosix
	/tmp/custombuild_1790_518258033/src/github.com/mholt/caddy/sigtrap_posix.go:83 +0x35

npm ERR! Darwin 16.4.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "serve"
npm ERR! node v6.2.2
npm ERR! npm  v3.9.6
npm ERR! code ELIFECYCLE
npm ERR! caddy-livetemplate@ serve: `caddy -pidfile caddy.pid`
npm [reload] [nodemon] clean exit - waiting for changes before restart
ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the caddy-livetemplate@ serve script 'caddy -pidfile caddy.pid'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the caddy-livetemplate package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     caddy -pidfile caddy.pid
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs caddy-livetemplate
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls caddy-livetemplate
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/sakamies/Dropbox/Projects/sites/pumpula.net/server/npm-debug.log

npm ERR! Darwin 16.4.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v6.2.2
npm ERR! npm  v3.9.6
npm ERR! code ELIFECYCLE
npm ERR! caddy-livetemplate@ start: `npm run watch & npm run serve`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the caddy-livetemplate@ start script 'npm run watch & npm run serve'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the caddy-livetemplate package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     npm run watch & npm run serve
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs caddy-livetemplate
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls caddy-livetemplate
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/sakamies/Dropbox/Projects/sites/pumpula.net/server/npm-debug.log
Parhelia:server sakamies$ ==> Successfully started `php56` (label: homebrew.mxcl.php56)
localhost:3000 {
  root pumpula6
  gzip
  status 404 /404
  rewrite {
    ext    /
    to     /index.php
  }
  markdown {
    template template.html
  }
  rewrite {
    regexp ^/p/apps
    to /p/apps/redir
  }
  redir /p/apps/redir http://apps.workflower.fi 301
  fastcgi / 127.0.0.1:9000 php
  startup brew services restart homebrew/php/php56
  shutdown brew services stop homebrew/php/php56
}

Well… @matt think this is a bug for the Github?

Yep, that’s a bug. Looks like initiating a reload before Caddy has “saved” a handle for the instance of the running server. I just replicated it locally and will have a fix out soon. Thanks!

1 Like

@sakamies I’ve fixed this on master: https://github.com/mholt/caddy/commit/c8514ad7b7329f348a3a9a9d9490a99f59847a9e

Feel free to give it a try. :slight_smile: If you reload Caddy before its config is loaded, it will just ignore the signal… because nothing to reload (yet).

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.