diff --git a/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js b/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js index 3498bed4c99a5..2dbd143766e67 100644 --- a/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js +++ b/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js @@ -96,7 +96,32 @@ function useResolveEditedEntityAndContext( { postId, postType } ) { postTypeToResolve === 'page' && homepageId === postIdToResolve ) { - return getDefaultTemplateId( { slug: 'front-page' } ); + // The /lookup endpoint cannot currently handle a lookup + // when a page is set as the front page, so specifically in + // that case, we want to check if there is a front page + // template, and instead of falling back to the home + // template, we want to fall back to the page template. + const templates = getEntityRecords( + 'postType', + TEMPLATE_POST_TYPE, + { + per_page: -1, + } + ); + if ( templates ) { + const id = templates?.find( + ( { slug } ) => slug === 'front-page' + )?.id; + if ( id ) { + return id; + } + + // If no front page template is found, continue with the + // logic below (fetching the page template). + } else { + // Still resolving `templates`. + return undefined; + } } const editedEntity = getEditedEntityRecord( diff --git a/test/e2e/specs/site-editor/template-hierarchy.spec.js b/test/e2e/specs/site-editor/template-hierarchy.spec.js new file mode 100644 index 0000000000000..5fcc77da9942c --- /dev/null +++ b/test/e2e/specs/site-editor/template-hierarchy.spec.js @@ -0,0 +1,35 @@ +/** + * WordPress dependencies + */ +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); + +test.describe( 'Template hierarchy', () => { + test.beforeAll( async ( { requestUtils } ) => { + await requestUtils.activateTheme( 'twentytwentyfour' ); + } ); + + test.afterEach( async ( { requestUtils } ) => { + await requestUtils.activateTheme( 'twentytwentyone' ); + } ); + + test( 'shows correct template with page on front option', async ( { + admin, + page, + editor, + } ) => { + await admin.visitAdminPage( 'options-reading.php' ); + await page.click( 'input[name="show_on_front"][value="page"]' ); + await page.selectOption( 'select[name="page_on_front"]', '2' ); + await page.click( 'input[type="submit"]' ); + await admin.visitSiteEditor(); + + // Title block should contain "Sample Page" + await expect( + editor.canvas.locator( 'role=document[name="Block: Title"]' ) + ).toContainText( 'Sample Page' ); + + await admin.visitAdminPage( 'options-reading.php' ); + await page.click( 'input[name="show_on_front"][value="posts"]' ); + await page.click( 'input[type="submit"]' ); + } ); +} );