-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site editor - try redirecting to homepage before the react render (#3…
…7248) * Replace showHomepage with a selector and dedicate redirect component * Remove show homepage * Remove component based redirect, instead use an async function prior to rendering app * Use a default export * Pass siteUrl in as a param
- Loading branch information
Showing
5 changed files
with
83 additions
and
160 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
packages/edit-site/src/components/routes/redirect-to-homepage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import history from '../../utils/history'; | ||
import getIsListPage from '../../utils/get-is-list-page'; | ||
|
||
function getNeedsHomepageRedirect( params ) { | ||
const { postType } = params; | ||
return ( | ||
! getIsListPage( params ) && | ||
! [ 'post', 'page', 'wp_template', 'wp_template_part' ].includes( | ||
postType | ||
) | ||
); | ||
} | ||
|
||
async function getHomepageParams( siteUrl ) { | ||
const siteSettings = await apiFetch( { path: '/wp/v2/settings' } ); | ||
if ( ! siteSettings ) { | ||
return; | ||
} | ||
|
||
const { | ||
show_on_front: showOnFront, | ||
page_on_front: frontpageId, | ||
} = siteSettings; | ||
|
||
// If the user has set a page as the homepage, use those details. | ||
if ( showOnFront === 'page' ) { | ||
return { | ||
postType: 'page', | ||
postId: frontpageId, | ||
}; | ||
} | ||
|
||
// Else get the home template. | ||
// This matches the logic in `__experimentalGetTemplateForLink`. | ||
// (packages/core-data/src/resolvers.js) | ||
const template = await window | ||
.fetch( addQueryArgs( siteUrl, { '_wp-find-template': true } ) ) | ||
.then( ( res ) => res.json() ) | ||
.then( ( { data } ) => data ); | ||
|
||
if ( ! template?.id ) { | ||
return; | ||
} | ||
|
||
return { | ||
postType: 'wp_template', | ||
postId: template.id, | ||
}; | ||
} | ||
|
||
export default async function redirectToHomepage( siteUrl ) { | ||
const searchParams = new URLSearchParams( history.location.search ); | ||
const params = Object.fromEntries( searchParams.entries() ); | ||
|
||
if ( getNeedsHomepageRedirect( params ) ) { | ||
const homepageParams = await getHomepageParams( siteUrl ); | ||
|
||
if ( homepageParams ) { | ||
history.replace( homepageParams ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters