Search code examples
rblogdown

r blogdown - build_site result does not matched preview from serve_site


I create a new blog with blogdown, but somehow i seem to make a mistake as the site i built does not match the preview from serve_site.

dir.create("blog7")
setwd("blog7")
blogdown::new_site(theme = "shenoybr/hugo-goa")
blogdown::serve_site()
blogdown::build_site()
browseURL(url = "public/index.html")

The links do not work as expected and the image is not shown. The image seems to work if i change: /img/headshot.jpg to ./img/headshot.jpg. The links dont seem to be relative.

How can i avoid These issues to appear and avoid editing the result in public manually?

enter image description here


Solution

  • Maybe you have some problem with baseURL settings in config.toml.

    • When running blogdown::serve_site(), baseURL setting is overriden by "/" to be served locally under localhost.
    • For blogdown::build_site() you need to set up a real base URL of your blog (i.e. "https://www.example.com/") or at least "/".

    Note that if you build your site with blogdown::build_site() and some settings and then try to open that HTML from local public folder, it will not work as expected. Just because the URLs are built from baseURL and point e.g. to /img/headshot.jpg (which does not make sense locally, but works well on the web like https://blog.example.com/img/headshot.jpg.

    So:

    • if you want to preview your site locally with localhost, use blogdown::serve_site(),
    • for exporting the site to the web, use blogdown::build_site(), but note that those generated HTML files will not work properly when running locally.

    See more details in the book about blogdown and some info about config.toml.