HTML elements ids are incremented on different pages loaded in projectData
hey @bgrand-ch I think one day all grapesjs entities will have a global uid, so in long-term, I'd expect this issue to be fixed 🤞
Read full answer below ↓Question
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Firefox 109
Reproducible demo link
https://grapesjs.com/demo.html
Describe the bug
Hello,
I noticed that when I load multiple pages in the projectData that have the same id used in the HTML, grapes will automatically increment the id on every page even though these are different pages.
To demonstrate this issue, you will find a piece of code below where I init grapesjs with a projectData containing two pages using identical ids on some HTML tags.
On the resulting HTML list, you can see the ids like body and main-title are incremented to body-2 and main-title-2 on the second page.
const editortest = grapesjs.init({
headless: true,
projectData: {
pages: [
{
"frames": [
{
"component": {
"type": "wrapper",
"stylable": [
"background",
"background-color",
"background-image",
"background-repeat",
"background-attachment",
"background-position",
"background-size"
],
"attributes": {
"id": "body"
},
"components": [
{
"tagName": "section",
"components": [
{
"tagName": "h1",
"type": "text",
"attributes": {
"id": "main-title"
},
"components": [
{
"type": "textnode",
"content": "This is a simple title"
}
]
},
{
"type": "text",
"components": [
{
"type": "textnode",
"content": "This is just a Lorem text: Lorem ipsum dolor sit amet"
}
]
}
]
}
]
}
}
],
"id": "ljc0blhC1ZeiCYyY"
},
{
"frames": [
{
"component": {
"type": "wrapper",
"stylable": [
"background",
"background-color",
"background-image",
"background-repeat",
"background-attachment",
"background-position",
"background-size"
],
"attributes": {
"id": "body"
},
"components": [
{
"tagName": "section",
"components": [
{
"tagName": "h1",
"type": "text",
"attributes": {
"id": "main-title"
},
"components": [
{
"type": "textnode",
"content": "This is another title"
}
]
},
{
"type": "text",
"components": [
{
"type": "textnode",
"content": "Some other text"
}
]
}
]
}
]
}
}
],
"id": "1zKkQnGVzy8nKXKE"
}
]
}
})
let pages = editortest.Pages.getAll().map(page => {
const component = page.getMainComponent();
return editortest.getHtml({ component })
});
console.log(pages)
The pages result:
[
"<body id=\"body\"><section><h1 id=\"main-title\">This is a simple title</h1><div>This is just a Lorem text: Lorem ipsum dolor sit amet</div></section></body>",
"<body id=\"body-2\"><section><h1 id=\"main-title-2\">This is another title</h1><div>Some other text</div></section></body>"
]
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (4)
hey @bgrand-ch I think one day all grapesjs entities will have a global uid, so in long-term, I'd expect this issue to be fixed 🤞
At first, I wanted to generate all the pages together to have a single CSS output for all the pages. But now I'm trying another solution for the styles.
So for the id problem, generating each page separately could be a solution. Thanks for the idea.
Grapes to use some data attributes (something like
data-grapes-id) as unique identifiers to leave the id free for some frontend scripts.
I like this idea 💯
Thanks for reporting this, @quentin-bettoum.
The issue with HTML elements ids are incremented on different pages loaded in projectData 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 #5584
"storage:end:load" event is not triggered when all components are mounted into the canvas
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v91 Reproducible demo link https://...
Issue #5378
Uncaught TypeError: Cannot read properties of undefined (reading 'Canvas') while dragging a component in Layer Manager Panel
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v116.0.5845.97 Reproducible demo li...
Issue #4848
Component content is set to empty string after inline editing
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Firefox v91.4.1 Reproducible demo link htt...
Issue #6606
Editor takes a long time to load (sometimes over 1 minute)
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Firefox Reproducible demo link https://gra...
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
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Tutorial
Big Updates: TinyMCE 8 and Placeholder 2.0 for GrapesJS
In May we shipped major updates to two of our most popular GrapesJS plugins — TinyMCE Inline Text Editor and Placeholder.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.