From 180f0b211554e6d9fc1d5c26c9e6df4189ede34b Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Sun, 6 Oct 2024 12:11:31 +0200 Subject: [PATCH] fix worker run --- cli/build_worker.ts | 19 +++++++++++++++---- cli/cms_worker.ts | 19 +++++++++++++++---- cli/missing_worker_apis.ts | 17 ++++++++--------- core/writer.ts | 1 + 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/cli/build_worker.ts b/cli/build_worker.ts index 81893ee6..91280320 100644 --- a/cli/build_worker.ts +++ b/cli/build_worker.ts @@ -11,7 +11,19 @@ import noCors from "../middlewares/no_cors.ts"; import notFound from "../middlewares/not_found.ts"; import reload from "../middlewares/reload.ts"; import { buildSite } from "./utils.ts"; -import "./missing_worker_apis.ts"; +import { initLocalStorage } from "./missing_worker_apis.ts"; + +addEventListener("message", (event) => { + const { type } = event.data; + + if (type === "build" || type === "rebuild") { + return build(event.data); + } + + if (type === "localStorage") { + return initLocalStorage(event.data.data); + } +}); interface BuildOptions { type: "build" | "rebuild"; @@ -19,8 +31,7 @@ interface BuildOptions { serve?: boolean; } -onmessage = async (event) => { - const { type, config, serve } = event.data as BuildOptions; +async function build({ type, config, serve }: BuildOptions) { const site = await buildSite(config); // Set the live reload environment variable to add hash to the URLs in the module loader @@ -122,4 +133,4 @@ onmessage = async (event) => { } server.start(); -}; +} diff --git a/cli/cms_worker.ts b/cli/cms_worker.ts index eef56236..39a92385 100644 --- a/cli/cms_worker.ts +++ b/cli/cms_worker.ts @@ -6,15 +6,26 @@ import { normalizePath } from "../core/utils/path.ts"; import { fromFileUrl } from "../deps/path.ts"; import { setEnv } from "../core/utils/env.ts"; import { createSite } from "./utils.ts"; -import "./missing_worker_apis.ts"; +import { initLocalStorage } from "./missing_worker_apis.ts"; + +addEventListener("message", (event) => { + const { type } = event.data; + + if (type === "build" || type === "rebuild") { + return build(event.data); + } + + if (type === "localStorage") { + return initLocalStorage(event.data.data); + } +}); interface CMSOptions { type: "build" | "rebuild"; config?: string; } -onmessage = async (event) => { - const { type, config } = event.data as CMSOptions; +async function build({ type, config }: CMSOptions) { const cmsConfig = await getConfigFile(undefined, ["_cms.ts", "_cms.js"]); if (!cmsConfig) { @@ -79,4 +90,4 @@ onmessage = async (event) => { } }, }); -}; +} diff --git a/cli/missing_worker_apis.ts b/cli/missing_worker_apis.ts index de1b02ff..e3556108 100644 --- a/cli/missing_worker_apis.ts +++ b/cli/missing_worker_apis.ts @@ -52,16 +52,15 @@ class S implements Storage { } } -globalThis.addEventListener("message", (event) => { - if (event.data.type === "localStorage") { - const storage = new S("localStorage"); +// sessionStorage is always empty +globalThis.sessionStorage = new S(); - for (const [key, value] of Object.entries(event.data.data)) { - storage.setItem(key, value); - } +export function initLocalStorage(data: Record) { + const storage = new S("localStorage"); - globalThis.localStorage = storage; + for (const [key, value] of Object.entries(data)) { + storage.setItem(key, value); } -}); -globalThis.sessionStorage = new S(); + globalThis.localStorage = storage; +} diff --git a/core/writer.ts b/core/writer.ts index 4037f3c9..8f738d69 100644 --- a/core/writer.ts +++ b/core/writer.ts @@ -71,6 +71,7 @@ export class FSWriter implements Writer { } const filename = posix.join(this.dest, outputPath); + console.log(page.data.url, page.src); const id = this.caseSensitiveUrls ? filename : filename.toLowerCase(); const hash = await sha1(content); const previous = this.#outputs.get(id);