- currently the setup in "opfs-vite" is working with the todos app (opfs works), so just piling on that with a bunch of scripts testing things.
- opfs doesn't work in a SharedWorker; wa-sqlite demo has a workaround wa-sqlite opfs
- rxdb opfs has something too rxdb opfs
// vite.config.js
import { defineConfig } from "vite";
export default defineConfig({
server: {
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
},
},
optimizeDeps: {
exclude: ["@sqlite.org/sqlite-wasm"],
},
});
// main thread using a worker
const worker = new Worker("worker.mjs", { type: "module" });
const log = console.log;
log("worker.postMessage hello world");
worker.postMessage({
type: "hello",
payload: "world",
});
worker.onmessage = (e) => {
log("worker.onmessage", JSON.stringify(e.data, null, 2));
const { type, payload } = e.data;
if (type === "log") {
console.log(payload);
}
};
// worker
const log = console.log;
self.onmessage = (e) => {
log("worker.onmessage");
const { type, payload } = e.data;
if (type === "init") {
log("worker.postMessage type log payload worker initialized");
self.postMessage({ type: "log", payload: "worker initialized" });
}
};
// main thread using a shared worker
const worker = new SharedWorker("worker.mjs", { type: "module" });
worker.port.postMessage({ type: "init" });
worker.port.onmessage = (e) => {
const { type, payload } = e.data;
if (type === "log") {
console.log(payload);
}
};
// shared worker
onconnect = (e) => {
const port = e.ports[0];
port.onmessage = (e) => {
const { type, payload } = e.data;
if (type === "log") {
console.log(payload);
}
};
port.postMessage({ type: "init" });
};