Skip to content

Commit 5baad12

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); an untitled entity now falls back to its URL. 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) <[email protected]>
1 parent 23f80e8 commit 5baad12

2 files changed

Lines changed: 21 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 fall back to the URL (not the title object) for an empty title', () => {
504+
const { result } = renderHook( () =>
505+
useLinkPreview( {
506+
url: 'https://example.com/page',
507+
// An untitled page has a { raw, rendered } title with an empty
508+
// rendered string; it must not surface the object itself.
509+
entityRecord: { title: { raw: '', rendered: '' } },
510+
type: 'page',
511+
hasBinding: false,
512+
isEntityAvailable: true,
513+
} )
514+
);
515+
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ export function useLinkPreview( {
225225

226226
const title =
227227
entityRecord?.title?.rendered ||
228-
entityRecord?.title ||
228+
// Some entity types expose `title` as a plain string rather than a
229+
// { raw, rendered } object. Only fall back to it when it is a string,
230+
// otherwise an empty `title.rendered` surfaces the title object itself
231+
// (which renders as "[object Object]").
232+
( typeof entityRecord?.title === 'string' && entityRecord.title ) ||
229233
entityRecord?.name;
230234

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

0 commit comments

Comments
 (0)