Skip to content

Commit

Permalink
Iframe: fix blob cleanup (#51004)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored May 26, 2023
1 parent 1a2d5f6 commit 3631e92
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
forwardRef,
useMemo,
useReducer,
renderToString,
useEffect,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -238,15 +237,19 @@ function Iframe( {
// Correct doctype is required to enable rendering in standards
// mode. Also preload the styles to avoid a flash of unstyled
// content.
const src = useMemo( () => {
const html = '<!doctype html>' + renderToString( styleAssets );
const blob = new window.Blob( [ html ], { type: 'text/html' } );
return URL.createObjectURL( blob );
}, [] );
const html =
'<!doctype html>' +
'<style>html{height:auto!important;}body{margin:0}</style>' +
( assets?.styles ?? '' );

const [ src, cleanup ] = useMemo( () => {
const _src = URL.createObjectURL(
new window.Blob( [ html ], { type: 'text/html' } )
);
return [ _src, () => URL.revokeObjectURL( _src ) ];
}, [ html ] );

useEffect( () => () => {
URL.revokeObjectURL( src );
} );
useEffect( () => cleanup, [ cleanup ] );

// We need to counter the margin created by scaling the iframe. If the scale
// is e.g. 0.45, then the top + bottom margin is 0.55 (1 - scale). Just the
Expand Down

1 comment on commit 3631e92

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 3631e92.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5091486091
📝 Reported issues:

Please sign in to comment.