mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
fix: the map falls back to raster where the vector style cannot load, the tour layer pill fits narrow maps and stands on the side in 3D, the dashboard's widget column scrolls on its own, and the CI checks the watch sync and journey write-up broke
This commit is contained in:
@@ -169,6 +169,38 @@ describe('offlineProtocol', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('offlineProtocol without Cache Storage', () => {
|
||||
it('draws from the network when the web view will not open the cache, and only then fails offline', async () => {
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
vi.stubGlobal('caches', {
|
||||
open: vi.fn(async () => {
|
||||
throw new DOMException('denied', 'SecurityError');
|
||||
}),
|
||||
});
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn(async () => new Response(JSON.stringify({ sources: {} })))
|
||||
);
|
||||
const doc = await offlineProtocol(params('trek-offline://s.test/style.json', 'json'), new AbortController());
|
||||
expect(doc.data).toEqual({ sources: {} });
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn(async () => new Response('tile'))
|
||||
);
|
||||
const tile = await offlineProtocol(params('trek-offline://t.test/1/1/1.png', 'image'), new AbortController());
|
||||
expect(new TextDecoder().decode(tile.data as ArrayBuffer)).toBe('tile');
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn(async () => {
|
||||
throw new TypeError('offline');
|
||||
})
|
||||
);
|
||||
await expect(
|
||||
offlineProtocol(params('trek-offline://s.test/style.json', 'json'), new AbortController())
|
||||
).rejects.toThrow('Not available offline');
|
||||
});
|
||||
});
|
||||
|
||||
describe('installOfflineProtocol', () => {
|
||||
it('registers only inside the iOS app', () => {
|
||||
const addProtocol = vi.fn();
|
||||
|
||||
@@ -80,8 +80,8 @@ export function rewriteDocument(doc: unknown): unknown {
|
||||
* their URL, so the cache goes first and saves the round trip.
|
||||
*/
|
||||
async function load(url: string, networkFirst: boolean, signal: AbortSignal): Promise<Response> {
|
||||
const cache = await caches.open(VECTOR_CACHE);
|
||||
if (!networkFirst) {
|
||||
const cache = await openCache();
|
||||
if (!networkFirst && cache) {
|
||||
const cached = await cache.match(url);
|
||||
if (cached) return cached;
|
||||
}
|
||||
@@ -92,11 +92,25 @@ async function load(url: string, networkFirst: boolean, signal: AbortSignal): Pr
|
||||
} catch (err) {
|
||||
if (signal.aborted || !networkFirst) throw err;
|
||||
}
|
||||
const cached = await cache.match(url);
|
||||
const cached = await cache?.match(url);
|
||||
if (cached) return cached;
|
||||
throw new Error(`Not available offline: ${url}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* The stored trips' vector cache, or null where the web view will not open it
|
||||
* (WKWebView can refuse CacheStorage). Without it the map still draws from the
|
||||
* network: the store is what keeps it offline, not what lets it draw at all.
|
||||
*/
|
||||
async function openCache(): Promise<Cache | null> {
|
||||
try {
|
||||
return await caches.open(VECTOR_CACHE);
|
||||
} catch (err) {
|
||||
console.warn('[offline tiles] the vector cache is not available, reading from the network', err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const offlineProtocol: AddProtocolAction = async (
|
||||
params: RequestParameters,
|
||||
abortController: AbortController
|
||||
|
||||
Reference in New Issue
Block a user