-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Improve the initial loading of the site editor #36044
Changes from all commits
8ce4700
f72e368
701542b
a782d71
679d966
2e2ceda
2e6eddc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ export const defaultEntities = [ | |
label: __( 'Base' ), | ||
name: '__unstableBase', | ||
kind: 'root', | ||
baseURL: '', | ||
baseURL: '/', | ||
}, | ||
{ | ||
label: __( 'Site' ), | ||
|
@@ -136,6 +136,14 @@ export const defaultEntities = [ | |
plural: 'globalStylesVariations', // should be different than name | ||
getTitle: ( record ) => record?.title?.rendered || record?.title, | ||
}, | ||
{ | ||
label: __( 'Themes' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds the "themes" entity allowing us to rely on the redux cache and avoid multiple |
||
name: 'theme', | ||
kind: 'root', | ||
baseURL: '/wp/v2/themes', | ||
baseURLParams: { context: 'edit' }, | ||
key: 'stylesheet', | ||
}, | ||
]; | ||
|
||
export const kinds = [ | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -14,7 +14,7 @@ import apiFetch from '@wordpress/api-fetch'; | |||
*/ | ||||
import { STORE_NAME } from './name'; | ||||
import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities'; | ||||
import { ifNotResolved, getNormalizedCommaSeparable } from './utils'; | ||||
import { forwardResolver, getNormalizedCommaSeparable } from './utils'; | ||||
|
||||
/** | ||||
* Requests authors from the REST API. | ||||
|
@@ -115,18 +115,12 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( { | |||
/** | ||||
* Requests an entity's record from the REST API. | ||||
*/ | ||||
export const getRawEntityRecord = ifNotResolved( | ||||
getEntityRecord, | ||||
'getEntityRecord' | ||||
); | ||||
export const getRawEntityRecord = forwardResolver( 'getEntityRecord' ); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference between the two approaches is that there are actually "two caches" for resolvers: the one that is in the state which ifNotResolved relied on but that one ignores the temporary cache used to track resolvers that are set to be triggered after a
ifNotResolved there's a change of running the same resolver twice if we call the same selector twice no the same tick, one with getEntityRecord and one time with getRawEntityRecord . (This was actually happening in the site editor)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great idea. The |
||||
|
||||
/** | ||||
* Requests an entity's record from the REST API. | ||||
*/ | ||||
export const getEditedEntityRecord = ifNotResolved( | ||||
getRawEntityRecord, | ||||
'getRawEntityRecord' | ||||
); | ||||
export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' ); | ||||
|
||||
/** | ||||
* Requests the entity's records from the REST API. | ||||
|
@@ -224,22 +218,20 @@ getEntityRecords.shouldInvalidate = ( action, kind, name ) => { | |||
/** | ||||
* Requests the current theme. | ||||
*/ | ||||
export const getCurrentTheme = () => async ( { dispatch } ) => { | ||||
const activeThemes = await apiFetch( { | ||||
path: '/wp/v2/themes?status=active', | ||||
} ); | ||||
export const getCurrentTheme = () => async ( { dispatch, resolveSelect } ) => { | ||||
const activeThemes = await resolveSelect.getEntityRecords( | ||||
'root', | ||||
'theme', | ||||
{ status: 'active' } | ||||
); | ||||
|
||||
dispatch.receiveCurrentTheme( activeThemes[ 0 ] ); | ||||
}; | ||||
|
||||
/** | ||||
* Requests theme supports data from the index. | ||||
*/ | ||||
export const getThemeSupports = () => async ( { dispatch } ) => { | ||||
const activeThemes = await apiFetch( { | ||||
path: '/wp/v2/themes?status=active', | ||||
} ); | ||||
dispatch.receiveThemeSupports( activeThemes[ 0 ].theme_supports ); | ||||
}; | ||||
export const getThemeSupports = forwardResolver( 'getCurrentTheme' ); | ||||
|
||||
/** | ||||
* Requests a preview from the from the Embed API. | ||||
|
@@ -421,10 +413,13 @@ __experimentalGetTemplateForLink.shouldInvalidate = ( action ) => { | |||
|
||||
export const __experimentalGetCurrentGlobalStylesId = () => async ( { | ||||
dispatch, | ||||
resolveSelect, | ||||
} ) => { | ||||
const activeThemes = await apiFetch( { | ||||
path: '/wp/v2/themes?status=active', | ||||
} ); | ||||
const activeThemes = await resolveSelect.getEntityRecords( | ||||
'root', | ||||
'theme', | ||||
{ status: 'active' } | ||||
); | ||||
const globalStylesURL = get( activeThemes, [ | ||||
0, | ||||
'_links', | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Higher-order function which forward the resolution to another resolver with the same arguments. | ||
* | ||
* @param {string} resolverName forwarded resolver. | ||
* | ||
* @return {Function} Enhanced resolver. | ||
*/ | ||
const forwardResolver = ( resolverName ) => ( ...args ) => async ( { | ||
resolveSelect, | ||
} ) => { | ||
await resolveSelect[ resolverName ]( ...args ); | ||
}; | ||
|
||
export default forwardResolver; |
This file was deleted.
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.
the
/
is preloaded.