The editor does not remove the dead script blocks
I'm unable to reproduce this, maybe you can provide more information on the custom component itself, anyway I suspect this is a storage related issue. Are you storing then loading pages from the generated html?
Read full answer below ↓Question
Version:
0.16.34
Are you able to reproduce the bug from the demo?
[ ] Yes [x ] No
As I understand the code import dialog skips all the HTML scripts, so I can not reproduce the issue on the demo
What is the expected behavior?
The editor must remove dead script blocks before appending a new script block at the end of the generated HTML
Describe the bug detailed
I defined a component with script (https://grapesjs.com/docs/modules/Components-js.html) in my project. When I add this component into the editor and save the template, the editor generates the HTML and appends a script block at the end of the body to initiate the related components. The output HTML is something like this:
<head>
<!-- Generated HTML -->
<script>/* The appended script block to initiate my custom components */</script>
</head>
If I load the above output on another editor instance and save the template again it appends another script block and does not remove the old ones and the output will be something like this:
<head>
<!-- Generated HTML -->
<script>/* The appended script block to initiate my custom block */</script>
<script>/* A new script block to initiate my custom block */</script>
</head>
And if I repeat this behavior, It appends a new script block to my template, and it leads to having a lot of dead codes on my final template.
<head>
<!-- Generated HTML -->
<script>/* Dead Script 1 */</script>
<script>/* Dead Script 2 */</script>
<script>/* .... */</script>
<script>/* A new script block to initiate my custom block */</script>
</head>
Answers (4)
I'm unable to reproduce this, maybe you can provide more information on the custom component itself, anyway I suspect this is a storage related issue. Are you storing then loading pages from the generated html?
Typically inline scripts go in the body at the bottom. Try putting it there maybe?
I do not append anything by myself. The editor appends the scripts related to my components in a new script tag at the end of the generated HTML. Please take a look at this file: https://github.com/artf/grapesjs/blob/dev/src/editor/model/Editor.js#L513 I think maybe there is something wrong here. The editor appends a new script block without removing the old one.
Thanks for reporting this, @am1rb.
The issue with The editor does not remove the dead script blocks appears to be a race condition or state management timing problem. This typically happens when component lifecycle events and DOM modifications overlap, creating an inconsistent state.
What to try:
- Add a setTimeout wrapper to ensure the DOM has settled:
setTimeout(() => {
// your operation here
}, 0);
-
Check initialization order — make sure components are fully loaded before you interact with them
-
Use the editor's event system — listen to completion events:
editor.on('component:mount', (component) => {
// safe to interact with component here
});
Recommended next steps:
- Test with the latest GrapesJS version if you haven't
- Provide a minimal reproducible example (CodeSandbox) — this helps the team identify the root cause faster
- Include GrapesJS version, browser, and console errors in your report
Related Questions and Answers
Continue research with similar issue discussions.
Issue #5503
replaceWith method doesn't have string type
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Reproducible demo link none Describ...
Issue #4384
index.d.ts says that components must be Backbone.Collection<Component> but the docs say Component[] is also accepted
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? AnyReproducible demo link https://grapesjs.c...
Issue #3491
iframe does not save src / does not recognize iframes after load
Version: 0.17.4 Are you able to reproduce the bug from the demo?[ ] Yes[X] No What is the expected behavior?After inserting an iFrame that...
Issue #3289
(0.16.41) Components have lost their styles
Version: 0.16.41 Are you able to reproduce the bug from the demo? [X] Yes [ ] No What is the expected behavior? 1) Copy selected parent com...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Tutorial
GrapesJS vs Webflow vs Builder.io vs Puck: Which Visual Builder to Choose in 2026
A practitioner's 2026 comparison of GrapesJS, Webflow, Builder.io, and Puck — pricing, AI features, lock-in, and a one-line rule for picking the right one
Tutorial
Find the Right GrapesJS Plugin in Seconds: Smarter Discovery Is Live
We're shipping a set of discovery upgrades. New label filters, a proper compatibility switch for GrapesJS vs Studio, one-click and a smarter sort bar.
Tutorial
Why use grapesjs shadcn for your template builder
Skip months of editor plumbing and start with a fully working visual builder — polished UI included.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.