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:
flowkclav
2025-06-04 18:39:52 +02:00
committed by GitHub
co-authored by floakclav
parent 80b089b691
commit b1b1363574
3 changed files with 15 additions and 4 deletions
+8 -2
View File
@@ -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
View File
@@ -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();
}
+2 -1
View File
@@ -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);