gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Problem with updating widgets

Open JelleTempelman opened this issue 1 year ago • 14 comments

Description

The Gutenberg editor on the widgets page is not working properly. When adding a linebreak inside an existing paragraph (in the middle, not at the end) the update button will get stuck loading.

Step-by-step reproduction instructions

  1. Go to playground.wordpress.net, enable network access, activate a theme that supports widgets (e.g. Twenty Twenty)
  2. Go to the widgets editor by clicking on appearance -> widgets
  3. Add a long paragraph with multiple sentences of text
  4. Click save
  5. Edit the widget by adding a linebreak somewhere inside the paragraph you wrote in step 3
  6. Click save

Screenshots, screen recording, code snippet

https://github.com/user-attachments/assets/c1433a72-1caf-41e1-a9c9-36fe92f8a63a

Environment info

WordPress 6.7.2, no other plugin installed, no Gutenberg plugin installed.

Please confirm that you have searched existing issues in the repo.

  • [x] Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • [x] Yes

Please confirm which theme type you used for testing.

  • [ ] Block
  • [ ] Classic
  • [x] Hybrid (e.g. classic with theme.json)
  • [ ] Not sure

JelleTempelman avatar Mar 25 '25 14:03 JelleTempelman

Hi @JelleTempelman, Thank you for reporting the issue. I was able to reproduce the bug when saving the widget.

It seems to be related to how the Widgets Editor handles content serialization when saving the content of innerblocks.

I’ll investigate further to identify the root cause and possible fixes. I’ll update this thread once I have more insights. 🙌🏻

export const __experimentalBatch =
	( requests ) =>
	async ( { dispatch } ) => {
		const batch = createBatch();
		const api = {
			saveEntityRecord( kind, name, record, options ) {
				return batch.add( ( add ) =>
					dispatch.saveEntityRecord( kind, name, record, {
						...options,
						__unstableFetch: add,
					} )
				);
			},
			saveEditedEntityRecord( kind, name, recordId, options ) {
				return batch.add( ( add ) =>
					dispatch.saveEditedEntityRecord( kind, name, recordId, {
						...options,
						__unstableFetch: add,
					} )
				);
			},
			deleteEntityRecord( kind, name, recordId, query, options ) {
				return batch.add( ( add ) =>
					dispatch.deleteEntityRecord( kind, name, recordId, query, {
						...options,
						__unstableFetch: add,
					} )
				);
			},
		};
		const resultPromises = requests.map( ( request ) => request( api ) );
		console.log( "resultPromises", resultPromises );
		const [ , ...results ] = await Promise.all( [
			batch.run(),
			...resultPromises,
		] );
		console.log( "results", results );
		return results;
	};

Following code is not correctly saving. Promises are not getting fullfilled in this case.

Mayank-Tripathi32 avatar Mar 25 '25 20:03 Mayank-Tripathi32

@JelleTempelman Thank you for reporting the issue.

After further investigation, I found that this issue occurs only when the paragraph is added directly to the widget. If the paragraph is placed inside a Group block or another container, the issue does not persist.

To provide more clarity, I have attached a video demonstrating this behavior:

https://github.com/user-attachments/assets/66cf75b3-16ae-459a-9da0-1546e788e389

cc: @Mamaduka

snehapatil2001 avatar Mar 25 '25 21:03 snehapatil2001

Combined Bug Reproduction and Patch Test Report

Description

✅ This report validates that the indicated patch works as expected.

Patch tested: https://github.com/WordPress/gutenberg/pull/69704

Environment

  • WordPress: 6.8-beta3-60061
  • PHP: 8.2.28
  • Server: Apache/2.4.62 (Debian)
  • Database: mysqli (Server: 11.4.5-MariaDB-ubu2404 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 134.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty 2.8
  • MU Plugins:
    • Gutenberg Test Plugin, Disable Login Autofocus
    • Gutenberg Test Plugin, Disable Remote Patterns
    • Gutenberg Test Plugin, Enable Templates UI
    • Gutenberg Test Plugin, No-cache Headers
    • Gutenberg Test Plugin, Normalize Theme
    • server-timing.php
  • Plugins:
    • Gutenberg 20.5.0
    • Test Reports 1.2.0

Minimal Bug Reproduction

  1. Go to the Appearance > Widgets Area with a Theme with Widgets (like Twenty Twenty).
  2. Insert a paragraph inside a Group in the widgets area and add "A" in text ⇾ Save post
  3. Then add a new paragraph outside the group and some text "AB" ⇾ Save post
  4. Now split the "AB" with a newline between A and B (enter) adding another paragraph with just the B ⇾ Save post It will end something like this: bug
  5. 🐞Bug Reproduced ⇾ Saving gets stuck

Expected Results

  • Saving is never stuck

Actual Results with Patch

  1. ✅ Issue is resolved with the patch.

Additional Notes

This bug is moderately challenging to reproduce, I could not reproduce it with @JelleTempelman video, but with the @snehapatil2001 I could for being able to test the patch more consistently. From there, I've been trying to create a minimal report without many steps.

SirLouen avatar Mar 26 '25 00:03 SirLouen

It would be nice to retest the same issue with other blocks that support splitting - Button, List Item, Heading.

Mamaduka avatar Mar 26 '25 05:03 Mamaduka

Thanks for the suggestion, @Mamaduka. I have tested the issue with Button, List Item, and Heading blocks, and the behaviour remains consistent.

snehapatil2001 avatar Mar 26 '25 05:03 snehapatil2001

Thank you, @snehapatil2001!

What do you mean by consistent? Does the same bug affect those blocks, or is it only limited to the Paragraph block?

Mamaduka avatar Mar 26 '25 05:03 Mamaduka

@Mamaduka Apologies for the confusion! To clarify:

  • Paragraph block was the only one exhibiting the hanging-save bug.
  • Other blocks (Heading/List Item/Button) were tested to confirm they don’t share the issue—all saved correctly when split.

snehapatil2001 avatar Mar 26 '25 06:03 snehapatil2001

It would be nice to retest the same issue with other blocks that support splitting - Button, List Item, Heading.

@Mamaduka make sense on the paper, but it took me longer than usual to minimally reproduce this bug. But I've noticed a big difference between paragraph and other "splittable" elements: When you split them, the splitted element defaults to paragraph (i.e. heading) OR splits into the same element (i.e list).

Anyway, today I cannot reproduce it the same I could yesterday.

According to @snehapatil2001 has detected that the problem is caused a block ID duplication on split? What I can't understand is why this duplication is caused after having to do multiple stunts on the editor. It's not like, you split a paragraph = you get the error.

My question is: what is exactly triggering it?

SirLouen avatar Mar 26 '25 10:03 SirLouen

My question is: what is exactly triggering it?

@SirLouen, that what we're trying to figure out and resolve.

Mamaduka avatar Mar 26 '25 10:03 Mamaduka

Hey @Mamaduka @SirLouen,

Previous Findings

After a thorough investigation, I was able to find out why paragraph splits fail in widget areas, why paragraph blocks only do not get unique IDs and why other block splits work correctly while paragraphs doesn't, and I have made changes to PR accordingly.


Paragraphs don't trigger special cases:

  • No transformation ( like heading )
  • No structural child creation ( like button or list )

So in paragraph case, duplication of the block with all attributes ( including __internalWidgetId, due to which the saving fails as widget area cannot have two blocks with same id ).


Root Cause:


The split logic in __unstableSplitSelection copied all attributes when:

  1. Creating the "tail" block during splits
  2. No block type transformation occurred (e.g., paragraphs splitting into paragraphs)

How Splitting Logic Works : 


1. Paragraph Blocks
When you press Enter in a paragraph:

  • The code detects it's splitting the same block type (blockA.name === blockB.name)
  • Creates a new block (the "tail") with:
 ✓A fresh clientId (normal behavior)
 ✓ All original attributes except __internalWidgetId (set to undefined)
  • Why this works: Prevents duplicate widget IDs while keeping other formatting (like bold text)

2. Heading Blocks
When you press Enter in a heading:

  • The code converts it to a paragraph (through switchToBlockType)
  • Generates a completely new block with:
 ✓ New clientId
 ✓ New attributes (no inherited widget ID)
  • Key difference: The block type change triggers fresh attribute generation

3. List/Button Blocks
When you press Enter in lists or buttons:

  • Creates structural children (like list-item or button blocks)
  • These children are designed to:
 ✓ Inherit only specific attributes
 ✓ Skip widget IDs entirely
  • Built-in safeguard: Child blocks never get __internalWidgetId

snehapatil2001 avatar Mar 26 '25 13:03 snehapatil2001

@snehapatil2001 but why it's affect when you simply split on a Post? Have you found why is this linked to the widgets section?

SirLouen avatar Mar 26 '25 13:03 SirLouen

@SirLouen This only affects widget areas because they use __internalWidgetId to sync with WordPress' legacy widget system, which enforces unique IDs. The post editor doesn’t rely on this ID check, so splits work fine there.

snehapatil2001 avatar Mar 26 '25 13:03 snehapatil2001

I could not reproduce again this bug this morning, because I forgot to refresh ths JS build

But why this is not being triggered on first split?

https://github.com/user-attachments/assets/2c506a7f-f370-4eb6-857d-74dff6460762

SirLouen avatar Mar 26 '25 13:03 SirLouen

I can still replicate this with WordPress 7.1.2 and Gutenberg 24.0.

annezazu avatar Sep 25 '26 00:09 annezazu