From 0a422ce2f21e65a58a6ffe606d87dbd8756fb036 Mon Sep 17 00:00:00 2001 From: netcon Date: Sat, 22 May 2021 13:56:08 +0800 Subject: [PATCH] fix: 404 response is cached when static files not found (#308) * fix: vercel cached 404 requests * fix: add static request fallback serverless --- api/static-fallback/index.js | 17 ++++++++++++ vercel.json | 52 ++++++++++++++++++++++++------------ 2 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 api/static-fallback/index.js diff --git a/api/static-fallback/index.js b/api/static-fallback/index.js new file mode 100644 index 000000000..32c04b7c3 --- /dev/null +++ b/api/static-fallback/index.js @@ -0,0 +1,17 @@ +/** + * @file static files not found fallback + * According to the config in vercel.json, we cached the + * static files with `max-age=X` header, but we may get 404 + * response durning deploying, the default behavior for vercel + * would add the cache header to this 404 response, which caused + * the user can not get right resources anymore. so we should + * fallback to here and clear the cache header for such 404 requests. + * See also: https://github.com/conwnet/github1s/issues/299 + * @author netcon + */ + +module.exports = async (req, res) => { + res.status(404); + res.setHeader('Cache-Control', 'no-store'); + res.send('Not Found'); +}; diff --git a/vercel.json b/vercel.json index f98a41564..c256fdc02 100644 --- a/vercel.json +++ b/vercel.json @@ -1,31 +1,49 @@ { - "routes": [ + "rewrites": [ { - "src": "/api/sourcegraph", - "dest": "https://sourcegraph.com/.api/graphql" + "source": "/api/sourcegraph", + "destination": "https://sourcegraph.com/.api/graphql" }, { - "src": "/static/(.*)", - "dest": "/static/$1", - "headers": { - "Cache-Control": "max-age=604800" - } + "source": "/api/github-auth-callback", + "destination": "/api/github-auth-callback" }, { - "src": "/favicon(-dark|-light)?.ico", - "dest": "/favicon$1.ico" + "source": "/static/(.*)", + "destination": "/api/static-fallback" }, { - "src": "/manifest.json", - "dest": "/manifest.json" + "source": "/(.*)", + "destination": "/index.html" + } + ], + "headers": [ + { + "source": "/static/(.*)", + "headers": [ + { + "key": "Cache-Control", + "value": "public, max-age=2592000, immutable" + } + ] }, { - "src": "/api/github-auth-callback", - "dest": "/api/github-auth-callback" + "source": "/favicon(-dark|-light)?.ico", + "headers": [ + { + "key": "Cache-Control", + "value": "public, max-age=3600" + } + ] }, { - "src": ".*", - "dest": "/index.html" + "source": "/manifest.json", + "headers": [ + { + "key": "Cache-Control", + "value": "public, max-age=3600" + } + ] } ] -} \ No newline at end of file +}