rstudio / blogdown

Create Blogs and Websites with R Markdown
https://pkgs.rstudio.com/blogdown/
1.73k stars 334 forks source link

Make Hugo's LiveReload work on RStudio Server #738

Open yihui opened 2 years ago

yihui commented 2 years ago

Unfortunately this doesn't work because I ran into an RStudio Server problem that I can't work around. The problem can be reproduced in the following steps. First create a new site and serve it:

install.packages('blogdown')
blogdown::new_site('test-site', serve = TRUE)

The default port is 4321. We can fetch livereload.js successfully from Hugo server via this port:

readLines('http://localhost:4321/livereload.js')
# [1] "!function(){return function e(t,o,n){...
# ...
# [34] "};"
# [35] "" 
# [36] "LiveReload.addPlugin(HugoReload)"

However, it can no longer be correctly loaded if we try to load it via the translated URL (I substituted random strings with xxxxxx):

u = rstudioapi::translateLocalUrl('http://localhost:4321/livereload.js', absolute = TRUE)
u
# [1] "https://xxxxxx.app.rstudio.cloud/p/xxxxxx/livereload.js"

readLines(u)
# [1] "<!DOCTYPE html>"
# [2] "<html lang=\"en\">"
# [3] "<head>"
# [4] "<meta name=\"author\" content=\"RStudio, PBC\">"
# [5] "<meta charset=\"utf-8\">"
# [6] ""
# [7] "<meta name=\"test:template\" content=\"login\">"
# [8] "<meta name=\"description\" content=\"Log in to RStudio.\">"
# [9] "<title>RStudio - Log In</title>"
# ...

It doesn't work either if we paste the URL in the web browser's address bar. By comparison, all other assets work well, e.g., https://xxxxxx.app.rstudio.cloud/p/xxxxxx/css/main.css. Also note that if we try to access livereload.js without the .js extension, we get a status code 400 (bad request) instead of the usual 404: https://xxxxxx.app.rstudio.cloud/p/xxxxxx/livereload

It seems that RStudio Server has a special routing mechanism for the filename livereload.js no matter if this file is served by which server (in this case, Hugo). This interference prevents Hugo's LiveReload from working. I wonder if this is a bug of RStudio Server.

cderv commented 1 year ago

@yihui I don't know fully the context here but just a reminder about https://github.com/rstudio/rstudio/issues/8096 that we added in https://github.com/rstudio/blogdown/pull/496

Slightly related, but maybe another issue as the RStudio IDE could have been using specific treatment, probably meant for Quarto which uses livereload.js I think.

Anyway, you're onto it!

yihui commented 1 year ago

Yes, I do remember https://github.com/rstudio/rstudio/issues/8096 well. That helped to some extent, and we have been "cheating" in the sense that we are refreshing the RStudio viewer by ourselves, instead of letting Hugo do it. This has a few limitations:

  1. It only works for R Markdown posts when users click the Knit button. It doesn't work with .md posts, and doesn't work when Rmd is automatically rendered to .md or .html on save. For this problem, I can provide an ugly workaround, though (watch for changes in .md/.html by myself and refresh viewer if necessary).
  2. Hugo's command-line option --navigateToChanged doesn't work. For this problem, I don't see any workaround on my side.

Originally Hugo had a bug (https://github.com/gohugoio/hugo/pull/6698) so there was no hope that its livereload.js could work on RStudio Server. It has been fixed later. Now I know how to make it work, but I ran into this RStudio Server bug and I don't see a possible workaround. Let's see if the Pro team could fix it: https://github.com/rstudio/rstudio-pro/issues/4012