-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the service worker to cache requests.
- Loading branch information
Showing
5 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
const CACHE_NAME = "TwinteFallback"; | ||
const VERSIONS = ["v1"]; | ||
|
||
let base_url: string; | ||
|
||
if (process.env.NODE_ENV === "development") { | ||
base_url = process.env.BASE_URL || "http://localhost:4000"; | ||
} else { | ||
base_url = process.env.BASE_URL; | ||
} | ||
|
||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
self.addEventListener("install", () => { | ||
VERSIONS.slice(0, Math.min(0, VERSIONS.length - 2)).forEach( | ||
async (version) => { | ||
await caches.delete(getCacheName(CACHE_NAME, version)); | ||
} | ||
); | ||
}); | ||
|
||
self.addEventListener("fetch", (event) => { | ||
const request = event.request; | ||
|
||
if (request.method !== "GET") return; | ||
if ( | ||
!( | ||
request.url.startsWith(base_url) || | ||
request.url.startsWith("https://fonts.googleapis.com/") || | ||
request.url.startsWith("https://fonts.gstatic.com/") | ||
) | ||
) | ||
return; | ||
|
||
event.respondWith(getResponseWithCaching(request)); | ||
}); | ||
|
||
const getCacheName = (name: string, version: string) => `${name}_${version}`; | ||
|
||
const getResponseWithCaching = async (request: Request): Promise<Response> => { | ||
const cache = await caches.open( | ||
getCacheName(CACHE_NAME, VERSIONS[VERSIONS.length - 1]) | ||
); | ||
|
||
let response: Response | undefined; | ||
|
||
const hasCredentials = request.url.startsWith(base_url); | ||
|
||
try { | ||
response = await fetch( | ||
request, | ||
hasCredentials | ||
? { | ||
credentials: "include", | ||
} | ||
: {} | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
response = await cache.match(request.url); | ||
if (!response) throw new Error("There is no cache"); | ||
} | ||
|
||
if (response.ok) cache.put(request, response.clone()); | ||
|
||
return response; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"include": ["./**/*.ts"], | ||
"compilerOptions": { | ||
"sourceMap": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"preserveValueImports": false, | ||
"esModuleInterop": true, | ||
"importsNotUsedAsValues": "remove", | ||
"baseUrl": ".", | ||
"paths": { | ||
"~/*": ["./*"] | ||
}, | ||
"lib": ["ES2021", "WebWorker"], | ||
"types": ["node"], | ||
"outDir": "../dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters