Automatically start browser on localhost:4000 after `jekyll serve`

Dear colleagues:

I’m starting to use make to steer my jekyll work, and find it saves much typing. One specific target is make serve. There after bundle exec jekyll serve --livereload I want to start the browser on port 4000 when the building is done. So far I do the following:

serve s:

(sleep 5; open “http://localhost:4000/index.html”) &
bundle exec jekyll serve --livereload

This allows jekyll to build and enter serve mode, while in the background the browser is started after 5 s. This is not fully satisfactory since the 5 seconds are frequently not enough.
Better ideas?

PS: For those (like me!) to very fluent in shell speak: the parentheses form a “command group”, allowing the 2-command sequence to be converted to a background job via the ampersand symbol.

Recent versions of Jekyll have the -o/--open-url option [corrected dashes] that will automatically open a browser window once the site is running.

2 Likes

Thank you! Great! (Double dash, BTW). Now my make command reads

bundle exec jekyll serve --livereload --open-url http://localhost:4000/

Totally solves my problem. I’m sure this option will soon enter the documentation.

Thanks, best, Michael

Ah, you’re right, the option isn’t documented on the site. It does appear in the command-line help (jekyll help serve). I’ve sent in a pull-request to fix the site.

The --open-url option doesn’t take an argument, so the http://localhost:4000/ is unnecessary. To set the host and port, there are --host/--port options. For example, to serve the site on the local net from the computer named “Eddy” on port 8080:

bundle exec jekyll serve --livereload --open-url --host eddy.local --port 8080
# Browser opens to http://eddy.local:8080

Thank you again! I was not aware that jekyll -h could be augmented with a subcommand! Now I see the many options, also for the other subcommands. And I’ve left off the URL.
Interesting: while --livereload can be placed into _config.yml as livereload: true, this does not seem to be the case for --open-url.

This great community is a major asset of jekyll!

Best, Michael

I like to use Firefox Developer edition when I’m building stuff but the regular edition when I’m just surfing. I’ve installed the npm package parallelshell globally and have an alias defined with parallelshell "bundle exec jekyll serve --livereload" "~/.local/share/firefox_dev/firefox 'http://localhost:4000/'"

The only thing to know with this is when you kill the server with Ctrl+C, it closes the browser.

2 Likes

Whelp, that’s awesome and simple. Thanks for sharing!

Again a juseful pearl. Thank you!

thank you for posting this. Works great. I now use
alias jekyllserver="jekyll serve --host 0.0.0.0 --open-url"
and this leaves the tab open in safari when I stop the jekyll serve command. reload is done by watch.rb script.

Great. Yet, as we learned from chuckhoupt, the argument http://localhost:4000/is not necessary.

excellent - edited the alias. works as advertised. thanks

Another approach is that will ctrl click on the URL in the console after start and that will open in the browser

If your terminal or IDE terminal supports links

The pull request landed, so now the Open-URL option and flag are documented (along with several other missing build and serve flags):

1 Like

great! Found even more new useful options. Thanks.