Skip to content

Commit 0eb0d83

Browse files
Copilotmattleibow
andauthored
Fix direct navigation 404 on Blazor sample pages (#369)
* Initial plan * Fix 404 when navigating directly to a Blazor sample page Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Update comment job to fire on synchronize and update existing comment Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Copy root 404.html during staging deploys so SPA redirect works for staging paths Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
1 parent c9bde7e commit 0eb0d83

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

.github/workflows/docs-deploy.yml

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,19 @@ jobs:
181181
mkdir -p staging/$PR_NUMBER
182182
rm -rf staging/$PR_NUMBER/*
183183
cp -r /tmp/site-temp/. staging/$PR_NUMBER/
184+
185+
# Copy 404.html to the gh-pages root so SPA redirects work for staging
186+
# paths (GitHub Pages only serves the root-level 404.html for missing paths)
187+
cp /tmp/site-temp/404.html 404.html
188+
184189
rm -rf /tmp/site-temp
185190
186191
# Ensure .nojekyll exists at root so _framework is served
187192
touch .nojekyll
188193
189194
git config --local user.email "action@github.com"
190195
git config --local user.name "GitHub Action"
191-
git add .nojekyll staging/$PR_NUMBER
196+
git add .nojekyll 404.html staging/$PR_NUMBER
192197
if ! git diff --cached --quiet; then
193198
git commit -m "Deploy staging docs for PR #$PR_NUMBER"
194199
git push origin gh-pages
@@ -199,7 +204,7 @@ jobs:
199204
comment:
200205
runs-on: ubuntu-latest
201206
needs: [build, deploy]
202-
if: needs.build.outputs.is_staging == 'true' && (github.event.action == 'opened' || github.event.action == 'reopened')
207+
if: needs.build.outputs.is_staging == 'true' && (github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize')
203208
permissions:
204209
pull-requests: write
205210
steps:
@@ -211,21 +216,6 @@ jobs:
211216
const stagingUrl = `https://mono.github.io/SkiaSharp.Extended/staging/${prNumber}/`;
212217
const sampleUrl = `https://mono.github.io/SkiaSharp.Extended/staging/${prNumber}/sample/`;
213218
214-
// Only comment once; check for an existing bot comment to avoid duplicates on reopen
215-
const comments = await github.rest.issues.listComments({
216-
owner: context.repo.owner,
217-
repo: context.repo.repo,
218-
issue_number: prNumber
219-
});
220-
const botComment = comments.data.find(c =>
221-
c.user.type === 'Bot' &&
222-
c.body.includes('Documentation Preview')
223-
);
224-
if (botComment) {
225-
console.log('Bot comment already exists, skipping.');
226-
return;
227-
}
228-
229219
const commentBody = [
230220
'📖 **Documentation Preview**',
231221
'',
@@ -241,9 +231,28 @@ jobs:
241231
'*This comment is automatically updated by the documentation staging workflow.*'
242232
].join('\n');
243233
244-
await github.rest.issues.createComment({
234+
// Update existing bot comment if present, otherwise create a new one
235+
const comments = await github.rest.issues.listComments({
245236
owner: context.repo.owner,
246237
repo: context.repo.repo,
247-
issue_number: prNumber,
248-
body: commentBody
238+
issue_number: prNumber
249239
});
240+
const botComment = comments.data.find(c =>
241+
c.user.type === 'Bot' &&
242+
c.body.includes('Documentation Preview')
243+
);
244+
if (botComment) {
245+
await github.rest.issues.updateComment({
246+
owner: context.repo.owner,
247+
repo: context.repo.repo,
248+
comment_id: botComment.id,
249+
body: commentBody
250+
});
251+
} else {
252+
await github.rest.issues.createComment({
253+
owner: context.repo.owner,
254+
repo: context.repo.repo,
255+
issue_number: prNumber,
256+
body: commentBody
257+
});
258+
}

docs/404.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>SkiaSharp Extended - Page Not Found</title>
6+
<script>
7+
// GitHub Pages SPA redirect: when a user navigates directly to a Blazor sample
8+
// sub-page, GitHub Pages serves this 404.html. If the path is under a /sample/
9+
// segment, redirect to that sample's index.html with the path encoded in the
10+
// query string so that the Blazor router can restore the correct route.
11+
(function () {
12+
var l = window.location;
13+
var pathParts = l.pathname.split('/');
14+
var sampleIdx = pathParts.indexOf('sample');
15+
if (sampleIdx !== -1 && sampleIdx < pathParts.length - 1 && pathParts[sampleIdx + 1] !== '') {
16+
var basePath = pathParts.slice(0, sampleIdx + 1).join('/');
17+
var remaining = pathParts.slice(sampleIdx + 1).join('/').replace(/&/g, '~and~');
18+
l.replace(
19+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
20+
basePath + '/?/' + remaining +
21+
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
22+
l.hash
23+
);
24+
}
25+
})();
26+
</script>
27+
</head>
28+
<body>
29+
<h1>Page Not Found</h1>
30+
<p>The page you are looking for does not exist.</p>
31+
<p><a href="/SkiaSharp.Extended/">Go to the documentation</a></p>
32+
</body>
33+
</html>

docs/docfx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"resource": [
3131
{
3232
"files": [
33-
"images/**"
33+
"images/**",
34+
"404.html"
3435
]
3536
}
3637
],

0 commit comments

Comments
 (0)