Component script function runs twice after page save
As explained here you should not reimport the output HTML to edit the project, only the JSON.
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
112.0.5615.137 (Official Build) (arm64)
Reproducible demo link
https://grapesjs.com/demo.html
Describe the bug
Component definition:
editor.DomComponents.addType("my-test", {
model: {
defaults: {
name: "Testing",
components: "Hello testing",
script: function () {
alert("hello");
},
},
},
});
When dropped, saved, and reload there are two places that call the same script. One in the saved HTML/JSON and one added by Grapesjs itself.
<script class="">
var items = document.querySelectorAll('#if1fgr');
for (var i = 0, len = items.length; i < len; i++) {
(function(){
alert("hello");
}.bind(items[i]))();
}
</script>
------------------------------------------------------------------------------------------------------------------
<div data-id="if1fgr">
<script>
setTimeout(function() {
var item = document.getElementById('if1fgr');
if (!item) return;
(function(){
alert("hello");
;}.bind(item))({})
}, 1);
</script>
</div>
How to reproduce the bug?
- Add a custom component type with a script function.
- Drop it and save the page.
- Reloading the page runs the script function twice.
What is the expected behavior? The script function should be running only once.
What is the current behavior? The script function runs twice.
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (2)
As explained here you should not reimport the output HTML to edit the project, only the JSON.
Thanks for reporting this, @FaisalShaikhHA.
Great question about Component script function runs twice after page save. The recommended approach with Components is to use the event-driven API.
Start here:
- Check the GrapesJS documentation for your specific module
- Look for the
on()event listener method - Most operations can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #6152
CSS added via custom code persists after custom code component is removed
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? any Reproducible demo link https://grapesj...
Issue #4752
Component with pointer-events: 'all' (except wrapper) or editable component are draggable & droppable to itself.
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome 107.0.5304.121 (Official Build) (arm6...
Issue #5173
editor.Css.setRule API not working as expected, it is not updating the rule existing rule.
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Version 112.0.5615.137 (Official Build) (a...
Issue #5346
Deleting a component without deleting its corresponding style
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? latest version of Chrome Reproducible demo...
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.