Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Hugo's LiveReload work on RStudio Server #738

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

yihui
Copy link
Member

@yihui yihui commented Nov 4, 2022

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.

…udio.cloud/p/hex, and use it as the baseURL, so that both livereload.js can be loaded (gohugoio/hugo#6698), and absolute URLs will work (this needes to be fixed because not all Hugo templates use the `relURL` function---some do generate absolute URLs, which will fail on RStudio server)
…RLs (ah, this is too complicated to explain...)
…t get around---RStudio Server seems to intefere with loading Hugo's livereload.js via the proxied port (the script can be read directly from the port, though)
@cderv
Copy link
Collaborator

cderv commented Nov 4, 2022

@yihui I don't know fully the context here but just a reminder about rstudio/rstudio#8096 that we added in #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
Copy link
Member Author

yihui commented Nov 4, 2022

Yes, I do remember rstudio/rstudio#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 (gohugoio/hugo#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants