Skip to content

Commit 17492bc

Browse files
fix preview of pr
Signed-off-by: saurabhraghuvanshii <saurabhsraghuvanshi@gmail.com>
1 parent 7f671b4 commit 17492bc

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

.github/workflows/build-and-preview-site.yml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,51 @@ jobs:
4141
if: github.event.action != 'closed'
4242
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
4343

44+
# Project Pages URLs are https://<owner>.github.io/<repo>/… so Gatsby pathPrefix must
45+
# include <repo> (e.g. layer5/pr-preview/pr-9), not only /pr-preview/pr-9 — otherwise JS/CSS
46+
# load from the wrong path and the shell renders without client-side content.
4447
- name: Resolve preview URLs
4548
if: github.event.action != 'closed'
4649
id: preview-vars
4750
env:
4851
PREVIEW_SITE_URL: ${{ vars.PREVIEW_SITE_URL }}
52+
GITHUB_PAGES_ORIGIN: ${{ vars.GITHUB_PAGES_ORIGIN }}
4953
run: |
54+
owner="${GITHUB_REPOSITORY%%/*}"
5055
repo_name="${GITHUB_REPOSITORY#*/}"
51-
base_url="${PREVIEW_SITE_URL:-https://${GITHUB_REPOSITORY_OWNER}.github.io/${repo_name}}"
52-
base_url="${base_url%/}"
53-
path_prefix="/pr-preview/pr-${{ github.event.pull_request.number }}"
54-
55-
echo "base_url=${base_url}" >> "$GITHUB_OUTPUT"
56+
pr="pr-${{ github.event.pull_request.number }}"
57+
58+
if [[ -n "${PREVIEW_SITE_URL:-}" ]]; then
59+
base="${PREVIEW_SITE_URL%/}"
60+
pages_origin="$(printf '%s' "$base" | sed -E 's|(https?://[^/]+).*|\1|')"
61+
repo_path="$(printf '%s' "$base" | sed -E 's|https?://[^/]+||' | sed 's|^/||;s|/$||')"
62+
if [[ -n "$repo_path" ]]; then
63+
path_prefix="${repo_path}/pr-preview/${pr}"
64+
else
65+
path_prefix="pr-preview/${pr}"
66+
fi
67+
preview_url="${base}/pr-preview/${pr}/"
68+
else
69+
pages_origin="${GITHUB_PAGES_ORIGIN:-https://${owner}.github.io}"
70+
pages_origin="${pages_origin%/}"
71+
if [[ "$repo_name" == "${owner}.github.io" ]]; then
72+
path_prefix="pr-preview/${pr}"
73+
preview_url="${pages_origin}/pr-preview/${pr}/"
74+
else
75+
path_prefix="${repo_name}/pr-preview/${pr}"
76+
preview_url="${pages_origin}/${repo_name}/pr-preview/${pr}/"
77+
fi
78+
fi
79+
80+
echo "pages_origin=${pages_origin}" >> "$GITHUB_OUTPUT"
5681
echo "path_prefix=${path_prefix}" >> "$GITHUB_OUTPUT"
82+
echo "preview_url=${preview_url}" >> "$GITHUB_OUTPUT"
5783
5884
- name: Build PR preview
5985
if: github.event.action != 'closed'
6086
env:
6187
GATSBY_PREVIEW: "true"
62-
GATSBY_SITE_URL: ${{ steps.preview-vars.outputs.base_url }}
88+
GATSBY_SITE_URL: ${{ steps.preview-vars.outputs.pages_origin }}
6389
PATH_PREFIX: ${{ steps.preview-vars.outputs.path_prefix }}
6490
run: |
6591
npm run build:preview
@@ -90,7 +116,7 @@ jobs:
90116
message: |
91117
🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}**
92118
93-
🌐 **Preview URL**: ${{ steps.preview-vars.outputs.base_url }}/pr-preview/pr-${{ github.event.pull_request.number }}/
119+
🌐 **Preview URL**: ${{ steps.preview-vars.outputs.preview_url }}
94120
95121
_This preview will be updated automatically when you push new commits to this PR._
96122

0 commit comments

Comments
 (0)