) => {
+ if (iframeRef.current?.contentWindow) {
+ const message = {
+ jsonrpc: '2.0',
+ method,
+ params: params || {},
+ id: Date.now()
+ };
+ const targetOrigin = `https://${projectId}.${IFRAME_DOMAIN}`;
+ iframeRef.current.contentWindow.postMessage(message, targetOrigin);
+ }
+ }, [projectId]);
+
+ const navigateIframe = useCallback((url: string) => {
+ // Convert path to full URL
+ const fullUrl = `https://${projectId}.${IFRAME_DOMAIN}${url}`;
+ sendNavigationCommand('navigate', { url: fullUrl });
+ }, [projectId, sendNavigationCommand]);
+
+ const refreshIframe = useCallback(() => {
+ sendNavigationCommand('refresh');
+ }, [sendNavigationCommand]);
+
+ const goBackIframe = useCallback(() => {
+ sendNavigationCommand('goBack');
+ }, [sendNavigationCommand]);
+
+ const goForwardIframe = useCallback(() => {
+ sendNavigationCommand('goForward');
+ }, [sendNavigationCommand]);
+
const handleFetch = useCallback(async (request: JSONRPCRequest) => {
const { params, id } = request;
const { request: fetchRequest } = params;
@@ -348,12 +364,14 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
handleFetch(message);
} else if (message.jsonrpc === '2.0' && message.method === 'console') {
handleConsoleMessage(message);
+ } else if (message.jsonrpc === '2.0' && message.method === 'updateNavigationState') {
+ handleUpdateNavigationState(message);
}
};
window.addEventListener('message', handleMessage);
return () => window.removeEventListener('message', handleMessage);
- }, [handleFetch, handleConsoleMessage, projectId]);
+ }, [handleFetch, handleConsoleMessage, handleUpdateNavigationState, projectId]);
useEffect(() => {
if (selectedFile) {
@@ -372,13 +390,13 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
console.log('Build completed for project, refreshing preview');
// Check for built project and refresh iframe
checkForBuiltProject();
- refreshIframe();
+ refreshIframeLegacy();
}
};
window.addEventListener('buildComplete', handleBuildComplete as EventListener);
return () => window.removeEventListener('buildComplete', handleBuildComplete as EventListener);
- }, [projectId, checkForBuiltProject, refreshIframe]);
+ }, [projectId, checkForBuiltProject, refreshIframeLegacy]);
const handleFileSelect = (filePath: string) => {
setSelectedFile(filePath);
@@ -503,10 +521,10 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
0}
canGoForward={historyIndex < navigationHistory.length - 1}
extraContent={}
@@ -517,7 +535,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
src={`https://${projectId}.${IFRAME_DOMAIN}${currentPath}`}
className="w-full flex-1 border-0"
title="Project Preview"
- sandbox="allow-scripts allow-same-origin"
+ sandbox="allow-scripts allow-same-origin allow-popups allow-forms allow-top-navigation"
/>
) : (