Skip to content

Commit 880c3c7

Browse files
committed
修復 GitHub Pages SPA 路由問題
🔧 問題修復: - 添加 web/404.html 處理 GitHub Pages 的 SPA 路由 - 修改 web/index.html 添加路由重導向處理邏輯 📝 技術說明: GitHub Pages 對於 SPA 應用程式,當用戶直接訪問 /user/123 這樣的路徑時, 會返回 404 因為沒有實際檔案。404.html 會將這些請求重導向到 index.html 並保留路由資訊,讓 Flutter 的路由系統能正確處理。 🎯 解決問題: - QR 碼連結 https://auto30next.alearn.org.tw/user/xxx 現在應該能正常工作 - 所有動態路由都能在 GitHub Pages 上正常運作
1 parent 80f33bd commit 880c3c7

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

web/404.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Auto30 Next</title>
6+
<script type="text/javascript">
7+
// Single Page Apps for GitHub Pages
8+
// MIT License
9+
// https://github.com/rafgraph/spa-github-pages
10+
// This script takes the current url and converts the path and query
11+
// string into just a query string, and then redirects the browser
12+
// to the new url with only a query string and hash fragment,
13+
// e.g. https://www.foo.tld/one/two?a=b&c=d#qwe, becomes
14+
// https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
15+
// Note: this 404.html file must be at least 512 bytes for it to work
16+
// with Internet Explorer (it is currently > 512 bytes)
17+
18+
// If you're creating a Project Pages site and NOT using a custom domain,
19+
// then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
20+
// This way the code will only replace the route part and not the real directory.
21+
// For example: https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
22+
// https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
23+
// Otherwise, leave pathSegmentsToKeep as 0.
24+
var pathSegmentsToKeep = 0;
25+
26+
var l = window.location;
27+
l.replace(
28+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
29+
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
30+
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
31+
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
32+
l.hash
33+
);
34+
</script>
35+
</head>
36+
<body>
37+
<!--
38+
This file is used to handle client-side routing for Single Page Applications (SPA)
39+
hosted on GitHub Pages. When a user directly accesses a route like /user/123,
40+
GitHub Pages returns a 404 because there's no actual file at that path.
41+
This script redirects such requests to the main index.html with the route
42+
information preserved in the query string, allowing the SPA router to handle it properly.
43+
-->
44+
</body>
45+
</html>

web/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@
100100
</div>
101101

102102
<script>
103+
// Handle GitHub Pages SPA routing
104+
(function(l) {
105+
if (l.search[1] === '/' ) {
106+
var decoded = l.search.slice(1).split('&').map(function(s) {
107+
return s.replace(/~and~/g, '&')
108+
}).join('?');
109+
window.history.replaceState(null, null,
110+
l.pathname.slice(0, -1) + decoded + l.hash
111+
);
112+
}
113+
}(window.location))
114+
103115
window.addEventListener('load', function() {
104116
var loading = document.querySelector('#loading-indicator');
105117

0 commit comments

Comments
 (0)