Skip to content

Commit b982be6

Browse files
Copilotrrrutledge
andcommitted
Make 'hugo server' work automatically in both Codespaces and local environments
Co-authored-by: rrrutledge <9609562+rrrutledge@users.noreply.github.com>
1 parent 7526abe commit b982be6

8 files changed

Lines changed: 101 additions & 30 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
}
3333
},
3434
"remoteUser": "vscode",
35-
"postCreateCommand": "npm install; cd /tmp && wget -O hugo.deb https://github.com/gohugoio/hugo/releases/download/v0.133.1/hugo_extended_0.133.1_linux-amd64.deb && sudo dpkg -i hugo.deb && rm hugo.deb && npm init -y && npm install hugo-bin@latest && hugo version && echo 'Hugo server ready to run! Use the Run and Debug view to start the server.'"
35+
"postCreateCommand": "npm install; cd /tmp && wget -O hugo.deb https://github.com/gohugoio/hugo/releases/download/v0.133.1/hugo_extended_0.133.1_linux-amd64.deb && sudo dpkg -i hugo.deb && rm hugo.deb && npm init -y && npm install hugo-bin@latest && sudo mv /usr/local/bin/hugo /usr/local/bin/hugo-real && sudo cp ${containerWorkspaceFolder}/.devcontainer/hugo-wrapper.sh /usr/local/bin/hugo && sudo chmod +x /usr/local/bin/hugo && hugo version && echo 'Hugo server ready! You can now run: hugo server'"
3636
}

.devcontainer/hugo-wrapper.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
3+
# Hugo Wrapper Script for Codespaces
4+
# This script wraps the hugo command to automatically set the correct baseURL in Codespaces
5+
6+
# Path to the real hugo binary
7+
HUGO_BIN="/usr/local/bin/hugo-real"
8+
9+
# Check if we're running 'hugo server' command
10+
if [[ "$1" == "server" ]]; then
11+
# Check if we're in GitHub Codespaces
12+
if [ -n "$CODESPACE_NAME" ]; then
13+
# Construct the Codespaces URL
14+
BASE_URL="https://${CODESPACE_NAME}-1313.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
15+
16+
# Check if --baseURL is already provided in arguments
17+
has_baseurl=false
18+
for arg in "$@"; do
19+
if [[ "$arg" == --baseURL=* ]] || [[ "$arg" == "--baseURL" ]]; then
20+
has_baseurl=true
21+
break
22+
fi
23+
done
24+
25+
# If no baseURL is provided, add our Codespaces URL
26+
if [ "$has_baseurl" = false ]; then
27+
echo "🚀 Running Hugo in Codespaces mode"
28+
echo "📍 Base URL: $BASE_URL"
29+
30+
# Check if --bind is already provided
31+
has_bind=false
32+
for arg in "$@"; do
33+
if [[ "$arg" == --bind=* ]] || [[ "$arg" == "--bind" ]]; then
34+
has_bind=true
35+
break
36+
fi
37+
done
38+
39+
# Build the command with automatic Codespaces configuration
40+
if [ "$has_bind" = false ]; then
41+
exec "$HUGO_BIN" "$@" --baseURL="$BASE_URL" --bind=0.0.0.0 --appendPort=false --liveReloadPort=443
42+
else
43+
exec "$HUGO_BIN" "$@" --baseURL="$BASE_URL" --appendPort=false --liveReloadPort=443
44+
fi
45+
fi
46+
fi
47+
fi
48+
49+
# If not 'hugo server' in Codespaces, or baseURL was already provided, just run hugo normally
50+
exec "$HUGO_BIN" "$@"

.vscode/start-hugo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
# Determine the base URL for Hugo server
44
# In Codespaces, use the Codespaces URL; otherwise, use localhost

.vscode/tasks.json

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,6 @@
1010
"panel": "dedicated"
1111
},
1212
"problemMatcher": []
13-
},
14-
{
15-
"label": "Hugo: Start Server",
16-
"type": "shell",
17-
"command": "hugo server --bind=0.0.0.0 --appendPort=false --liveReloadPort=443 -D",
18-
"isBackground": true,
19-
"presentation": {
20-
"reveal": "always",
21-
"panel": "dedicated"
22-
},
23-
"problemMatcher": {
24-
"pattern": {
25-
"regexp": "^$",
26-
"file": 0,
27-
"location": 1,
28-
"message": 2
29-
},
30-
"background": {
31-
"activeOnStart": true,
32-
"beginsPattern": "^.*Building sites.*$",
33-
"endsPattern": "^.*Web Server is available.*$"
34-
}
35-
}
3613
}
3714
]
3815
}

CODESPACES.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,28 @@ The Codespace will take a minute or two to set up. You'll see a loading screen w
2626

2727
### Step 4: Run the Hugo Server
2828

29-
Once the Codespace is loaded:
29+
Once the Codespace is loaded, you can start the Hugo server using any of these methods:
30+
31+
**Option A: Command Line (Recommended and Easiest)**
32+
33+
1. Open the terminal (Terminal → New Terminal if not already open)
34+
2. Run: `hugo server`
35+
36+
That's it! In Codespaces, the `hugo server` command automatically detects the environment and configures everything correctly.
37+
38+
**Option B: Using the Run and Debug Panel**
3039

3140
1. Click the "Run and Debug" icon in the left sidebar (it looks like a play button with a bug)
3241
2. At the top of the Run and Debug panel, click on the dropdown and select "Hugo Server"
3342
3. Click the green play button to start the Hugo server
3443

44+
**Option C: Using npm**
45+
46+
1. Open the terminal (Terminal → New Terminal)
47+
2. Run: `npm start`
48+
49+
All methods work correctly in Codespaces and will automatically configure the proper URL so that CSS and assets load correctly.
50+
3551
### Step 5: View the Website Preview
3652

3753
When the Hugo server starts:

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ GitHub Codespaces provides an easy way to work on the website without installing
2727
3. Select the "Codespaces" tab
2828
4. Click "Create codespace on master"
2929
5. Wait for the codespace to load (this may take a minute)
30-
6. Once loaded, click the "Run and Debug" icon in the sidebar (or press F5)
31-
7. Select "Hugo Server" from the dropdown menu and click the play button
32-
8. When prompted, click "Open in Browser" to view the website
30+
6. Start the Hugo server using **any of these methods**:
31+
- Run `hugo server` in the terminal (recommended - works automatically in Codespaces!)
32+
- Run `npm start` in the terminal
33+
- Click the "Run and Debug" icon in the sidebar (or press F5), select "Hugo Server" and click the play button
34+
7. When prompted, click "Open in Browser" to view the website
3335

3436
The website will be displayed in a new browser tab, and any changes you make will automatically update in real-time.
3537

38+
**Note**: In Codespaces, `hugo server` automatically detects the environment and configures the correct URL, so CSS and assets load properly from the Codespaces forwarded URL.
39+
3640
For a more detailed guide with screenshots, please see our [Codespaces documentation](CODESPACES.md).
3741

3842
### Local Installation

hugo-server.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
# Hugo Server Starter for Codespaces and Local Development
4+
# This script automatically detects the environment and starts Hugo with the correct configuration
5+
6+
if [ -n "$CODESPACE_NAME" ]; then
7+
# Running in GitHub Codespaces
8+
BASE_URL="https://${CODESPACE_NAME}-1313.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
9+
echo "🚀 Starting Hugo in Codespaces mode"
10+
echo "📍 Base URL: $BASE_URL"
11+
else
12+
# Running locally
13+
BASE_URL="http://localhost:1313"
14+
echo "🚀 Starting Hugo in local mode"
15+
echo "📍 Base URL: $BASE_URL"
16+
fi
17+
18+
# Start Hugo server with the appropriate configuration
19+
exec hugo server \
20+
--bind=0.0.0.0 \
21+
--baseURL="$BASE_URL" \
22+
--appendPort=false \
23+
--liveReloadPort=443 \
24+
-D

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"scripts": {
99
"build": "hugo",
1010
"build:preview": "hugo --baseURL \"${DEPLOY_PRIME_URL:-/}\" --buildDrafts --buildFuture",
11-
"start": "hugo server"
11+
"start": ".vscode/start-hugo.sh"
1212
},
1313
"devDependencies": {
1414
"autoprefixer": "^10.3.4",

0 commit comments

Comments
 (0)