From 0aa2b2bd4532b3dd441c6fdbf057b4221571b9f0 Mon Sep 17 00:00:00 2001 From: Nystik <236107-Nystik@users.noreply.gitlab.com> Date: Tue, 16 Jun 2026 23:44:26 +0200 Subject: [PATCH] resolve workspace and appearance from the warm cache at boot --- packages/shim/src/init.js | 20 ++++--- packages/shim/src/native-menu-guard.js | 30 ++-------- packages/shim/src/workspace.js | 83 ++++++++------------------ 3 files changed, 45 insertions(+), 88 deletions(-) diff --git a/packages/shim/src/init.js b/packages/shim/src/init.js index 907c628..ae76072 100644 --- a/packages/shim/src/init.js +++ b/packages/shim/src/init.js @@ -171,8 +171,7 @@ function applyCoreSyncGuard(plugins) { return data; } - let text = - typeof data === "string" ? data : new TextDecoder().decode(data); + let text = typeof data === "string" ? data : new TextDecoder().decode(data); try { const config = JSON.parse(text); @@ -226,6 +225,13 @@ function updateBootProgress(received, total) { label.textContent = `Loading plugins... ${mb(received)}/${mb(total)} MB`; } +// Resolve the active workspace and snapshot the appearance config. +function resolveWorkspaceAndAppearance() { + resolveWorkspaceName(); + loadPresetIfRequested(); + initNativeMenuGuard(); +} + export function initialize() { if (maybeProvisionDemoVault()) { window.__ignisBootReady = Promise.resolve(); @@ -233,9 +239,6 @@ export function initialize() { } resolveVaultId(); - resolveWorkspaceName(); - loadPresetIfRequested(); - initNativeMenuGuard(window.__currentVaultId); const bootstrap = fetchBootstrap(); @@ -258,13 +261,16 @@ export function initialize() { { onProgress: updateBootProgress }, ); - window.__ignisBootReady = priority; + // Chain workspace/appearance resolution onto readiness so its config reads hit the warm priority slice instead of the network. + window.__ignisBootReady = priority.then(resolveWorkspaceAndAppearance); } else { - window.__ignisBootReady = Promise.resolve(); initVaultConfigFallback(); initVaultListFallback(); initMetadataCacheFallback(); initCoreSyncGuardFallback(); + // No prefetch on the fallback path, so resolve directly; the reads fall through to the network. + resolveWorkspaceAndAppearance(); + window.__ignisBootReady = Promise.resolve(); } installRequestUrlShim(); diff --git a/packages/shim/src/native-menu-guard.js b/packages/shim/src/native-menu-guard.js index 0cdff52..241f456 100644 --- a/packages/shim/src/native-menu-guard.js +++ b/packages/shim/src/native-menu-guard.js @@ -6,34 +6,16 @@ import { registerReadTransform, registerWriteTransform, } from "./fs/transforms.js"; +import { fsShim } from "./fs/index.js"; const APPEARANCE_PATH = ".obsidian/appearance.json"; // undefined = key absent on disk; write transform keeps it absent. let preservedNativeMenus = undefined; -function snapshotAppearance(vaultId) { - if (!vaultId) { - return; - } - +function snapshotAppearance() { try { - const xhr = new XMLHttpRequest(); - const url = - "/api/fs/readFile?vault=" + - encodeURIComponent(vaultId) + - "&path=" + - encodeURIComponent(APPEARANCE_PATH) + - "&encoding=utf-8"; - - xhr.open("GET", url, false); - xhr.send(); - - if (xhr.status !== 200) { - return; - } - - const obj = JSON.parse(xhr.responseText); + const obj = JSON.parse(fsShim.readFileSync(APPEARANCE_PATH, "utf-8")); if ("nativeMenus" in obj) { preservedNativeMenus = obj.nativeMenus; @@ -158,9 +140,9 @@ function disableNativeMenuToggle() { }); } -export function initNativeMenuGuard(vaultId) { - // Snapshot before registering transforms so the write transform has the original disk value to substitute back. - snapshotAppearance(vaultId); +export function initNativeMenuGuard() { + // Snapshot before registering the read transform so the captured value is the original on disk, not the forced value. + snapshotAppearance(); registerReadTransform(APPEARANCE_PATH, readTransform); registerWriteTransform(APPEARANCE_PATH, writeTransform); patchSetConfig(); diff --git a/packages/shim/src/workspace.js b/packages/shim/src/workspace.js index 99867cd..4116bff 100644 --- a/packages/shim/src/workspace.js +++ b/packages/shim/src/workspace.js @@ -86,71 +86,40 @@ export function loadPresetIfRequested() { } } -export function resolveWorkspaceName() { +function readJsonIfPresent(path) { try { - const vaultParam = window.__currentVaultId - ? "?vault=" + encodeURIComponent(window.__currentVaultId) - : ""; + return JSON.parse(fsShim.readFileSync(path, "utf-8")); + } catch { + return null; + } +} - const sep = vaultParam ? "&" : "?"; +export function resolveWorkspaceName() { + // With no URL param, only resolve a workspace when the workspaces core plugin is enabled. + if (!window.__workspaceName) { + const corePlugins = readJsonIfPresent(".obsidian/core-plugins.json"); - // If no param provided, check if workspaces plugin is enabled before resolving. - if (!window.__workspaceName) { - const coreXhr = new XMLHttpRequest(); - - coreXhr.open( - "GET", - "/api/fs/readFile" + - vaultParam + - sep + - "path=.obsidian/core-plugins.json&encoding=utf-8", - false, - ); - coreXhr.send(); - - if (coreXhr.status !== 200) { - return; - } - - const corePlugins = JSON.parse(coreXhr.responseText); - - if (!corePlugins.workspaces) { - return; - } - } - - // Read workspaces.json to get the active field. - const xhr = new XMLHttpRequest(); - - xhr.open( - "GET", - "/api/fs/readFile" + - vaultParam + - sep + - "path=.obsidian/workspaces.json&encoding=utf-8", - false, - ); - xhr.send(); - - if (xhr.status !== 200) { + if (!corePlugins || !corePlugins.workspaces) { return; } + } - const workspaces = JSON.parse(xhr.responseText); + const workspaces = readJsonIfPresent(WORKSPACES_PATH); - // Always store the original active value for the write transform. - if (workspaces.active) { - window.__originalActiveWorkspace = workspaces.active; - } + if (!workspaces) { + return; + } - // If no param was provided, seed from the active workspace. - if (!window.__workspaceName && workspaces.active) { - window.__workspaceName = workspaces.active; - setWorkspaceParam(workspaces.active); - console.log("[ignis] Workspace resolved from active:", workspaces.active); - } - } catch (e) { - console.warn("[ignis] Failed to resolve workspace name:", e); + // Keep the original active value so the write transform can restore it on disk. + if (workspaces.active) { + window.__originalActiveWorkspace = workspaces.active; + } + + // With no URL param, seed from the active workspace. + if (!window.__workspaceName && workspaces.active) { + window.__workspaceName = workspaces.active; + setWorkspaceParam(workspaces.active); + console.log("[ignis] Workspace resolved from active:", workspaces.active); } }