Fixing service worker and local storage conflicts when hosting multiple instances on same origin, different URL prefix but same space path (#1415)
Co-authored-by: floakclav <floakclav@gmail.com>
This commit is contained in:
+8
-2
@@ -1,7 +1,7 @@
|
||||
import { safeRun } from "$lib/async.ts";
|
||||
import { Client, type ClientConfig } from "./client.ts";
|
||||
|
||||
const configCacheKey = "config";
|
||||
const configCacheKey = `silverbullet.${document.baseURI}.config`;
|
||||
|
||||
safeRun(async () => {
|
||||
// First we attempt to fetch the config from the server
|
||||
@@ -50,9 +50,15 @@ safeRun(async () => {
|
||||
}
|
||||
if (navigator.serviceWorker) {
|
||||
// Register service worker
|
||||
const workerURL = new URL("service_worker.js", document.baseURI);
|
||||
navigator.serviceWorker
|
||||
.register(new URL("service_worker.js", document.baseURI), {
|
||||
.register(workerURL, {
|
||||
type: "module",
|
||||
//limit the scope of the service worker to any potential URL prefix
|
||||
scope: workerURL.pathname.substring(
|
||||
0,
|
||||
workerURL.pathname.lastIndexOf("/") + 1,
|
||||
),
|
||||
})
|
||||
.then((registration) => {
|
||||
console.log("Service worker registered...");
|
||||
|
||||
+5
-1
@@ -145,7 +145,11 @@ export class Client {
|
||||
) {
|
||||
// Generate a semi-unique prefix for the database so not to reuse databases for different space paths
|
||||
this.dbPrefix = "" +
|
||||
simpleHash(clientConfig.spaceFolderPath);
|
||||
simpleHash(
|
||||
`${clientConfig.spaceFolderPath}:${
|
||||
document.baseURI.replace(/\/*$/, "")
|
||||
}`,
|
||||
);
|
||||
this.onLoadRef = parseRefFromURI();
|
||||
}
|
||||
|
||||
|
||||
@@ -217,7 +217,8 @@ self.addEventListener("message", (event: any) => {
|
||||
}
|
||||
case "config": {
|
||||
const spaceFolderPath = event.data.config.spaceFolderPath;
|
||||
const dbPrefix = "" + simpleHash(spaceFolderPath);
|
||||
const dbPrefix = "" +
|
||||
simpleHash(`${spaceFolderPath}:${baseURI.replace(/\/*$/, "")}`);
|
||||
|
||||
// Setup space
|
||||
const kv = new IndexedDBKvPrimitives(dbPrefix);
|
||||
|
||||
Reference in New Issue
Block a user