|
|
import config from '@automattic/calypso-config'; |
|
|
import page from '@automattic/calypso-router'; |
|
|
import * as LoadingError from 'calypso/layout/error'; |
|
|
import { performanceTrackerStart } from 'calypso/lib/performance-tracking'; |
|
|
import { bumpStat } from 'calypso/state/analytics/actions'; |
|
|
import { setSectionLoading } from 'calypso/state/ui/actions'; |
|
|
import { activateNextLayoutFocus } from 'calypso/state/ui/layout-focus/actions'; |
|
|
import * as controller from './controller/index.web'; |
|
|
import { composeHandlers } from './controller/shared'; |
|
|
import sections from './sections'; |
|
|
import isSectionEnabled from './sections-filter'; |
|
|
import { receiveSections, load } from './sections-helper'; |
|
|
import { pathToRegExp } from './utils'; |
|
|
|
|
|
receiveSections( sections ); |
|
|
|
|
|
function activateSection( section, context ) { |
|
|
controller.setSectionMiddleware( section )( context ); |
|
|
context.store.dispatch( activateNextLayoutFocus() ); |
|
|
} |
|
|
|
|
|
async function loadSection( context, sectionDefinition ) { |
|
|
context.store.dispatch( setSectionLoading( true ) ); |
|
|
|
|
|
|
|
|
const loadReportTimeout = setTimeout( () => { |
|
|
context.store.dispatch( bumpStat( 'calypso_chunk_waiting', sectionDefinition.name ) ); |
|
|
}, 400 ); |
|
|
|
|
|
try { |
|
|
|
|
|
const requiredModule = await load( sectionDefinition.name, sectionDefinition.module ); |
|
|
|
|
|
await requiredModule.default( controller.clientRouter ); |
|
|
} finally { |
|
|
context.store.dispatch( setSectionLoading( false ) ); |
|
|
|
|
|
|
|
|
clearTimeout( loadReportTimeout ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const _loadedSections = {}; |
|
|
|
|
|
function loadSectionHandler( sectionDefinition ) { |
|
|
return async ( context, next ) => { |
|
|
try { |
|
|
const loadedSection = _loadedSections[ sectionDefinition.module ]; |
|
|
if ( loadedSection ) { |
|
|
|
|
|
if ( loadedSection !== true ) { |
|
|
await loadedSection; |
|
|
} |
|
|
} else { |
|
|
|
|
|
const loadingSection = loadSection( context, sectionDefinition ); |
|
|
_loadedSections[ sectionDefinition.module ] = loadingSection; |
|
|
|
|
|
|
|
|
await loadingSection; |
|
|
_loadedSections[ sectionDefinition.module ] = true; |
|
|
} |
|
|
|
|
|
|
|
|
activateSection( sectionDefinition, context ); |
|
|
next(); |
|
|
} catch ( error ) { |
|
|
|
|
|
delete _loadedSections[ sectionDefinition.module ]; |
|
|
|
|
|
console.error( error ); |
|
|
if ( ! LoadingError.isRetry() && process.env.NODE_ENV !== 'development' ) { |
|
|
LoadingError.retry( sectionDefinition.name ); |
|
|
} else { |
|
|
LoadingError.show( context, sectionDefinition.name ); |
|
|
} |
|
|
} |
|
|
}; |
|
|
} |
|
|
|
|
|
function createPageDefinition( path, sectionDefinition ) { |
|
|
|
|
|
const { envId } = sectionDefinition; |
|
|
if ( envId && ! envId.includes( config( 'env_id' ) ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const pathRegex = pathToRegExp( path ); |
|
|
let handler = loadSectionHandler( sectionDefinition ); |
|
|
|
|
|
|
|
|
if ( sectionDefinition.trackLoadPerformance ) { |
|
|
handler = composeHandlers( performanceTrackerStart( sectionDefinition.name ), handler ); |
|
|
} |
|
|
|
|
|
|
|
|
if ( ! sectionDefinition.enableLoggedOut ) { |
|
|
handler = composeHandlers( controller.redirectLoggedOut, handler ); |
|
|
} |
|
|
|
|
|
page( pathRegex, handler ); |
|
|
} |
|
|
|
|
|
export const setupRoutes = () => { |
|
|
for ( const section of sections ) { |
|
|
if ( ! isSectionEnabled( section ) ) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
for ( const path of section.paths ) { |
|
|
createPageDefinition( path, section ); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
if ( module.hot ) { |
|
|
module.hot.accept( './sections', () => { |
|
|
const updatedSections = require( './sections' ).default; |
|
|
receiveSections( updatedSections ); |
|
|
setupRoutes(); |
|
|
} ); |
|
|
} |
|
|
|