mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
Cache config.json
This commit is contained in:
parent
6cd3c8ee2b
commit
468b7e1595
@ -171,7 +171,7 @@ export class Platform {
|
|||||||
if (!this._configURL) {
|
if (!this._configURL) {
|
||||||
throw new Error("Neither config nor configURL was provided!");
|
throw new Error("Neither config nor configURL was provided!");
|
||||||
}
|
}
|
||||||
const {body}= await this.request(this._configURL, {method: "GET", format: "json"}).response();
|
const {body}= await this.request(this._configURL, {method: "GET", format: "json", cache: true}).response();
|
||||||
this._config = body;
|
this._config = body;
|
||||||
}
|
}
|
||||||
this._notificationService = new NotificationService(
|
this._notificationService = new NotificationService(
|
||||||
|
@ -75,8 +75,15 @@ self.addEventListener('fetch', (event) => {
|
|||||||
This has to do with xhr not being supported in service workers.
|
This has to do with xhr not being supported in service workers.
|
||||||
*/
|
*/
|
||||||
if (event.request.method === "GET") {
|
if (event.request.method === "GET") {
|
||||||
|
if (event.request.url.includes("config.json")) {
|
||||||
|
/**
|
||||||
|
* Use a different strategy for this file.
|
||||||
|
*/
|
||||||
|
event.respondWith(handleConfigRequest(event.request));
|
||||||
|
} else {
|
||||||
event.respondWith(handleRequest(event.request));
|
event.respondWith(handleRequest(event.request));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function isCacheableThumbnail(url) {
|
function isCacheableThumbnail(url) {
|
||||||
@ -119,6 +126,32 @@ async function handleRequest(request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleConfigRequest(request) {
|
||||||
|
const url = new URL(request.url);
|
||||||
|
// rewrite / to /index.html so it hits the cache
|
||||||
|
if (url.origin === baseURL.origin && url.pathname === baseURL.pathname) {
|
||||||
|
request = new Request(new URL("index.html", baseURL.href));
|
||||||
|
}
|
||||||
|
let response = await readCache(request);
|
||||||
|
if (response) {
|
||||||
|
fetchAndUpdateConfig(request);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
response = await fetchAndUpdateConfig(request);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchAndUpdateConfig(request) {
|
||||||
|
const response = await fetch(request, {
|
||||||
|
signal: pendingFetchAbortController.signal,
|
||||||
|
headers: {
|
||||||
|
"Cache-Control": "no-cache",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
updateCache(request, response.clone());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
async function updateCache(request, response) {
|
async function updateCache(request, response) {
|
||||||
// don't write error responses to the cache
|
// don't write error responses to the cache
|
||||||
if (response.status >= 400) {
|
if (response.status >= 400) {
|
||||||
|
@ -14,6 +14,11 @@ export default defineConfig(({mode}) => {
|
|||||||
outDir: "../../../target",
|
outDir: "../../../target",
|
||||||
minify: true,
|
minify: true,
|
||||||
sourcemap: true,
|
sourcemap: true,
|
||||||
|
rollupOptions: {
|
||||||
|
output: {
|
||||||
|
assetFileNames: (asset) => asset.name.includes("config.json") ? "assets/[name][extname]": "assets/[name].[hash][extname]",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
themeBuilder({
|
themeBuilder({
|
||||||
|
Loading…
Reference in New Issue
Block a user