Make iframe previewDomain configurable

This commit is contained in:
Alex Gleason committed 2025-10-08 15:01:40 -05:00
1 parent b9d65e39b1
commit 45b0ae836a
9 files changed
+61 -22

No files matched your search

+1
View File
@@ -45,6 +45,7 @@ const defaultConfig: AppConfig = {
deployServer: "shakespeare.wtf",
esmUrl: "https://esm.shakespeare.diy",
corsProxy: "https://proxy.shakespeare.diy/?url={href}",
previewDomain: "local-shakespeare.dev",
};
const presetRelays = [
+1
View File
@@ -21,6 +21,7 @@ const AppConfigSchema = z.object({
deployServer: z.string().min(1),
esmUrl: z.string().url(),
corsProxy: z.string().url(),
previewDomain: z.string().min(1),
language: z.string(),
}) satisfies z.ZodType<AppConfig>;
+16 -16
View File
@@ -30,9 +30,7 @@ import { FileEditor } from './FileEditor';
import { DeployDialog } from '@/components/DeployDialog';
import { Terminal as TerminalComponent } from '@/components/Terminal';
import { useSearchParams } from 'react-router-dom';
// Get iframe domain from environment variable
const IFRAME_DOMAIN = import.meta.env.VITE_IFRAME_DOMAIN || 'local-shakespeare.dev';
import { useAppContext } from '@/hooks/useAppContext';
interface PreviewPaneProps {
projectId: string;
@@ -75,6 +73,8 @@ interface JSONRPCResponse {
export function PreviewPane({ projectId, activeTab, onToggleView, projectName, onFirstInteraction, isPreviewable = true }: PreviewPaneProps) {
const { t } = useTranslation();
const { config } = useAppContext();
const { previewDomain } = config;
const [selectedFile, setSelectedFile] = useState<string | null>(null);
const [fileContent, setFileContent] = useState<string>('');
const [isLoading, setIsLoading] = useState(false);
@@ -184,26 +184,26 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
// Use a small timeout to ensure the src is cleared before setting it back
setTimeout(() => {
if (iframeRef.current) {
iframeRef.current.src = `https://${projectId}.${IFRAME_DOMAIN}/`;
iframeRef.current.src = `https://${projectId}.${previewDomain}/`;
}
}, 10);
}
}, [projectId]);
}, [projectId, previewDomain]);
const sendResponse = useCallback((message: JSONRPCResponse) => {
if (iframeRef.current?.contentWindow) {
const targetOrigin = `https://${projectId}.${IFRAME_DOMAIN}`;
const targetOrigin = `https://${projectId}.${previewDomain}`;
iframeRef.current.contentWindow.postMessage(message, targetOrigin);
}
}, [projectId]);
}, [projectId, previewDomain]);
const sendError = useCallback((message: JSONRPCResponse) => {
if (iframeRef.current?.contentWindow) {
const targetOrigin = `https://${projectId}.${IFRAME_DOMAIN}`;
const targetOrigin = `https://${projectId}.${previewDomain}`;
iframeRef.current.contentWindow.postMessage(message, targetOrigin);
}
}, [projectId]);
}, [projectId, previewDomain]);
const handleConsoleMessage = useCallback((message: {
jsonrpc: '2.0';
@@ -266,10 +266,10 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
params: params || {},
id: Date.now()
};
const targetOrigin = `https://${projectId}.${IFRAME_DOMAIN}`;
const targetOrigin = `https://${projectId}.${previewDomain}`;
iframeRef.current.contentWindow.postMessage(message, targetOrigin);
}
}, [projectId]);
}, [projectId, previewDomain]);
const navigateIframe = useCallback((url: string) => {
// Send semantic path directly (e.g., "/about", "/contact")
@@ -316,7 +316,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
try {
// Parse the URL and validate origin
const url = new URL(fetchRequest.url);
const expectedOrigin = `https://${projectId}.${IFRAME_DOMAIN}`;
const expectedOrigin = `https://${projectId}.${previewDomain}`;
if (url.origin !== expectedOrigin) {
console.log(`Invalid origin: ${url.origin}, expected: ${expectedOrigin}`);
@@ -423,13 +423,13 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
id
});
}
}, [projectId, projectsManager, sendResponse, sendError]);
}, [projectId, projectsManager, sendResponse, sendError, previewDomain]);
// Setup messaging protocol for iframe communication
useEffect(() => {
const handleMessage = (event: MessageEvent) => {
// Verify origin for security
const expectedOrigin = `https://${projectId}.${IFRAME_DOMAIN}`;
const expectedOrigin = `https://${projectId}.${previewDomain}`;
if (event.origin !== expectedOrigin) {
console.log(`Ignoring message from unexpected origin: ${event.origin}, expected: ${expectedOrigin}`);
return;
@@ -448,7 +448,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
window.addEventListener('message', handleMessage);
return () => window.removeEventListener('message', handleMessage);
}, [handleFetch, handleConsoleMessage, handleUpdateNavigationState, projectId]);
}, [handleFetch, handleConsoleMessage, handleUpdateNavigationState, projectId, previewDomain]);
useEffect(() => {
if (selectedFile) {
@@ -699,7 +699,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
{hasBuiltProject ? (
<iframe
ref={iframeRef}
src={`https://${projectId}.${IFRAME_DOMAIN}/`}
src={`https://${projectId}.${previewDomain}/`}
className="w-full h-full border-0"
title="Project Preview"
sandbox="allow-scripts allow-same-origin"
+2
View File
@@ -13,6 +13,8 @@ export interface AppConfig {
esmUrl: string;
/** HTTP proxy used to bypass CORS for AI and Git operations */
corsProxy: string;
/** Preview domain for iframe sandboxing (e.g., "local-shakespeare.dev") */
previewDomain: string;
/** Selected language */
language?: string;
}
+2 -2
View File
@@ -37,6 +37,7 @@ function createWrapper(corsProxy: string) {
deployServer: 'test.com',
esmUrl: 'https://esm.test',
corsProxy,
previewDomain: 'local-test.dev',
};
return createElement(
@@ -44,8 +45,7 @@ function createWrapper(corsProxy: string) {
null,
createElement(
AppProvider,
{ storageKey: 'test-app-config', defaultConfig },
children
{ storageKey: 'test-app-config', defaultConfig, children }
)
);
};
+2 -2
View File
@@ -18,6 +18,7 @@ function Wrapper({ children }: { children: React.ReactNode }) {
deployServer: 'test.com',
esmUrl: 'https://esm.test',
corsProxy: 'https://proxy.shakespeare.diy/?url={href}',
previewDomain: 'local-test.dev',
};
return createElement(
@@ -25,8 +26,7 @@ function Wrapper({ children }: { children: React.ReactNode }) {
null,
createElement(
AppProvider,
{ storageKey: 'test-app-config', defaultConfig },
children
{ storageKey: 'test-app-config', defaultConfig, children }
)
);
}
+4 -2
View File
@@ -32,6 +32,10 @@ const resources = {
systemSettingsDescription: 'Configure system-level settings and advanced options.',
esmUrl: 'Javascript CDN',
esmUrlDescription: 'Javascript CDN URL for package imports.',
previewDomain: 'Preview Domain',
previewDomainDescription: 'Domain used for iframe preview sandboxing.',
corsProxy: 'CORS Proxy',
corsProxyDescription: 'HTTP proxy URL for bypassing CORS restrictions. Use {href} as a placeholder for the target URL.',
// Language Options
english: 'English',
@@ -205,8 +209,6 @@ const resources = {
password: 'Password',
enterPassword: 'Enter your password/token',
credentialsExist: 'Credentials for this origin already exist',
corsProxy: 'CORS Proxy',
corsProxyDescription: 'Proxy used to bypass CORS for AI and Git operations.',
// About Settings
aboutShakespeare: 'About Shakespeare',
+31
View File
@@ -15,6 +15,7 @@ export function SystemSettings() {
const { config, updateConfig } = useAppContext();
const [esmUrlInput, setEsmUrlInput] = useState(config.esmUrl);
const [corsProxyInput, setCorsProxyInput] = useState(config.corsProxy);
const [previewDomainInput, setPreviewDomainInput] = useState(config.previewDomain);
return (
<div className="p-6 space-y-6">
@@ -54,6 +55,36 @@ export function SystemSettings() {
)}
<div className="space-y-3 max-w-xl">
{/* Preview Domain Configuration */}
<Accordion type="single" collapsible className="w-full">
<AccordionItem value="preview-domain" className="border rounded-lg">
<AccordionTrigger className="px-4 py-3 hover:no-underline">
<h4 className="text-sm font-medium">{t('previewDomain')}</h4>
</AccordionTrigger>
<AccordionContent className="px-4 pb-4">
<div className="py-1 space-y-2">
<Input
id="preview-domain"
type="text"
placeholder="local-shakespeare.dev"
value={previewDomainInput}
onChange={(e) => {
const value = e.target.value;
setPreviewDomainInput(value);
updateConfig((current) => ({
...current,
previewDomain: value,
}));
}}
/>
<p className="text-xs text-muted-foreground">
{t('previewDomainDescription')}
</p>
</div>
</AccordionContent>
</AccordionItem>
</Accordion>
{/* CORS Proxy Configuration */}
<Accordion type="single" collapsible className="w-full">
<AccordionItem value="cors-proxy" className="border rounded-lg">
+2
View File
@@ -32,6 +32,8 @@ export function TestApp({ children }: TestAppProps) {
relayUrl: 'wss://relay.nostr.band',
deployServer: 'shakespeare.wtf',
esmUrl: 'https://esm.shakespeare.diy',
corsProxy: 'https://proxy.shakespeare.diy/?url={href}',
previewDomain: 'local-shakespeare.dev',
language: 'en',
};