From 36eb11bf0fa5878e482bb8e0ec24c05b0949a2b0 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 23 Feb 2024 11:08:20 +0000 Subject: [PATCH] Revert removal of Nav fallback auto embed (#59220) * Revert the changes that removed the feature * Fix nits Co-authored-by: Mamaduka Co-authored-by: MaggieCabrera Co-authored-by: kevin940726 --- packages/core-data/src/resolvers.js | 34 +++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index f59520f3c436bd..003d01e49641d9 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -661,12 +661,42 @@ export const getUserPatternCategories = export const getNavigationFallbackId = () => - async ( { dispatch } ) => { + async ( { dispatch, select } ) => { const fallback = await apiFetch( { - path: addQueryArgs( '/wp-block-editor/v1/navigation-fallback' ), + path: addQueryArgs( '/wp-block-editor/v1/navigation-fallback', { + _embed: true, + } ), } ); + const record = fallback?._embedded?.self; + dispatch.receiveNavigationFallbackId( fallback?.id ); + + if ( record ) { + // If the fallback is already in the store, don't invalidate navigation queries. + // Otherwise, invalidate the cache for the scenario where there were no Navigation + // posts in the state and the fallback created one. + const existingFallbackEntityRecord = select.getEntityRecord( + 'postType', + 'wp_navigation', + fallback.id + ); + const invalidateNavigationQueries = ! existingFallbackEntityRecord; + dispatch.receiveEntityRecords( + 'postType', + 'wp_navigation', + record, + undefined, + invalidateNavigationQueries + ); + + // Resolve to avoid further network requests. + dispatch.finishResolution( 'getEntityRecord', [ + 'postType', + 'wp_navigation', + fallback.id, + ] ); + } }; export const getDefaultTemplateId =