Browse Source

fix(sandpack): improve memory usage (#4126)

* fix(sandpack): init mode

* init mode tweaks

* sandpack preview: keep loading state while preview is still working

* prettify it

* fix: renew client listener on remount
main
Danilo Woznica 3 years ago
committed by GitHub
parent
commit
626f957364
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      beta/package.json
  2. 46
      beta/src/components/MDX/Sandpack/Preview.tsx
  3. 4
      beta/src/components/MDX/Sandpack/index.tsx
  4. 19
      beta/yarn.lock

2
beta/package.json

@ -22,7 +22,7 @@
"check-all": "npm-run-all prettier lint:fix tsc"
},
"dependencies": {
"@codesandbox/sandpack-react": "0.13.6-experimental.0",
"@codesandbox/sandpack-react": "0.13.9-experimental.6",
"@docsearch/css": "3.0.0-alpha.41",
"@docsearch/react": "3.0.0-alpha.41",
"@headlessui/react": "^1.3.0",

46
beta/src/components/MDX/Sandpack/Preview.tsx

@ -46,6 +46,7 @@ export function Preview({
errorScreenRegisteredRef,
openInCSBRegisteredRef,
loadingScreenRegisteredRef,
status,
} = sandpack;
if (
@ -67,30 +68,38 @@ export function Preview({
errorScreenRegisteredRef.current = true;
loadingScreenRegisteredRef.current = true;
React.useEffect(() => {
React.useEffect(function createBundler() {
const iframeElement = iframeRef.current!;
registerBundler(iframeElement, clientId.current);
const unsub = listen((message: any) => {
if (message.type === 'resize') {
setComputedAutoHeight(message.height);
} else if (message.type === 'start') {
if (message.firstLoad) {
setIsReady(false);
}
} else if (message.type === 'test') {
// Does it make sense that we're listening to "test" event?
// Not really. Does it cause less flicker than "done"? Yes.
setIsReady(true);
}
}, clientId.current);
return () => {
unsub();
unregisterBundler(clientId.current);
};
}, []);
React.useEffect(
function bundlerListener() {
const unsubscribe = listen((message: any) => {
if (message.type === 'resize') {
setComputedAutoHeight(message.height);
} else if (message.type === 'start') {
if (message.firstLoad) {
setIsReady(false);
}
} else if (message.type === 'done') {
setIsReady(true);
}
}, clientId.current);
return () => {
setIsReady(false);
setComputedAutoHeight(null);
unsubscribe();
};
},
[status === 'idle']
);
const viewportStyle = computeViewportSize('auto', 'portrait');
const overrideStyle = error
? {
@ -175,7 +184,10 @@ export function Preview({
<Error error={error} />
</div>
)}
<LoadingOverlay clientId={clientId.current} />
<LoadingOverlay
clientId={clientId.current}
loading={!isReady && iframeComputedHeight === null}
/>
</div>
</div>
);

4
beta/src/components/MDX/Sandpack/index.tsx

@ -127,7 +127,9 @@ function Sandpack(props: SandpackProps) {
<SandpackProvider
template="react"
customSetup={{...setup, files: files}}
autorun={autorun}>
autorun={autorun}
initMode="user-visible"
initModeObserverOptions={{rootMargin: '1400px 0px'}}>
<CustomPreset
isSingleFile={isSingleFile}
showDevTools={showDevTools}

19
beta/yarn.lock

@ -547,18 +547,18 @@
style-mod "^4.0.0"
w3c-keyname "^2.2.4"
"@codesandbox/sandpack-client@^0.13.6-experimental.0":
version "0.13.6-experimental.0"
resolved "https://registry.yarnpkg.com/@codesandbox/sandpack-client/-/sandpack-client-0.13.6-experimental.0.tgz#6688663f74f3f526f11691fb23cc3d1dea6d1f80"
integrity sha512-xEwGRg3ETgsmcGFYAcWSyYDRRxy5uwxQ2Zu0G82GXySZunLYwc5gQy+NOfgjnn6O+Tp7BW4ao6j3OGTZYqZOUg==
"@codesandbox/sandpack-client@^0.13.9-experimental.0":
version "0.13.9-experimental.0"
resolved "https://registry.yarnpkg.com/@codesandbox/sandpack-client/-/sandpack-client-0.13.9-experimental.0.tgz#10eccbd0a3b35d3f46f9484405580c13a4693127"
integrity sha512-ktd6HWeQyErO91Sm5DxcLAc4BouUyUUNM1mKwWKzRkw6uUyt9IPbJNttnU0FH88gQxIwduEYVi7wEx/qn0AtMQ==
dependencies:
codesandbox-import-utils "^1.2.3"
lodash.isequal "^4.5.0"
"@codesandbox/sandpack-react@0.13.6-experimental.0":
version "0.13.6-experimental.0"
resolved "https://registry.yarnpkg.com/@codesandbox/sandpack-react/-/sandpack-react-0.13.6-experimental.0.tgz#3070cd0e5b741284133b65e32d313b2aca0c5d50"
integrity sha512-QLP6wusM8COpFpmizUh1HGtNmKXDo8KNk5NSYSsCojeQyr9YFC1ye65MM6bMp5kXeF7c6kJFs9MulxpIckEbUQ==
"@codesandbox/sandpack-react@0.13.9-experimental.6":
version "0.13.9-experimental.6"
resolved "https://registry.yarnpkg.com/@codesandbox/sandpack-react/-/sandpack-react-0.13.9-experimental.6.tgz#81ee72ad96b8dadf3626271a0ec9ae0694a421de"
integrity sha512-IOjuSPg28B/YkRGvbYCjDA45SHIBL2oF/1LBDkUisBHz1fbMTROYLLqdzNDP4yWgA9Ya9OgOgJe904i3pcE4YQ==
dependencies:
"@code-hike/classer" "^0.0.0-aa6efee"
"@codemirror/closebrackets" "^0.19.0"
@ -574,10 +574,11 @@
"@codemirror/matchbrackets" "^0.19.3"
"@codemirror/state" "^0.19.6"
"@codemirror/view" "^0.19.32"
"@codesandbox/sandpack-client" "^0.13.6-experimental.0"
"@codesandbox/sandpack-client" "^0.13.9-experimental.0"
"@react-hook/intersection-observer" "^3.1.1"
codesandbox-import-util-types "^2.2.3"
codesandbox-import-utils "^2.2.3"
lodash.isequal "^4.5.0"
react-devtools-inline "4.22.1"
react-is "^17.0.2"

Loading…
Cancel
Save