-
Notifications
You must be signed in to change notification settings - Fork 2k
Framework: introduce sections middleware #21349
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
| /** @format */ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import config from 'config'; | ||
| import page from 'page'; | ||
| import { find, filter, isEmpty } from 'lodash'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { activateNextLayoutFocus } from 'state/ui/layout-focus/actions'; | ||
| import * as LoadingError from 'layout/error'; | ||
| import * as controller from './controller/index.web'; | ||
| import { restoreLastSession } from 'lib/restore-last-path'; | ||
| import { hub as preloadHub } from 'sections-preload'; | ||
| import { switchCSS } from 'lib/i18n-utils/switch-locale'; | ||
| import { pathToRegExp } from './utils'; | ||
|
|
||
| import sections from './sections'; | ||
|
|
||
| const _loadedSections = {}; | ||
|
|
||
| function maybeLoadCSS( sectionName ) { | ||
| //eslint-disable-line no-unused-vars | ||
| const section = find( sections, { name: sectionName } ); | ||
|
|
||
| if ( ! ( section && section.css ) ) { | ||
| return; | ||
| } | ||
|
|
||
| const url = | ||
| typeof document !== 'undefined' && document.documentElement.dir === 'rtl' | ||
| ? section.css.urls.rtl | ||
| : section.css.urls.ltr; | ||
|
|
||
| switchCSS( 'section-css-' + section.css.id, url ); | ||
| } | ||
|
|
||
| function preload( sectionName ) { | ||
| maybeLoadCSS( sectionName ); | ||
| const filteredSections = filter( sections, { name: sectionName } ); | ||
| if ( isEmpty( filteredSections ) ) { | ||
| return Promise.reject( `Attempting to load non-existent section: ${ sectionName }` ); | ||
| } | ||
| return Promise.all( filteredSections.map( section => section.load() ) ); | ||
| } | ||
|
|
||
| preloadHub.on( 'preload', preload ); | ||
|
|
||
| function activateSection( sectionDefinition, context, next ) { | ||
| const dispatch = context.store.dispatch; | ||
|
|
||
| controller.setSection( sectionDefinition )( context ); | ||
| dispatch( { type: 'SECTION_SET', isLoading: false } ); | ||
| dispatch( activateNextLayoutFocus() ); | ||
| next(); | ||
| } | ||
|
|
||
| function createPageDefinition( path, sectionDefinition ) { | ||
| const pathRegex = pathToRegExp( path ); | ||
|
|
||
| page( pathRegex, function( context, next ) { | ||
| const envId = sectionDefinition.envId; | ||
| const dispatch = context.store.dispatch; | ||
|
Contributor
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. |
||
|
|
||
| if ( envId && envId.indexOf( config( 'env_id' ) ) === -1 ) { | ||
| return next(); | ||
| } | ||
| if ( _loadedSections[ sectionDefinition.module ] ) { | ||
| return activateSection( sectionDefinition, context, next ); | ||
| } | ||
| if ( config.isEnabled( 'restore-last-location' ) && restoreLastSession( context.path ) ) { | ||
| return; | ||
| } | ||
| dispatch( { type: 'SECTION_SET', isLoading: true } ); | ||
| preload( sectionDefinition.name ) | ||
| .then( requiredModules => { | ||
| if ( ! _loadedSections[ sectionDefinition.module ] ) { | ||
| requiredModules.forEach( mod => mod( controller.clientRouter ) ); // if we do array | ||
| _loadedSections[ sectionDefinition.module ] = true; | ||
| } | ||
| return activateSection( sectionDefinition, context, next ); | ||
| } ) | ||
| .catch( error => { | ||
| console.error( error ); // eslint-disable-line | ||
| if ( ! LoadingError.isRetry() ) { | ||
| LoadingError.retry( sectionDefinition.name ); | ||
| } else { | ||
| dispatch( { type: 'SECTION_SET', isLoading: false } ); | ||
| LoadingError.show( sectionDefinition.name ); | ||
| } | ||
| } ); | ||
| } ); | ||
| } | ||
|
|
||
| export const getSections = () => sections; | ||
|
|
||
| export const setupRoutes = () => { | ||
| sections.forEach( section => | ||
| section.paths.forEach( path => createPageDefinition( path, section ) ) | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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,11 @@ | ||
| /** @format */ | ||
|
|
||
| // Adapts route paths to also include wildcard | ||
| // subroutes under the root level section. | ||
| export function pathToRegExp( path ) { | ||
| // Prevents root level double dash urls from being validated. | ||
| if ( path === '/' ) { | ||
| return path; | ||
| } | ||
| return new RegExp( '^' + path + '(/.*)?$' ); | ||
| } |
Oops, something went wrong.
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.
Nitpick -- Can we destructure here? Or is that part of the language spec not available at this stage?
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.
🤷♂️
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.
If the question is "Does Caylpso's Node version (8.9.3 at the moment) support destructuring assignment?" the answer is "Yes."