From e93b091566fd3e05b48ee512ec7e3ceb010fed15 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 2 Oct 2020 14:02:59 +0200 Subject: [PATCH 1/2] dont use no-cache in fetch, as it doesn't play well with CORS on Safari --- src/matrix/net/common.js | 22 ++++++++++++++++++++++ src/matrix/net/request/fetch.js | 15 ++++++++++++++- src/matrix/net/request/xhr.js | 23 +---------------------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/matrix/net/common.js b/src/matrix/net/common.js index c7a06351..6a772463 100644 --- a/src/matrix/net/common.js +++ b/src/matrix/net/common.js @@ -26,3 +26,25 @@ export function encodeQueryParams(queryParams) { }) .join("&"); } + +export function addCacheBuster(urlStr, random = Math.random) { + // XHR doesn't have a good way to disable cache, + // so add a random query param + // see https://davidtranscend.com/blog/prevent-ie11-cache-ajax-requests/ + if (urlStr.includes("?")) { + urlStr = urlStr + "&"; + } else { + urlStr = urlStr + "?"; + } + return urlStr + `_cacheBuster=${Math.ceil(random() * Number.MAX_SAFE_INTEGER)}`; +} + +export function tests() { + return { + "add cache buster": assert => { + const random = () => 0.5; + assert.equal(addCacheBuster("http://foo", random), "http://foo?_cacheBuster=4503599627370496"); + assert.equal(addCacheBuster("http://foo?bar=baz", random), "http://foo?bar=baz&_cacheBuster=4503599627370496"); + } + } +} diff --git a/src/matrix/net/request/fetch.js b/src/matrix/net/request/fetch.js index 38624682..d1561692 100644 --- a/src/matrix/net/request/fetch.js +++ b/src/matrix/net/request/fetch.js @@ -20,6 +20,7 @@ import { ConnectionError } from "../../error.js"; import {abortOnTimeout} from "../timeout.js"; +import {addCacheBuster} from "../common.js"; class RequestResult { constructor(promise, controller) { @@ -57,11 +58,23 @@ export function createFetchRequest(createTimeout) { signal: controller.signal }); } + url = addCacheBuster(url); options = Object.assign(options, { mode: "cors", credentials: "omit", referrer: "no-referrer", - cache: "no-cache", + // ideally we'd turn off cache here, but Safari interprets + // `Access-Control-Allow-Headers` strictly (only when fetch is + // intercepted by a service worker strangely enough), in that + // it gives a CORS error if Cache-Control is not present + // in the list of allowed headers (which it isn't commonly, at least not on matrix.org). + // With no-store or no-cache here, it does set `Cache-Control` + // so we don't do that, and prevent caching with `addCacheBuster`. + // We also hope the server responds with `Cache-Control: no-store` so + // we don't fill the http cache with api responses. + // + // cache: "no-store", + cache: "default", }); if (options.headers) { const headers = new Headers(); diff --git a/src/matrix/net/request/xhr.js b/src/matrix/net/request/xhr.js index ed4a36c3..3cde1b14 100644 --- a/src/matrix/net/request/xhr.js +++ b/src/matrix/net/request/xhr.js @@ -18,6 +18,7 @@ import { AbortError, ConnectionError } from "../../error.js"; +import {addCacheBuster} from "../common.js"; class RequestResult { constructor(promise, xhr) { @@ -60,18 +61,6 @@ function xhrAsPromise(xhr, method, url) { }); } -function addCacheBuster(urlStr, random = Math.random) { - // XHR doesn't have a good way to disable cache, - // so add a random query param - // see https://davidtranscend.com/blog/prevent-ie11-cache-ajax-requests/ - if (urlStr.includes("?")) { - urlStr = urlStr + "&"; - } else { - urlStr = urlStr + "?"; - } - return urlStr + `_cacheBuster=${Math.ceil(random() * Number.MAX_SAFE_INTEGER)}`; -} - export function xhrRequest(url, options) { url = addCacheBuster(url); const xhr = send(url, options); @@ -85,13 +74,3 @@ export function xhrRequest(url, options) { }); return new RequestResult(promise, xhr); } - -export function tests() { - return { - "add cache buster": assert => { - const random = () => 0.5; - assert.equal(addCacheBuster("http://foo", random), "http://foo?_cacheBuster=4503599627370496"); - assert.equal(addCacheBuster("http://foo?bar=baz", random), "http://foo?bar=baz&_cacheBuster=4503599627370496"); - } - } -} From 5bba5778c41cb37e6f2121aa9d35a3f11d65333f Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 2 Oct 2020 14:04:55 +0200 Subject: [PATCH 2/2] release v0.1.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 362c6a52..15a06225 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hydrogen-web", - "version": "0.1.3", + "version": "0.1.4", "description": "A javascript matrix client prototype, trying to minize RAM usage by offloading as much as possible to IndexedDB", "main": "index.js", "directories": {