From fae90cc85e99a6755b633a9653de661081803430 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sun, 21 Sep 2025 06:01:47 +0000 Subject: [PATCH] refresh functionality fix --- iframe-fetch-client/_iframe-client/main.js | 223 +++++++++++++++++++-- iframe-fetch-client/sw.js | 91 ++++++--- 2 files changed, 266 insertions(+), 48 deletions(-) diff --git a/iframe-fetch-client/_iframe-client/main.js b/iframe-fetch-client/_iframe-client/main.js index 3b6212ce..4a731d37 100644 --- a/iframe-fetch-client/_iframe-client/main.js +++ b/iframe-fetch-client/_iframe-client/main.js @@ -13,6 +13,11 @@ class JSONRPCClient { setupMessageListener() { window.addEventListener("message", (event) => { + // Only accept messages from parent window for security + if (event.source !== window.parent) { + return; + } + const message = event.data; // Check if this is a response to our own request or a ServiceWorker request if (message.jsonrpc === "2.0" && message.id !== undefined) { @@ -153,15 +158,38 @@ class NavigationHandler { constructor() { this.historyIndex = 0; // Track current position in history this.historyLength = 1; // Track total history length - this.currentSemanticPath = '/'; // Always start with root path for SPAs + + // Check for initial path set by loadSiteIndex + if (window.__IFRAME_INITIAL_PATH__) { + this.currentSemanticPath = window.__IFRAME_INITIAL_PATH__; + console.log('NavigationHandler initialized with path:', this.currentSemanticPath); + } else { + this.currentSemanticPath = '/'; // Default to root path + } + this.setupMessageListener(); this.setupHistoryListeners(); this.overrideHistoryMethods(); this.updateNavigationState(); } + // Helper method to update semantic path and ensure it's properly tracked + updateSemanticPath(path) { + if (typeof path === 'string') { + this.currentSemanticPath = path; + } else { + // Fallback to current location if path is invalid + this.currentSemanticPath = window.location.pathname + window.location.search + window.location.hash; + } + } + setupMessageListener() { window.addEventListener("message", (event) => { + // Only accept messages from parent window for security + if (event.source !== window.parent) { + return; + } + const message = event.data; if (message.jsonrpc === "2.0") { switch (message.method) { @@ -188,13 +216,15 @@ class NavigationHandler { // When user navigates (back or forward), we need to update our tracking // The state object can help us determine our position // Update current semantic path based on actual URL - this.currentSemanticPath = window.location.pathname + window.location.search + window.location.hash; + const newPath = window.location.pathname + window.location.search + window.location.hash; + this.updateSemanticPath(newPath); this.updateNavigationState(); }); // Listen for hash changes window.addEventListener('hashchange', () => { - this.currentSemanticPath = window.location.pathname + window.location.search + window.location.hash; + const newPath = window.location.pathname + window.location.search + window.location.hash; + this.updateSemanticPath(newPath); this.updateNavigationState(); }); } @@ -223,7 +253,7 @@ class NavigationHandler { } // pushState was called, update semantic path - this.currentSemanticPath = semanticPath; + this.updateSemanticPath(semanticPath); this.updateNavigationState(); return result; }; @@ -245,7 +275,7 @@ class NavigationHandler { } // replaceState was called, update semantic path - this.currentSemanticPath = semanticPath; + this.updateSemanticPath(semanticPath); this.updateNavigationState(); return result; }; @@ -263,22 +293,17 @@ class NavigationHandler { // Extract the semantic path (pathname + search + hash) const semanticPath = targetUrl.pathname + targetUrl.search + targetUrl.hash; - // Use browser's history API to trigger SPA navigation - // This triggers SPA router navigation the semantic path to the parent for display - // The SPA's router will handle the navigation internally - - // Use history.pushState to trigger SPA navigation - this.currentSemanticPath = semanticPath; + // Update our tracked semantic path using helper + this.updateSemanticPath(semanticPath); this.updateNavigationState(); - // Use browser's history.pushState to trigger SPA navigation the SPA of the navigation intent + // Use browser's history.pushState to trigger SPA navigation // This is what most SPAs (React Router, Vue Router, etc.) listen for window.history.pushState({}, '', semanticPath); - - // URL change - Many SPAs listen for popstate events + + // Dispatch popstate event to trigger SPA router navigation + // Many SPAs listen for popstate events to handle navigation window.dispatchEvent(new PopStateEvent('popstate', { state: {} })); - - // Update navigation state to parent } catch (error) { console.error('Invalid navigation URL:', url, error); @@ -286,9 +311,19 @@ class NavigationHandler { } handleRefresh() { - // Reload the iframe by going back to base URL and then reloading - // This prevents trying to fetch non-existent SPA routes - window.location.replace('/'); + // Store current semantic path for reload + const currentPath = this.currentSemanticPath || '/'; + + // Save the current path to sessionStorage so it persists across reload + try { + sessionStorage.setItem('iframe_initial_path', currentPath); + } catch (error) { + console.warn('Failed to store initial path in sessionStorage:', error); + } + + // Actually reload the iframe to get a fresh SPA instance + // This will trigger loadSiteIndex again, which will read the stored path + window.location.reload(); } handleGoBack() { @@ -306,7 +341,13 @@ class NavigationHandler { updateNavigationState() { // Use tracked semantic path instead of window.location // This allows iframe to stay on base URL while showing semantic paths - const semanticPath = this.currentSemanticPath || (window.location.pathname + window.location.search + window.location.hash); + let semanticPath = this.currentSemanticPath; + + // Fallback to current location if semantic path is not set + if (!semanticPath || semanticPath === '') { + semanticPath = window.location.pathname + window.location.search + window.location.hash; + this.updateSemanticPath(semanticPath); + } const state = { currentUrl: semanticPath, @@ -330,17 +371,68 @@ class NavigationHandler { // Main Fetch Client class FetchClient { constructor() { + console.log("FetchClient: Initializing..."); this.rpcClient = new JSONRPCClient(); this.consoleInterceptor = new ConsoleInterceptor(); this.navigationHandler = new NavigationHandler(); this.init(); } + // Ensure SPA navigates to initial path after document is loaded + ensureInitialNavigation() { + console.log('ensureInitialNavigation: Starting...'); + + // Wait for the SPA to initialize, then navigate to the initial path + setTimeout(() => { + // Check multiple sources for the initial path + let initialPath = window.__IFRAME_INITIAL_PATH__; + + // Try sessionStorage backup + if (!initialPath) { + try { + initialPath = sessionStorage.getItem('iframe_initial_path_backup'); + sessionStorage.removeItem('iframe_initial_path_backup'); + if (initialPath) { + window.__IFRAME_INITIAL_PATH__ = initialPath; + console.log('ensureInitialNavigation: Restored path from sessionStorage:', initialPath); + } + } catch (e) { + console.warn('ensureInitialNavigation: Failed to read sessionStorage backup:', e); + } + } + + if (initialPath && initialPath !== '/') { + const currentPath = window.location.pathname + window.location.search + window.location.hash; + + console.log('ensureInitialNavigation: Current path:', currentPath, 'Target path:', initialPath); + + // If we're not already at the initial path, navigate there + if (currentPath !== initialPath) { + console.log('ensureInitialNavigation: Navigating to initial path:', initialPath); + + // Use a small delay to ensure the SPA is fully initialized + setTimeout(() => { + console.log('ensureInitialNavigation: Executing navigation...'); + this.navigationHandler.handleNavigate(initialPath); + }, 50); + } else { + console.log('ensureInitialNavigation: Already at target path, no navigation needed'); + } + } else { + console.log('ensureInitialNavigation: No initial path or path is root, skipping navigation'); + } + }, 150); // Give SPA more time to initialize + } + async init() { try { + console.log("FetchClient: Setting up ServiceWorker..."); await this.setupServiceWorker(); + console.log("FetchClient: Loading site index..."); await this.loadSiteIndex(); + console.log("FetchClient: Initialization complete"); } catch (error) { + console.error("FetchClient: Initialization failed:", error); this.showError("Failed to initialize fetch client", error); } } @@ -392,6 +484,32 @@ class FetchClient { } async loadSiteIndex() { + console.log("loadSiteIndex: Starting..."); + + // Check if we have a stored initial path from a refresh + let initialPath = '/'; + try { + const storedPath = sessionStorage.getItem('iframe_initial_path'); + if (storedPath) { + initialPath = storedPath; + // Clear the stored path after using it + sessionStorage.removeItem('iframe_initial_path'); + console.log('loadSiteIndex: Loading SPA with initial path:', initialPath); + } + } catch (error) { + console.warn('loadSiteIndex: Failed to read initial path from sessionStorage:', error); + } + + // Store the initial path in sessionStorage for the new document to access + if (initialPath && initialPath !== '/') { + try { + sessionStorage.setItem('spa_initial_path', initialPath); + console.log('loadSiteIndex: Stored initial path for new document:', initialPath); + } catch (error) { + console.warn('loadSiteIndex: Failed to store initial path for new document:', error); + } + } + const serializedRequest = { url: `${location.origin}/`, method: "GET", @@ -410,12 +528,72 @@ class FetchClient { } const bytes = new Uint8Array(atob(response.body).split('').map(c => c.charCodeAt(0))); - const text = new TextDecoder().decode(bytes); + let text = new TextDecoder().decode(bytes); + + // Inject a script to set the initial path before the SPA loads + const initialPathScript = ` + + `; + + // Inject the script at the beginning of the head to ensure it runs before the SPA + text = text.replace('', `${initialPathScript}`); this.replaceDocument(text); } replaceDocument(htmlContent) { + console.log("replaceDocument: Starting..."); + + // Restore the initial path from sessionStorage before replacing document + let initialPath = '/'; + try { + const storedPath = sessionStorage.getItem('spa_initial_path'); + if (storedPath) { + initialPath = storedPath; + sessionStorage.removeItem('spa_initial_path'); + console.log('replaceDocument: Restored initial path:', initialPath); + // Set it on window for the new document + window.__IFRAME_INITIAL_PATH__ = initialPath; + } + } catch (error) { + console.warn('replaceDocument: Failed to restore initial path:', error); + } + // Create a new document from the HTML content const parser = new DOMParser(); const newDoc = parser.parseFromString(htmlContent, "text/html"); @@ -447,6 +625,9 @@ class FetchClient { }); console.log("Document replaced with site content"); + + // Ensure initial navigation after document is loaded + this.ensureInitialNavigation(); } showError(message, error) { diff --git a/iframe-fetch-client/sw.js b/iframe-fetch-client/sw.js index 9338592f..12498bcf 100644 --- a/iframe-fetch-client/sw.js +++ b/iframe-fetch-client/sw.js @@ -68,23 +68,37 @@ async function requestViaJSONRPC(request) { return new Promise(async (resolve, reject) => { const id = ++requestIdCounter; - // Serialize the fetch request - let body = null; - if (request.body) { - try { - // Convert body to Uint8Array then base64 encode - const bytes = await request.bytes(); - body = btoa(String.fromCharCode(...bytes)); - } catch (error) { - reject(new Error(`Failed to serialize request body: ${error.message}`)); - return; + // Check if request is a plain object or Request object + let url, method, headers, body; + + if (request instanceof Request) { + // Handle Request object + url = request.url; + method = request.method; + headers = Object.fromEntries(request.headers.entries()); + + if (request.body) { + try { + // Convert body to Uint8Array then base64 encode + const bytes = await request.bytes(); + body = btoa(String.fromCharCode(...bytes)); + } catch (error) { + reject(new Error(`Failed to serialize request body: ${error.message}`)); + return; + } } + } else { + // Handle plain request object + url = request.url; + method = request.method || 'GET'; + headers = request.headers || {}; + body = request.body || null; } const serializedRequest = { - url: request.url, - method: request.method, - headers: Object.fromEntries(request.headers.entries()), + url: url, + method: method, + headers: headers, body: body, }; @@ -109,7 +123,7 @@ async function requestViaJSONRPC(request) { setTimeout(() => { if (pendingRequests.has(id)) { pendingRequests.delete(id); - reject(new Error(`Timeout requesting: ${request.url}`)); + reject(new Error(`Timeout requesting: ${url}`)); } }, 10000); }); @@ -119,37 +133,60 @@ async function requestViaJSONRPC(request) { self.addEventListener("fetch", (event) => { const url = new URL(event.request.url); + // Debug logging for fetch interception + console.log("ServiceWorker fetch:", url.pathname); + // Ignore requests to other origins if (url.origin !== location.origin) { + console.log("Ignoring different origin:", url.origin); return; } // Let iframe client system files be handled normally if (url.pathname.startsWith("/_") || url.pathname === "/sw.js") { + console.log("Serving iframe client system file:", url.pathname); return; } // Let root path be handled normally (loads the iframe client page) if (url.pathname === "/") { + console.log("Serving root path (iframe client)"); return; } + // Let iframe client assets be handled normally (anything in _iframe-client directory and common assets) + if (url.pathname.startsWith("/_iframe-client/") || + url.pathname === "/favicon.ico" || + url.pathname === "/manifest.webmanifest") { + console.log("Serving iframe client asset:", url.pathname); + return; + } + + console.log("Intercepting request for project:", url.pathname); + // For SPA routes, check if this is a navigation request (HTML page request) - // If it's a navigation request for an SPA route, serve the site's index.html via JSON-RPC + // But first check if we need to load the iframe client (this happens during refresh) if (event.request.mode === "navigate" && (event.request.headers.get("accept") || "").includes("text/html")) { - // This is an SPA route navigation request, serve the site's index.html - // Create a new request for the root path to get the site's index.html - const rootRequest = new Request("/", { - method: "GET", - headers: event.request.headers, - mode: event.request.mode, - credentials: event.request.credentials, - cache: event.request.cache, - redirect: event.request.redirect, - referrer: event.request.referrer, - referrerPolicy: event.request.referrerPolicy, - }); + + // Check if this is a request for the iframe client itself + // We need to serve the iframe client HTML first, which will then load the SPA + if (url.pathname !== "/") { + console.log("Navigation request to non-root path, redirecting to iframe client first"); + + // Redirect to root to load the iframe client, which will then navigate to the target path + const response = Response.redirect(new URL("/", event.request.url).href, 302); + event.respondWith(response); + return; + } + + // This is a request for the root path, serve the site's index.html via JSON-RPC + const rootRequest = { + url: new URL("/", event.request.url).href, + method: event.request.method, + headers: Object.fromEntries(event.request.headers.entries()), + body: event.request.body + }; event.respondWith(handleRequest(rootRequest)); return; }