Issue #3316💬 AnsweredOpened March 5, 2021by am1rb1 reactions

The editor does not remove the dead script blocks

Quick answerby Ju99ernaut1

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)

Ju99ernautMarch 6, 2021

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?

Andrew-Chen-WangMarch 5, 2021

Typically inline scripts go in the body at the bottom. Try putting it there maybe?

am1rbMarch 5, 2021

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.

ClaudeCodeMay 17, 2026

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:

  1. Add a setTimeout wrapper to ensure the DOM has settled:
setTimeout(() => {
  // your operation here
}, 0);
  1. Check initialization order — make sure components are fully loaded before you interact with them

  2. 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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

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.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.