-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Site editor - try redirecting to homepage before the react render #37248
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
974613e
Replace showHomepage with a selector and dedicate redirect component
talldan b895427
Remove show homepage
talldan b3a595f
Remove component based redirect, instead use an async function prior …
talldan 0179fcc
Use a default export
talldan 7cdd4be
Pass siteUrl in as a param
talldan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this code is replicating
showHomepage
, but I wasn't sure how to test this as there's no customizer where this setting can be changed. Is it still possible to configure a homepage in this way?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can change some of these via Settings -> Reading -> Your homepage displays.