fix: 修复 SPA 路由刷新 404 和认证状态丢失问题
- 移除手动 SPA fallback 逻辑,使用 Cloudflare Assets 原生 SPA 支持 - 修复刷新时认证状态丢失导致跳转首页的问题 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9bf3a9efb2
commit
6713f240ba
4 files changed
+14
-25
No files matched your search
@@ -21,7 +21,11 @@ const queryClient = new QueryClient({
|
||||
})
|
||||
|
||||
function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
||||
const { isAuthenticated } = useAuthStore()
|
||||
const { isAuthenticated, isLoading } = useAuthStore()
|
||||
|
||||
if (isLoading) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/" replace />
|
||||
|
||||
@@ -15,7 +15,7 @@ interface AuthState {
|
||||
|
||||
export const useAuthStore = create<AuthState>((set) => ({
|
||||
isAuthenticated: false,
|
||||
isLoading: false,
|
||||
isLoading: true,
|
||||
error: null,
|
||||
showAuthModal: false,
|
||||
|
||||
@@ -48,16 +48,17 @@ export const useAuthStore = create<AuthState>((set) => ({
|
||||
checkAuth: async () => {
|
||||
const token = localStorage.getItem('adminToken')
|
||||
if (!token) {
|
||||
set({ isAuthenticated: false })
|
||||
set({ isAuthenticated: false, isLoading: false })
|
||||
return
|
||||
}
|
||||
|
||||
set({ isLoading: true })
|
||||
try {
|
||||
await apiClient.checkAuth()
|
||||
set({ isAuthenticated: true })
|
||||
set({ isAuthenticated: true, isLoading: false })
|
||||
} catch (error) {
|
||||
localStorage.removeItem('adminToken')
|
||||
set({ isAuthenticated: false })
|
||||
set({ isAuthenticated: false, isLoading: false })
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
+1
-18
@@ -1,14 +1,9 @@
|
||||
import { Context, Hono, Next } from 'hono'
|
||||
import { Hono } from 'hono'
|
||||
import { cors } from 'hono/cors';
|
||||
import { api as providerApi } from './providers'
|
||||
import { api as adminApi } from './admin'
|
||||
import { fromHono } from 'chanfana';
|
||||
|
||||
const API_PATHS = [
|
||||
"/api",
|
||||
"/v1",
|
||||
];
|
||||
|
||||
const app = new Hono<HonoCustomType>()
|
||||
const openapi = fromHono(app, {
|
||||
schema: {
|
||||
@@ -24,18 +19,6 @@ const openapi = fromHono(app, {
|
||||
|
||||
// cors
|
||||
openapi.use('/*', cors());
|
||||
// global middlewares
|
||||
app.use('/*', async (c, next) => {
|
||||
// check if the request is for static files
|
||||
if (c.env.ASSETS && !API_PATHS.some(path => c.req.path.startsWith(path))) {
|
||||
const url = new URL(c.req.raw.url);
|
||||
if (!url.pathname.includes('.')) {
|
||||
url.pathname = ""
|
||||
}
|
||||
return c.env.ASSETS.fetch(url);
|
||||
}
|
||||
await next()
|
||||
});
|
||||
// global error handler
|
||||
openapi.onError((err, c) => {
|
||||
console.error(err)
|
||||
|
||||
@@ -9,9 +9,10 @@ routes = [
|
||||
ADMIN_TOKEN = "your-secure-admin-token-here"
|
||||
|
||||
[assets]
|
||||
directory = "public"
|
||||
directory = "./public"
|
||||
not_found_handling = "single-page-application"
|
||||
binding = "ASSETS"
|
||||
run_worker_first = true
|
||||
run_worker_first = ["/api/*", "/v1/*"]
|
||||
|
||||
[[d1_databases]]
|
||||
binding = "DB"
|
||||
|
||||
Reference in new issue
Block a user