Skip to content

Commit

Permalink
Chrome windows cache workaround (#454)
Browse files Browse the repository at this point in the history
js 3.1.0

* Disable browser caching on chrome on windows [#442]
* work around for chromium issue: https://issues.chromium.org/issues/40542704
  • Loading branch information
bdon authored Sep 19, 2024
1 parent 897d6f1 commit ce959e5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion js/examples/leaflet.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8"/>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://unpkg.com/pmtiles@3.0.7/dist/pmtiles.js"></script>
<script src="https://unpkg.com/pmtiles@3.1.0/dist/pmtiles.js"></script>
<style>
body, #map {
height:100vh;
Expand Down
2 changes: 1 addition & 1 deletion js/examples/maplibre.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8"/>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/maplibre-gl.css" crossorigin="anonymous">
<script src="https://unpkg.com/[email protected]/dist/maplibre-gl.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/pmtiles@3.0.7/dist/pmtiles.js"></script>
<script src="https://unpkg.com/pmtiles@3.1.0/dist/pmtiles.js"></script>
<style>
body {
margin: 0;
Expand Down
2 changes: 1 addition & 1 deletion js/examples/maplibre_raster_dem.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8"/>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/maplibre-gl.css" crossorigin="anonymous">
<script src="https://unpkg.com/[email protected]/dist/maplibre-gl.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/pmtiles@3.0.7/dist/pmtiles.js"></script>
<script src="https://unpkg.com/pmtiles@3.1.0/dist/pmtiles.js"></script>
<style>
body {
margin: 0;
Expand Down
15 changes: 15 additions & 0 deletions js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,24 @@ export class FetchSource implements Source {
customHeaders: Headers;
/** @hidden */
mustReload: boolean;
/** @hidden */
chromeWindowsNoCache: boolean;

constructor(url: string, customHeaders: Headers = new Headers()) {
this.url = url;
this.customHeaders = customHeaders;
this.mustReload = false;
let userAgent = "";
if ("navigator" in globalThis) {
//biome-ignore lint: cf workers
userAgent = (globalThis as any).navigator.userAgent || "";
}
const isWindows = userAgent.indexOf("Windows") > -1;
const isChromiumBased = /Chrome|Chromium|Edg|OPR|Brave/.test(userAgent);
this.chromeWindowsNoCache = false;
if (isWindows && isChromiumBased) {
this.chromeWindowsNoCache = true;
}
}

getKey() {
Expand Down Expand Up @@ -404,6 +417,8 @@ export class FetchSource implements Source {
let cache: string | undefined;
if (this.mustReload) {
cache = "reload";
} else if (this.chromeWindowsNoCache) {
cache = "no-store";
}

let resp = await fetch(this.url, {
Expand Down
4 changes: 2 additions & 2 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pmtiles",
"version": "3.0.7",
"version": "3.1.0",
"description": "PMTiles archive decoder for browsers",
"type": "module",
"exports": {
Expand Down
22 changes: 21 additions & 1 deletion js/test/v3.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import assert from "node:assert";
import { test } from "node:test";
import { afterEach, beforeEach, describe, it, test } from "node:test";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";

Expand All @@ -23,6 +23,7 @@ import {
class MockServer {
etag?: string;
numRequests: number;
lastCache?: string;

reset() {
this.numRequests = 0;
Expand All @@ -37,6 +38,7 @@ class MockServer {
http.get(
"http://localhost:1337/example.pmtiles",
({ request, params }) => {
this.lastCache = request.cache;
this.numRequests++;
const range = request.headers.get("range")?.substr(6).split("-");
if (!range) {
Expand Down Expand Up @@ -415,3 +417,21 @@ test("pmtiles get TileJSON", async () => {
assert.equal("test_fixture_1.pmtiles", tilejson.name);
assert.equal("2", tilejson.version);
});

describe("user agent", async () => {
beforeEach(() => {
// @ts-ignore
global.navigator = { userAgent: "Windows Chrome" };
});

afterEach(() => {
// @ts-ignore
global.navigator.userAgent = undefined;
});

it("works around caching bug on chrome on windows", async () => {
const p = new PMTiles("http://localhost:1337/example.pmtiles");
await p.getZxy(0, 0, 0);
assert.equal("no-store", mockserver.lastCache);
});
});

0 comments on commit ce959e5

Please sign in to comment.