Skip to content
Snippets Groups Projects
Verified Commit 462d7336 authored by Dorian Koch's avatar Dorian Koch
Browse files

Begin smart caching

parent cdfd1821
No related branches found
No related tags found
No related merge requests found
Pipeline #6379 passed
...@@ -67,6 +67,10 @@ export class BackendImpl { ...@@ -67,6 +67,10 @@ export class BackendImpl {
private csrfToken?: string; private csrfToken?: string;
public isPrivileged = false; public isPrivileged = false;
protected fetchImpl(url: string, init?: RequestInit): Promise<Response> {
return fetch(url, init);
}
assetUrl(): string { assetUrl(): string {
return "https://video.fsmpi.rwth-aachen.de/files"; return "https://video.fsmpi.rwth-aachen.de/files";
} }
...@@ -135,7 +139,7 @@ export class BackendImpl { ...@@ -135,7 +139,7 @@ export class BackendImpl {
init.cache = "no-cache"; init.cache = "no-cache";
} }
init.credentials = "include"; init.credentials = "include";
return fetch(url, init); return this.fetchImpl(url, init);
} }
setCsrfToken(csrfToken?: string) { setCsrfToken(csrfToken?: string) {
...@@ -505,3 +509,38 @@ export class BackendImpl { ...@@ -505,3 +509,38 @@ export class BackendImpl {
}); });
} }
} }
export class SmartCacheBackend extends BackendImpl {
protected fetchImpl(url: string, init?: RequestInit) {
let shouldCache = true;
if (!SmartCacheBackend.isSupported()) shouldCache = false;
if (init && init.cache === "no-cache") shouldCache = false;
if (!url.startsWith(this.baseUrl())) shouldCache = false;
if (!shouldCache) return super.fetchImpl(url, init);
const cacheName = `api-v1-${this.isPrivileged ? "privileged" : "public"}`;
return caches.open(cacheName).then((cache) => {
return cache.match(url).then((response) => {
if (response) {
console.log(`Cache hit (${cacheName}) on ${url}`);
return response;
} else {
return fetch(url, init).then((response) => {
cache.put(url, response.clone());
return response;
});
}
});
});
}
static isSupported() {
return window.isSecureContext && "caches" in window;
}
}
export class InvalidBackend extends BackendImpl {
protected fetchImpl(url: string, init?: RequestInit): Promise<Response> {
return Promise.reject(new Error("Invalid backend"));
}
}
import { BackendImpl } from "@/api/Backend"; import { Backend, InvalidBackend, SmartCacheBackend } from "@/api/Backend";
import { createContext, useContext, useState } from "react"; import { createContext, useContext, useState } from "react";
import type React from "react"; import type React from "react";
const BackendContext = createContext<BackendImpl>(new BackendImpl()); const BackendContext = createContext<Backend>(new InvalidBackend());
export function RealBackendProvider({ children }: { children: React.ReactNode }) { export function RealBackendProvider({ children }: { children: React.ReactNode }) {
let [state, _] = useState(() => new BackendImpl()); let [state, _] = useState(() => new SmartCacheBackend());
return <BackendContext.Provider value={state}>{children}</BackendContext.Provider>; return <BackendContext.Provider value={state}>{children}</BackendContext.Provider>;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment