Skip to content

Commit 505db83

Browse files
Fix GitHub Pages demo base href rewriting
Co-authored-by: erik <erik@zettersten.com>
1 parent b606c76 commit 505db83

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

.github/workflows/deploy-demo.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ jobs:
4141
run: |
4242
set -euo pipefail
4343
WWWROOT="artifacts/publish/wwwroot"
44+
# GitHub Pages serves this app from "/<repo-name>/", not "/".
45+
# Some SDK combinations don't reliably apply -p:BaseHref at publish-time,
46+
# so we rewrite the published index.html as a backstop.
47+
REPO_NAME="${{ github.event.repository.name }}"
48+
BASE_HREF="/${REPO_NAME}/"
49+
WWWROOT="$WWWROOT" BASE_HREF="$BASE_HREF" python3 - << 'PY'
50+
import os, re
51+
wwwroot = os.environ["WWWROOT"]
52+
base_href = os.environ["BASE_HREF"]
53+
index_path = os.path.join(wwwroot, "index.html")
54+
with open(index_path, "r", encoding="utf-8") as f:
55+
html = f.read()
56+
# Replace any existing <base href="..."> with the Pages base.
57+
html2, n = re.subn(r'<base\s+href="[^"]*"\s*/?>', f'<base href="{base_href}" />', html, count=1, flags=re.IGNORECASE)
58+
if n != 1:
59+
raise SystemExit(f"Expected to replace 1 <base ...> tag, replaced {n}")
60+
with open(index_path, "w", encoding="utf-8") as f:
61+
f.write(html2)
62+
PY
4463
# Ensure SPA fallback works for GitHub Pages (client-side routing)
4564
cp "${WWWROOT}/index.html" "${WWWROOT}/404.html"
4665
# Avoid Pages attempting to run Jekyll processing

0 commit comments

Comments
 (0)