Skip to content

Commit

Permalink
fix: webcontainer error
Browse files Browse the repository at this point in the history
  • Loading branch information
coderz-w committed Jul 24, 2024
1 parent aa7d187 commit 6521e66
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 70 deletions.
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const nextConfig = {
eslint: {
ignoreDuringBuilds: true,
},
reactStrictMode:false,

async headers() {
return [
Expand Down
5 changes: 1 addition & 4 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { Inter } from 'next/font/google';

import { WebContainerProvider } from '@/context/webContainer';
import './globals.css';

const inter = Inter({ subsets: ['latin'] });
Expand All @@ -14,9 +13,7 @@ export default function RootLayout({
}>) {
return (
<html lang="en" suppressHydrationWarning>
<body className={inter.className}>
<WebContainerProvider>{children}</WebContainerProvider>
</body>
<body className={inter.className}>{children}</body>
</html>
);
}
2 changes: 2 additions & 0 deletions src/components/edit/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GoRepoForked } from 'react-icons/go';
import { Avatar } from '@/components/common/Avatar';
import { Button } from '@/components/ui/button';
import AvatarPopover from '@/components/avatarPopover';
import WebContainerProvider from '@/components/webContainerProvider';

interface ProjectData {
[key: string]: any;
Expand Down Expand Up @@ -55,6 +56,7 @@ export const Header: React.FC<HeaderProps> = ({ project, userInfo }) => {
<Avatar src={userInfo.imgurl} className=" flex h-[3.5vh] w-[3.5vh]" />
</AvatarPopover>
</div>
<WebContainerProvider></WebContainerProvider>
</header>
);
};
16 changes: 16 additions & 0 deletions src/components/webContainerProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { Fragment, useEffect, memo } from 'react';

import { useWebContainerStore } from '@/store/webContainerStore';

interface WebContainerProviderProps {}

const WebContainerProvider: React.FC<WebContainerProviderProps> = () => {
const { initWebContainer } = useWebContainerStore();
useEffect(() => {
initWebContainer();
}, [initWebContainer]); // 注意这里需要将 initWebContainer 添加到依赖数组中

return <Fragment></Fragment>;
};

export default memo(WebContainerProvider);
66 changes: 0 additions & 66 deletions src/context/webContainer.tsx

This file was deleted.

52 changes: 52 additions & 0 deletions src/store/webContainerStore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { create } from 'zustand';
import { WebContainer } from '@webcontainer/api';

import { WebContainerFileSystemTreeSavePoint, saveFileSystemTree } from '@/utils';

interface WebContainerState {
webContainerInstance: WebContainer | null;
isInitialized: boolean;
}

interface WebContainerActions {
initWebContainer: () => Promise<void>;
saveFileSystemOnUnload: () => void;
}

type WebContainerStore = WebContainerState & WebContainerActions;

export const useWebContainerStore = create<WebContainerStore>((set, get) => ({
webContainerInstance: null,
isInitialized: false,

async initWebContainer() {
const { webContainerInstance, isInitialized } = get();

if (!isInitialized && !webContainerInstance) {
const newWebContainerInstance = await WebContainer.boot();

const treeData = localStorage.getItem(WebContainerFileSystemTreeSavePoint);

if (treeData) {
const fileSystemTree = JSON.parse(treeData);
await newWebContainerInstance.mount(fileSystemTree);
}

set({ webContainerInstance: newWebContainerInstance, isInitialized: true });

// Add the beforeunload listener here
window.addEventListener('beforeunload', get().saveFileSystemOnUnload);
}
},

saveFileSystemOnUnload() {
const { webContainerInstance } = get();

if (webContainerInstance) {
saveFileSystemTree(webContainerInstance);
}

// Remove the listener when the function is called
window.removeEventListener('beforeunload', this.saveFileSystemOnUnload);
},
}));

0 comments on commit 6521e66

Please sign in to comment.