diff --git a/cptr/frontend/src/lib/apis/browser.ts b/cptr/frontend/src/lib/apis/browser.ts index 7fa6e0f..d9b513f 100644 --- a/cptr/frontend/src/lib/apis/browser.ts +++ b/cptr/frontend/src/lib/apis/browser.ts @@ -30,7 +30,7 @@ export const updateBrowserSession = (sessionId: string, url: string, title: stri export const browserFrameUrl = (sessionId: string, rawUrl: string) => { const normalized = /^https?:\/\//i.test(rawUrl) ? rawUrl : `https://${rawUrl}`; const url = new URL(normalized); - return `/api/browser/frame/${sessionId}?url=${encodeURIComponent(url.href)}`; + return `/api/browser/frame/${sessionId}/${url.protocol.slice(0, -1)}/${encodeURIComponent(url.host)}${url.pathname}${url.search}${url.hash}`; }; export const browserBlankUrl = (sessionId: string) => `/api/browser/sessions/${sessionId}/blank`; diff --git a/cptr/frontend/src/lib/components/BrowserPreview.svelte b/cptr/frontend/src/lib/components/BrowserPreview.svelte index 7058f22..d6adf71 100644 --- a/cptr/frontend/src/lib/components/BrowserPreview.svelte +++ b/cptr/frontend/src/lib/components/BrowserPreview.svelte @@ -1,7 +1,12 @@ ' f'' - '' + '' ) rendered = "".join(parser.output) + if urlsplit(final_url).hostname in {"localhost", "127.0.0.1", "::1"}: + rendered = rewrite_javascript(rendered, final_url, session_id) if re.search(r"]*)?>", rendered, re.I): return re.sub(r"(]*)?>)", r"\1" + runtime, rendered, count=1, flags=re.I) return runtime + rendered @@ -236,4 +239,13 @@ def rewrite_css(content: str, base_url: str, session_id: str) -> str: ) +def rewrite_javascript(content: str, base_url: str, session_id: str) -> str: + """Rewrite root-relative ES module imports used by local dev servers.""" + return re.sub( + r"((?:from\s*|import\s*\(\s*|import\s*)['\"])(/[^'\"]+)", + lambda match: match.group(1) + rewrite_url(match.group(2), base_url, session_id), + content, + ) + + manager = BrowserProxyManager()