Using Hugo server command to make a site accessible on your local network with options --bind and --baseUrl

What a brilliant feature and I only discovered this the other day so thought I’d post it here in case it helps others to see a real example.

If you want to test your site on other devices on your local network you can use the following:

hugo server -D --bind 192.168.X.Y --baseUrl http://192.168.X.Y

Replace the X & Y with the actual numbers of your host machine’s IP and when your site builds it will be available to your local network on 192.168.X.Y:1313 rather than the usual localhost:1313 which is inaccessible to other machines.

A real time saver if you were having to build on Netlify in order to test your site on say an iPad and/or smart phone.

4 Likes

@codingforfun thanks for the info! I put that in a bash shell for ease-of-use. I’m on MacOS hugo v0.124.1 …

#!/bin/bash

# Set the local IP address to the variable ip
ip=$(ifconfig en0 | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}')

# Start hugo
hugo server -D --bind $ip --baseURL http://$ip
1 Like

I have a Makefile that contains a wee bit of shell to do the same. I do like trying to find minimal pipes to achieve something too. Step up awk:

int := $(shell route -n get default | awk '/interface: / {print $$2}')
ip := $(shell ifconfig $(int) |awk '/inet / {print $$2}')

server:
	@hugo server -D --bind $(ip) -b "http://macbook.local"

This is on macOS (obvs), and works out the default interface before getting its IP :smile: