(function () { // Key used to track if the page has been reloaded to fetch fresh data const LOCAL_STORAGE_RELOAD_KEY = "open-in-storyblok-reloaded"; // Check if this is the first time loading the bookmarklet const hasBeenReloaded = localStorage.getItem(LOCAL_STORAGE_RELOAD_KEY); if (!hasBeenReloaded) { // Set flag and reload page to ensure we have fresh Next.js data localStorage.setItem(LOCAL_STORAGE_RELOAD_KEY, "true"); window.location.reload(); return; } // Clean up the reload flag after successful reload localStorage.removeItem(LOCAL_STORAGE_RELOAD_KEY); // Configuration: Map domain names to environment names const DOMAIN_TO_ENV = { "localhost": "dev", "envdevel.com": "dev", "vercel.app": "dev", "envstage.com": "stage", "engelvoelkers.com": "prod", }; // Configuration: Map environments to their Storyblok space IDs const ENV_TO_SPACE_ID = { dev: 181774, stage: 208867, prod: 209096, }; /** * Extracts the main domain from the current URL * Example: "subdomain.envdevel.com" -> "envdevel.com" */ function extractMainDomain() { const hostname = window.location.hostname; const lastDotIndex = hostname.lastIndexOf("."); const secondLastDotIndex = hostname.lastIndexOf(".", lastDotIndex - 1); return hostname.substring(secondLastDotIndex + 1); } /** * Builds the Storyblok editor URL for a specific story */ function buildStoryblokEditorUrl(spaceId, storyId) { return `https://app.storyblok.com/#/me/spaces/${spaceId}/stories/0/0/${storyId}`; } /** * Gets the current story ID from Next.js data */ function getCurrentStoryId() { return window?.__NEXT_DATA__?.props?.pageProps?.story?.id; } // Main execution try { const storyId = getCurrentStoryId(); if (!storyId) { throw new Error("No story ID found on this page"); } const mainDomain = extractMainDomain(); const environment = DOMAIN_TO_ENV[mainDomain]; if (!environment) { throw new Error(`Unknown domain: ${mainDomain}`); } const spaceId = ENV_TO_SPACE_ID[environment]; const editorUrl = buildStoryblokEditorUrl(spaceId, storyId); window.open(editorUrl, "_blank"); } catch (error) { alert(error.message); } })();