Skip to content

Commit

Permalink
fix(iframe): reload the frame on every render
Browse files Browse the repository at this point in the history
Not just overwrite content, because global javascript variables will collide otherwise.

Instead, using object URL we can re-render the iframe everytime and free up memory be revoking object on next render.
  • Loading branch information
acouvreur committed Oct 21, 2024
1 parent acbf476 commit 7bcd232
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions webui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,20 @@
});
function render() {
var output = template(ace.edit("input-editor").getValue(), ace.edit("data-editor").getValue());
var doc = document.getElementById('output').contentWindow.document;
doc.open();
doc.write(output);
doc.close();
var frame = document.getElementById('output')
var oldSrc = frame.src
frame.src = ''; // Clear the current content
setTimeout(() => {
// Create a Blob URL for the code and set it as the iframe's source
const blob = new Blob([output], { type: 'text/html' });
const url = URL.createObjectURL(blob);
frame.src = url; // Load new code into the iframe

if (oldSrc) {
console.log("Clearing old object:" + oldSrc)
URL.revokeObjectURL(oldSrc);
}
}, 0); // Delay to allow the iframe to clear
}
function loadFromLocalStorage() {
var input_editor = ace.edit("input-editor");
Expand Down

0 comments on commit 7bcd232

Please sign in to comment.