Skip to content

Commit b3ffc2b

Browse files
authored
Merge pull request #181 from slaveofcode/develop
Promote to production: bypass CF edge cache for OFM proxy
2 parents e926b40 + 002e3ec commit b3ffc2b

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/islands/maps/MapExplorer.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ export default function MapExplorer({ lang = 'en' }: { lang?: Lang }) {
192192
// never needs its own TileJSON fetch (avoids stale CDN edge responses).
193193
const styleInput = await fetchResolvedStyle(initialUrl);
194194
if (cancelled || !containerRef.current) return;
195+
196+
// Diagnostic: probe one vector tile to verify the proxy returns valid PBF.
197+
// Valid gzip-PBF starts with bytes 1f 8b; raw PBF starts with a protobuf tag.
198+
// HTML/JSON/text responses (corrupted edge cache) start with ASCII bytes.
199+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
200+
const omtTiles = (styleInput as any)?.sources?.openmaptiles?.tiles;
201+
if (Array.isArray(omtTiles) && omtTiles[0]) {
202+
const probeUrl = (omtTiles[0] as string).replace('{z}', '5').replace('{x}', '26').replace('{y}', '16');
203+
fetch(probeUrl).then(async r => {
204+
const buf = await r.arrayBuffer();
205+
const b = new Uint8Array(buf.slice(0, 8));
206+
const hex = Array.from(b).map(n => n.toString(16).padStart(2, '0')).join(' ');
207+
console.log('[map] tile probe', probeUrl, '→ status:', r.status, 'ct:', r.headers.get('content-type'), 'size:', buf.byteLength, 'bytes:', hex);
208+
}).catch(e => console.error('[map] tile probe error:', e));
209+
}
210+
195211
// Rewrite every OpenFreeMap URL that MapLibre would fetch on its own
196212
// (TileJSON, vector/raster tiles, glyphs, sprites) through our /ofm/ proxy.
197213
// This is the definitive safety net: even if fetchResolvedStyle leaves a

worker/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,15 @@ export default {
6464
return new Response('Method not allowed', { status: 405 });
6565
}
6666
const upstream = 'https://tiles.openfreemap.org/' + url.pathname.slice('/ofm/'.length) + url.search;
67+
// Fetch WITHOUT cf.cacheEverything so we bypass Cloudflare's edge cache.
68+
// The Singapore edge had corrupted cached responses for OFM PBF tile URLs
69+
// (HTTP 200 with wrong body), which were being served by the edge cache
70+
// when cacheEverything:true was set. Going directly to OFM's origin via
71+
// CF backbone avoids those stale entries. The browser's own HTTP cache
72+
// (driven by the cache-control header we set below) handles client-side
73+
// caching without touching the broken edge entries.
6774
const res = await fetch(upstream, {
6875
headers: { 'user-agent': 'goodwebtools-ofm-proxy' },
69-
cf: { cacheEverything: true, cacheTtl: 86400 },
7076
});
7177
if (!res.ok) {
7278
return new Response('Upstream OFM fetch failed', { status: res.status || 502 });

0 commit comments

Comments
 (0)