Added IndexedDB
This commit is contained in:
parent
96f7f8462b
commit
12ce54e95d
@ -22,6 +22,7 @@
|
|||||||
"webpack-dev-server": "^3.11.2"
|
"webpack-dev-server": "^3.11.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"idb": "^6.1.2",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"wouter": "^2.7.4"
|
"wouter": "^2.7.4"
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
export const API_URL = process.env.PUBLIC_API_URL;
|
export const API_URL = process.env.PUBLIC_API_URL;
|
||||||
// export const BASE_URL = process.env.PUBLIC_BASE_URL;
|
// export const BASE_URL = process.env.PUBLIC_BASE_URL;
|
||||||
export const CACHE = "v1";
|
export const CACHE = "v1";
|
||||||
|
export const DB_NAME = "publite";
|
||||||
|
export const DB_VERSION = 1;
|
||||||
|
13
src/serviceWorker/db.ts
Normal file
13
src/serviceWorker/db.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import * as idb from "idb";
|
||||||
|
import { DB_NAME, DB_VERSION } from "../constants";
|
||||||
|
import { PubliteDB } from "./schema";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens IndexedDB for interactions
|
||||||
|
*/
|
||||||
|
export const openDB = () =>
|
||||||
|
idb.openDB<PubliteDB>(DB_NAME, DB_VERSION, {
|
||||||
|
upgrade: (db, oldVersion, _, tsx) => {
|
||||||
|
if (oldVersion < 1) db.createObjectStore("Books", { keyPath: "hash" });
|
||||||
|
},
|
||||||
|
});
|
@ -1,4 +1,5 @@
|
|||||||
import { precache } from "./cache";
|
import { precache } from "./cache";
|
||||||
|
import { openDB as createDB } from "./db";
|
||||||
|
|
||||||
declare const self: ServiceWorkerGlobalScope;
|
declare const self: ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
@ -7,4 +8,4 @@ self.addEventListener("install", (event) => {
|
|||||||
event.waitUntil(precache());
|
event.waitUntil(precache());
|
||||||
});
|
});
|
||||||
|
|
||||||
export type {};
|
self.addEventListener("activate", (event) => event.waitUntil(createDB()));
|
||||||
|
9
src/serviceWorker/schema.ts
Normal file
9
src/serviceWorker/schema.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { DBSchema } from "idb";
|
||||||
|
import { BookT } from "../types/book";
|
||||||
|
|
||||||
|
export interface PubliteDB extends DBSchema {
|
||||||
|
Books: {
|
||||||
|
key: string;
|
||||||
|
value: BookT;
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user