How to build hash routing in remix? #10386
-
remix template uses spa server: import os.path
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/", StaticFiles(directory='static', html=True), name="static") |
Beta Was this translation helpful? Give feedback.
Answered by
InfernalAzazel
Jan 9, 2025
Replies: 3 comments
-
Thanks for the official example app.use("/assets", express.static("build/client/assets"));
app.get("*", (req, res, next) =>
res.sendFile(
path.join(process.cwd(), "build/client/index.html"),
next
)
); Modify the fastapi code after reference to solve the problem from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pathlib import Path
from fastapi.responses import HTMLResponse
STATIC_DIR = Path(__file__).parent / "static"
ASSETS_DIR = STATIC_DIR / "assets"
INDEX_FILE = STATIC_DIR / "index.html"
app = FastAPI()
# 提供静态资源,映射到 /assets
app.mount("/assets",StaticFiles(directory=ASSETS_DIR), name="assets")
@app.get("/{_:path}", response_class=HTMLResponse)
async def serve_spa():
"""
捕获所有路由并返回 index.html
"""
return FileResponse(INDEX_FILE) |
Beta Was this translation helpful? Give feedback.
0 replies
-
I want to support hash routing because for polluting fastapi api routing |
Beta Was this translation helpful? Give feedback.
0 replies
-
Fastapi mount remix spa final solution static.py from fastapi.responses import FileResponse
from app.settings import settings
router = APIRouter()
@router.get('/{path:path}', include_in_schema=False)
async def frontend_handler(path: str):
fp = settings.STATIC_DIR / path
if not fp.exists() or not fp.is_file():
fp = settings.STATIC_DIR / "index.html"
return FileResponse(fp) app.py
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
InfernalAzazel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fastapi mount remix spa final solution
static.py
app.py