9

Jekyll generates a static site in a given directory (by default, _site). Running jekyll serve builds the site and then sets up a server such that the site can be viewed locally on the specified port (e.g. localhost:4000 by default). I'm wondering if there is a way to activate this serve behavior without triggering the gem to recompile the site first.

Alternatively, it would be sufficient to use some other tool to serve the site from a localhost port without using jekyll, but I'm not sure how to do that (node.js?). While I can open the static files directly in a browser, this doesn't find all the relative url links (to css, etc) correctly, defaulting links such as /css/default.css to the root file://css/default.css instead, which of course does not exist there.

(This would be useful, for instance, because Jekyll takes quite some time to build a large site, and certain plugins I use need internet access to various APIs. It would be nice to view the site offline without triggering these).

2
  • If you just need a static file server, you can use node-static. But, if the site's still building, the files may not actually be available yet. Aug 5, 2013 at 22:04
  • Hmm.. Installs fine with npm, but when I try static in the site directory I get no console output and nothing visible at localhost:8080... not sure what I missed...
    – cboettig
    Aug 5, 2013 at 22:32

2 Answers 2

10
jekyll serve --skip-initial-build 

This will serve the site, skipping the initial build process. Additional configuration options for building and serving the site can be found here.

7

If you just want to serve an already built _site directory, there are any number of ways to quickly run a web server locally. With ruby you can just cd into _site and use WEBrick like so:

ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port=>4000,:DocumentRoot=>".").start'

or python's SimpleHTTPServer:

python -mSimpleHTTPServer 4000

Both these set the port to 4000, but that could be any number.

2
  • Perfect, thanks! Both worked like a charm for me. The WEBrick appears to be what jekyll is using internally too.
    – cboettig
    Aug 6, 2013 at 22:04
  • Running ruby 2.5.1 and WEBrick 1.4.2 on windows I found this didn't work as written. However after swapping the single and double quotes around it is working a charm i.e. ruby -rwebrick -e "WEBrick::HTTPServer.new(:Port=>4000,:DocumentRoot=>'.').start" May 18, 2018 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.