yihui/servr

Integrate reload or similar

hadley opened this issue · 11 comments

(e.g. https://www.npmjs.com/package/reload) - so you don't have to refresh the browser, it does it automatically when the file changes

cc @jjallaire

Another option is http://www.livejs.com - you'd need to inject the js header into each page. Not sure how hard that is to do in general. You'd also need to set up the server to send/interpret the correct Last-Modified and/or ETags headers

Thanks! I will consider this when it is expensive to refresh the page. At the moment, it does not seem to hurt much to refresh the whole page.

It's not expensive computationally, but it's expensive cognitively because you have to remember.

Currently I have WebSockets listening on all HTML pages (this is injected into every page: https://github.com/yihui/servr/blob/master/inst/resources/ws-reload.html), and the current page will automatically refresh itself if R Markdown files have been rebuilt (which also happen automatically). So there is nothing to remember -- just open the R Markdown document, fire up a server, and start writing.

Oh, you're already doing it, sweet! Does this work with a general make based workflow (i.e. looking at the timestamps of the generated html)?

Yes, the timestamp of foo.html is compared to that of foo.Rmd. If the latter is newer, I will just rebuild foo.Rmd and foo.html, then tell foo.html to reload itself.

But that assumes you're using .Rmd? In a more general make based process, you might be using .md or something else. What if the websocket just returned the timestamp of the html file? Then the js could cache that when it was loaded, and then reload if it changes.

You have more freedom if you use Makefile. In that case, I do not care if it is .Rmd or .md, or even any other dependencies of .html, all I look at is the exit status of make -q. The page is reloaded only if make -q returns non-zero exit code. That is, it is totally up to you to define the rules of updating the site.

Ah, ok, that sounds perfect!

One caveat, though: when foo2.html is recompiled from foo2.Rmd, foo1.html may refresh itself as well if it is the current page loaded in the web browser, i.e. refreshing the browser is irrelevant to which specific output file is re-generated.

%.html: %.Rmd
    Rscript -e "rmarkdown::render('$^')"

I guess this is no big deal.

Yeah, that doesn't sound like a big problem to me