From ecb4a08e56b36c6b6f40ed21cf67951aea37d490 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 29 Jul 2023 23:05:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=AF=BC=E8=87=B4=E5=89=8D=E7=AB=AF=E6=B2=A1?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 18 ++++++++++++++---- api/base.py | 2 ++ api/main.py | 11 ++++++++++- main.py | 4 +--- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fdbf72a..14ecada 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,8 @@ server { ssl_certificate /PATH/TO/CERT.crt; ssl_certificate_key /PATH/TO/CERT_KEY.key; + set $blivechat_path /PATH/TO/BLIVECHAT; + client_body_buffer_size 256k; client_max_body_size 1.1m; @@ -120,12 +122,20 @@ server { # 静态文件 location / { - root /PATH/TO/BLIVECHAT/frontend/dist; - # 如果文件不存在,交给前端路由 - try_files $uri $uri/ /index.html; + root $blivechat_path/frontend/dist; + try_files $uri $uri/ @index; + } + # 不存在的文件请求转发到index.html,交给前端路由 + location @index { + rewrite ^ /index.html last; + } + location = /index.html { + root $blivechat_path/frontend/dist; + # index.html不缓存,防止更新后前端还是旧版 + add_header Cache-Control no-cache; } location /emoticons { - alias /PATH/TO/BLIVECHAT/data/emoticons; + alias $blivechat_path/data/emoticons; } # 动态API location /api { diff --git a/api/base.py b/api/base.py index b9d0f25..5db9f64 100644 --- a/api/base.py +++ b/api/base.py @@ -10,6 +10,8 @@ class ApiHandler(tornado.web.RequestHandler): # noqa self.json_args = None def prepare(self): + self.set_header('Cache-Control', 'no-cache') + if not self.request.headers.get('Content-Type', '').startswith('application/json'): return try: diff --git a/api/main.py b/api/main.py index 577ba6f..6d170fc 100644 --- a/api/main.py +++ b/api/main.py @@ -19,13 +19,22 @@ EMOTICON_BASE_URL = '/emoticons' class MainHandler(tornado.web.StaticFileHandler): # noqa """为了使用Vue Router的history模式,把不存在的文件请求转发到index.html""" async def get(self, path, include_body=True): + if path == '': + await self._get_index(include_body) + return + try: await super().get(path, include_body) except tornado.web.HTTPError as e: if e.status_code != 404: raise # 不存在的文件请求转发到index.html,交给前端路由 - await super().get('index.html', include_body) + await self._get_index(include_body) + + async def _get_index(self, include_body=True): + # index.html不缓存,防止更新后前端还是旧版 + self.set_header('Cache-Control', 'no-cache') + await super().get('index.html', include_body) class ServerInfoHandler(api.base.ApiHandler): # noqa diff --git a/main.py b/main.py index e27e4b1..4c73e12 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ routes = [ (r'/api/avatar_url', api.chat.AvatarHandler), (rf'{api.main.EMOTICON_BASE_URL}/(.*)', tornado.web.StaticFileHandler, {'path': api.main.EMOTICON_UPLOAD_PATH}), - (r'/(.*)', api.main.MainHandler, {'path': config.WEB_ROOT, 'default_filename': 'index.html'}) + (r'/(.*)', api.main.MainHandler, {'path': config.WEB_ROOT}) ] @@ -98,8 +98,6 @@ def run_server(host, port, debug): return finally: url = 'http://localhost/' if port == 80 else f'http://localhost:{port}/' - # 防止更新版本后浏览器加载缓存 - url += '?_v=' + update.VERSION webbrowser.open(url) logger.info('Server started: %s:%d', host, port) tornado.ioloop.IOLoop.current().start()