Skip to content

Commit

Permalink
fix: 404 response is cached when static files not found (#308)
Browse files Browse the repository at this point in the history
* fix: vercel cached 404 requests

* fix: add static request fallback serverless
  • Loading branch information
conwnet authored May 22, 2021
1 parent f6403e0 commit 0a422ce
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
17 changes: 17 additions & 0 deletions api/static-fallback/index.js
Original file line number Diff line number Diff line change
@@ -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');
};
52 changes: 35 additions & 17 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
}
}

0 comments on commit 0a422ce

Please sign in to comment.