Skip to content

Commit 9df0e19

Browse files
taipeicoderclaude
andcommitted
Navigation Link: Fix "[object Object]" in link preview for untitled entities
For a page with an empty title the entity record's title is `{ raw: '', rendered: '' }`, so `entityRecord?.title?.rendered` is an empty string and the link preview's title computation fell through to `entityRecord?.title` -- the title object itself -- which renders as "[object Object]". Only fall back to `entityRecord.title` when it is a plain string (some entity types expose it that way) so the object is never surfaced. Before WordPress#77170 this rendered an object as a React child and crashed the block ("This block has encountered an error and cannot be previewed"). WordPress#77170 wrapped the preview title in stripHTML, which coerced the object to the harmless "[object Object]" string; this addresses the underlying cause. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 23f80e8 commit 9df0e19

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/block-library/src/navigation-link/shared/test/use-link-preview.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,22 @@ describe( 'useLinkPreview', () => {
500500
expect( result.current.title ).toBe( 'Test Category' );
501501
} );
502502

503+
it( 'should not surface the title object for an empty title', () => {
504+
const { result } = renderHook( () =>
505+
useLinkPreview( {
506+
url: 'https://example.com/page',
507+
// Untitled page: an empty `title.rendered` object.
508+
entityRecord: { title: { raw: '', rendered: '' } },
509+
type: 'page',
510+
hasBinding: false,
511+
isEntityAvailable: true,
512+
} )
513+
);
514+
515+
// Falls back to a string, not the title object (or "[object Object]").
516+
expect( result.current.title ).toBe( 'https://example.com/page' );
517+
} );
518+
503519
it( 'should use entityRecord.name for taxonomy terms', () => {
504520
const { result } = renderHook( () =>
505521
useLinkPreview( {

packages/block-library/src/navigation-link/shared/use-link-preview.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ export function useLinkPreview( {
225225

226226
const title =
227227
entityRecord?.title?.rendered ||
228-
entityRecord?.title ||
228+
// Only use a string `title`, so an empty `title.rendered` doesn't
229+
// surface the { raw, rendered } object ("[object Object]").
230+
( typeof entityRecord?.title === 'string' && entityRecord.title ) ||
229231
entityRecord?.name;
230232

231233
// Fetch rich URL data if we don't have a title. Internal links should have passed a title.

0 commit comments

Comments
 (0)