Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use a proxy server created by servr to proxy hugo server to correct U…
…RLs (ah, this is too complicated to explain...)
  • Loading branch information
yihui committed Nov 4, 2022
commit 9a5de8a2b2eefbbc36b5d565a321ec52a894ca21
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: blogdown
Title: Create Blogs and Websites with R Markdown
Version: 1.13.3
Version: 1.13.4
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Christophe", "Dervieux", role = "aut", email = "[email protected]", comment = c(ORCID = "0000-0003-4474-2498")),
Expand Down Expand Up @@ -67,3 +67,4 @@ Config/Needs/website: pkgdown, tidyverse/tidytemplate, rstudio/quillt, rstudio/w
Encoding: UTF-8
RoxygenNote: 7.2.1
SystemRequirements: Hugo (<https://gohugo.io>) and Pandoc (<https://pandoc.org>)
Remotes: yihui/servr
55 changes: 44 additions & 11 deletions R/serve.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,11 @@ serve_site = function(..., .site_dir = NULL) {
serve(..., .site_dir = .site_dir)
}

server_ready = function(url) {
server_ready = function(url, base = NULL) {
# for some reason, R cannot read localhost, but 127.0.0.1 works
url = sub('^http://localhost:', 'http://127.0.0.1:', url)
# need to tweak the url to include the subpath from RStudio translation
if (is_rstudio_server()) {
b = rstudioapi::translateLocalUrl(url)
url = paste0(sub('^(http://127[.]0[.]0[.]1:[0-9]+).*', '\\1/', url), b)
}
# need to include a subpath from RStudio translation
if (!is.null(base)) url = paste0(url, '/', rstudioapi::translateLocalUrl(base))
!inherits(
xfun::try_silent(suppressWarnings(readLines(url))), 'try-error'
)
Expand Down Expand Up @@ -116,10 +113,20 @@ serve_it = function(pdir = publish_dir(), baseurl = site_base_dir()) {

owd = setwd(root); on.exit(setwd(owd), add = TRUE)

rsv = is_rstudio_server()
if (rsv) baseurl = '' # don't use baseurl on RStudio Server

server = servr::server_config(..., baseurl = baseurl, hosturl = function(host) {
if (g == 'hugo' && host == '127.0.0.1') 'localhost' else host
})

if (rsv) {
port2 = servr::random_port(exclude = server$port)
server2 = servr::create_server(
port = port2, browser = FALSE, handler = proxy_handler(server$port, port2)
)
}

# launch the hugo/jekyll/hexo server
cmd = if (g == 'hugo') find_hugo() else g
host = server$host; port = server$port; intv = server$interval
Expand All @@ -133,9 +140,8 @@ serve_it = function(pdir = publish_dir(), baseurl = site_base_dir()) {
# http://localhost:4321, so we must use relativeURLs = TRUE:
# https://github.com/rstudio/blogdown/issues/124
tweak_hugo_env(
baseURL = if (is_rstudio_server())
rstudioapi::translateLocalUrl(server$url, TRUE),
relativeURLs = if (is_rstudio_server()) TRUE, server = TRUE
baseURL = if (rsv) rstudioapi::translateLocalUrl(server2$url, TRUE),
relativeURLs = if (rsv) TRUE, server = TRUE
)
if (length(list_rmds(pattern = bundle_regex('.R(md|markdown)$'))))
create_shortcode('postref.html', 'blogdown/postref')
Expand Down Expand Up @@ -183,7 +189,7 @@ serve_it = function(pdir = publish_dir(), baseurl = site_base_dir()) {
'Failed to serve the site; see if blogdown::build_site() gives more info.'
} else err, call. = FALSE)
}
if (server_ready(server$url)) break
if (server_ready(server$url, if (rsv) server2$url)) break
if (i >= get_option('blogdown.server.timeout', 30)) {
s = proc_kill(pid) # if s == 0, the server must have been started successfully
stop(if (s == 0) c(
Expand All @@ -198,7 +204,7 @@ serve_it = function(pdir = publish_dir(), baseurl = site_base_dir()) {
}
i = i + 1
}
server$browse()
if (rsv) server2$browse(TRUE) else server$browse()
# server is correctly started so we record the directory served
opts$append(served_dirs = root)
Sys.setenv(BLOGDOWN_SERVING_DIR = root)
Expand Down Expand Up @@ -307,3 +313,30 @@ refresh_viewer = function() {
server_wait = function() {
Sys.sleep(get_option('blogdown.server.wait', 2))
}

# port is hugo's port; port2 is servr proxy's port
proxy_handler = function(port, port2) {
# hugo's base url
b1 = rstudioapi::translateLocalUrl(sprintf('http://localhost:%d', port))
b2 = rstudioapi::translateLocalUrl(sprintf('http://localhost:%d', port2))
function(req) {
path = req$PATH_INFO
# for HTML pages, read their content; for other resources, redirect to hugo
if (grepl('[.]html?|/$', path)) {
tryCatch({
u = sprintf('http://127.0.0.1:%d/%s%s', port, b2, path)
list(
status = 200L, headers = list('Content-Type' = 'text/html'),
body = paste(readLines(u, encoding = 'UTF-8', warn = FALSE), collapse = TRUE)
)
}, error = function(e) {
list(
status = 404L, headers = list('Content-Type' = 'text/plain'),
body = paste0('Not found:', path)
)
})
} else {
servr::redirect(sprintf('/%s%s%s', b1, b2, path))
}
}
}