mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
fix(places): don't let the browser cache a refused Google answer
The Places and Geocoding proxies sent Cache-Control: private, max-age=300 whatever Google answered. A 403 from a key without the Places API, or a geocode REQUEST_DENIED / OVER_QUERY_LIMIT (which Google sends as HTTP 200), was then replayed from the browser cache for five minutes after the key was fixed. Refusals are now no-store.
This commit is contained in:
1 parent
7d8b80db0a
commit
2e2f756468
3 files changed
+62
-3
No files matched your search
@@ -71,6 +71,9 @@ const GEOCODE_UPSTREAM_TIMEOUT_MS = 10_000;
|
||||
|
||||
const GEOCODE_MAX_RESPONSE_BYTES = 256 * 1024;
|
||||
|
||||
/** Geocoding statuses that answer the query; the rest report a failure. */
|
||||
const GEOCODE_ANSWER_STATUSES = new Set(['OK', 'ZERO_RESULTS']);
|
||||
|
||||
/** Validate a free-text geocoder query before consuming request quota. */
|
||||
export function validateGeocodeAddress(searchParams) {
|
||||
const address = String(searchParams.get('address') || '').trim();
|
||||
@@ -199,7 +202,12 @@ export function googlePlacesContextProxy({
|
||||
|
||||
res.statusCode = response.ok ? 200 : response.status;
|
||||
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
res.setHeader('Cache-Control', 'private, max-age=300');
|
||||
// A refusal (bad key, API not enabled, quota) is not kept: the browser
|
||||
// would replay it for five minutes after the key is fixed.
|
||||
res.setHeader(
|
||||
'Cache-Control',
|
||||
response.ok ? 'private, max-age=300' : 'no-store',
|
||||
);
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
places,
|
||||
@@ -320,7 +328,12 @@ export function googlePlacesContextProxy({
|
||||
|
||||
res.statusCode = response.ok ? 200 : response.status;
|
||||
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
res.setHeader('Cache-Control', 'private, max-age=300');
|
||||
// A refusal (bad key, API not enabled, quota) is not kept: the browser
|
||||
// would replay it for five minutes after the key is fixed.
|
||||
res.setHeader(
|
||||
'Cache-Control',
|
||||
response.ok ? 'private, max-age=300' : 'no-store',
|
||||
);
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
places,
|
||||
@@ -446,7 +459,14 @@ export function googlePlacesContextProxy({
|
||||
|
||||
res.statusCode = response.ok ? 200 : response.status;
|
||||
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
res.setHeader('Cache-Control', 'private, max-age=300');
|
||||
// Geocoding reports a denied key or spent quota as HTTP 200 with a
|
||||
// status of its own; only an answer is worth keeping.
|
||||
res.setHeader(
|
||||
'Cache-Control',
|
||||
response.ok && GEOCODE_ANSWER_STATUSES.has(data.status)
|
||||
? 'private, max-age=300'
|
||||
: 'no-store',
|
||||
);
|
||||
res.end(JSON.stringify(data));
|
||||
} catch {
|
||||
// Fixed public text: a transport, URL or runtime message would
|
||||
|
||||
Reference in new issue
Block a user