|
|
@@ -21,16 +21,20 @@ os.chdir(ROOT)
|
|
|
|
|
|
|
|
|
class SpaHandler(SimpleHTTPRequestHandler):
|
|
|
- def do_GET(self):
|
|
|
- # 路径不是真实文件 → 回退到 index.html(SPA 客户端路由)。
|
|
|
+ # 覆盖 send_head(GET 与 HEAD 都走它),统一做 SPA 回退。
|
|
|
+ def send_head(self):
|
|
|
rel = self.path.split("?", 1)[0].split("#", 1)[0].lstrip("/")
|
|
|
if rel and not os.path.isfile(os.path.join(ROOT, rel)):
|
|
|
- self.path = "/index.html"
|
|
|
- return SimpleHTTPRequestHandler.do_GET(self)
|
|
|
+ last = rel.rsplit("/", 1)[-1]
|
|
|
+ # 仅"看起来像路由"的路径(末段无扩展名)回退到 index.html;
|
|
|
+ # 缺失的资源文件(如 /assets/x.js)让其正常 404,避免返回 HTML 触发 MIME 错误。
|
|
|
+ if "." not in last:
|
|
|
+ self.path = "/index.html"
|
|
|
+ return SimpleHTTPRequestHandler.send_head(self)
|
|
|
|
|
|
def end_headers(self):
|
|
|
# index.html 不缓存(构建产物自带 hash,可长缓存)。
|
|
|
- if self.path in ("/", "/index.html"):
|
|
|
+ if self.path.split("?", 1)[0] in ("/", "/index.html"):
|
|
|
self.send_header("Cache-Control", "no-cache")
|
|
|
SimpleHTTPRequestHandler.end_headers(self)
|
|
|
|