camptocamp / ogc-client

A TypeScript library for interacting with geospatial services
https://camptocamp.github.io/ogc-client/
BSD 3-Clause "New" or "Revised" License
66 stars 12 forks source link

`WfsEndpoint.isReady()` throws: Failed to execute 'put' on 'Cache': Entry already exists. #49

Closed mdastous-bentley closed 5 months ago

mdastous-bentley commented 5 months ago

Using this two lines of code:

const endpoint  = new WfsEndpoint(url);
await endpoint.isReady();

I get this exception: image

Initially it was working fine, but suddenly it started throwing this error.

This is the call causing the exception (I cannot step i further):

export async function storeCacheEntry(object: unknown, ...keys: string[]) {
  const cache = await getCache();
  if (!cache) return;
  const entryUrl = 'https://cache/' + keys.join('/');
  await cache.put(
    entryUrl,
    new Response(JSON.stringify(object), {
      headers: {
        'x-expiry': (Date.now() + getCacheExpiryDuration()).toString(10),
      },
    })
  );
}
jahow commented 5 months ago

Thank you for the report, could you please give us the url that you're using?

jahow commented 5 months ago

Actually if you could provide a more complete reproduction case, i.e. a simple script showcasing the error, that would be really helpful. Thanks!

mdastous-bentley commented 5 months ago

I tried with 3 different URL, result is the same. I feel like the cache on my machine got corrupted somehow and cannot be recovered. Is there anything I can do to clear any previous cache entry ?

Complete code here really:

    const url = "https://ahocevar.com/geoserver/wfs";
    const endpoint  = new WfsEndpoint(url);
    await endpoint.isReady();

Which sandbox can I use that got ogc-client pre-installed ?

jahow commented 5 months ago

I created a codesandbox to try to reproduce this but I couldn't: https://codesandbox.io/p/devbox/tqp983

I feel like the cache on my machine got corrupted somehow and cannot be recovered. Is there anything I can do to clear any previous cache entry ?

You can go to the "application" tab of your dev tools, there should be an "ogc-client" entry that you can delete.

Do you reproduce the issue with another browser, or with a private navigation tab?

mdastous-bentley commented 5 months ago

I made further investigations on this, and it appears Cache api doesn't work properly when the application is launched from VS Code (pwa-chrome).

I even checked cache entries before calling WfsEnpoint functions, using this code:

    const cache = await caches.open('ogc-client');
    console.log(await cache.keys());

and it returns an empty array, but still the call to cache.put will fail complaining about existing key.

jahow commented 5 months ago

Ok then we can simply catch any error happening when writing in the cache and, in case of an error, disable the cache. Thanks for the report!