|
|
import debugFactory from 'debug'; |
|
|
import { isEmpty } from 'lodash'; |
|
|
import { stringify } from 'qs'; |
|
|
import { setSectionMiddleware } from 'calypso/controller'; |
|
|
import performanceMark from 'calypso/server/lib/performance-mark'; |
|
|
import { serverRender, setShouldServerSideRender, markupCache } from 'calypso/server/render'; |
|
|
import { createQueryClientSSR } from 'calypso/state/query-client-ssr'; |
|
|
import { setRoute } from 'calypso/state/route/actions'; |
|
|
|
|
|
const debug = debugFactory( 'calypso:pages' ); |
|
|
|
|
|
export function serverRouter( expressApp, setUpRoute, section ) { |
|
|
return function ( route, ...middlewares ) { |
|
|
expressApp.get( |
|
|
route, |
|
|
( req, res, next ) => { |
|
|
const markup = markupCache.get( getCacheKey( req ) ); |
|
|
if ( markup ) { |
|
|
req.context.cachedMarkup = markup; |
|
|
} |
|
|
req.context.usedSSRHandler = true; |
|
|
debug( |
|
|
`Using SSR pipeline for path: ${ |
|
|
req.path |
|
|
} with handler ${ route }. Cached layout: ${ !! markup }` |
|
|
); |
|
|
next(); |
|
|
}, |
|
|
setUpRoute, |
|
|
combineMiddlewares( |
|
|
setSectionMiddleware( section ), |
|
|
setRouteMiddleware, |
|
|
setShouldServerSideRender, |
|
|
...middlewares |
|
|
), |
|
|
|
|
|
serverRender, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
( err, req, res, next ) => { |
|
|
performanceMark( req.context, 'serverRouter error handler' ); |
|
|
req.error = err; |
|
|
res.status( err.status || 404 ); |
|
|
if ( err.status >= 500 ) { |
|
|
req.logger.error( err ); |
|
|
} else { |
|
|
req.logger.warn( err ); |
|
|
} |
|
|
serverRender( req, res, next ); |
|
|
performanceMark( req.context, 'post-handling serverRouter error' ); |
|
|
|
|
|
|
|
|
next( err ); |
|
|
} |
|
|
); |
|
|
}; |
|
|
} |
|
|
|
|
|
function setRouteMiddleware( context, next ) { |
|
|
context.store.dispatch( setRoute( context.pathname, context.query ) ); |
|
|
|
|
|
next(); |
|
|
} |
|
|
|
|
|
function combineMiddlewares( ...middlewares ) { |
|
|
return function ( req, res, expressNext ) { |
|
|
req.context = getEnhancedContext( req, res ); |
|
|
applyMiddlewares( |
|
|
req.context, |
|
|
...middlewares, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
( context, next ) => expressNext(), |
|
|
|
|
|
( err, context, next ) => expressNext( err ) |
|
|
); |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
function getEnhancedContext( req, res ) { |
|
|
return Object.assign( {}, req.context, { |
|
|
isServerSide: true, |
|
|
originalUrl: req.originalUrl, |
|
|
path: req.url, |
|
|
queryClient: createQueryClientSSR(), |
|
|
pathname: req.path, |
|
|
params: req.params, |
|
|
query: req.query, |
|
|
redirect: res.redirect.bind( res ), |
|
|
res, |
|
|
} ); |
|
|
} |
|
|
|
|
|
function applyMiddlewares( context, ...middlewares ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const liftedMiddlewares = middlewares.map( ( middleware ) => ( next, err ) => { |
|
|
try { |
|
|
if ( ! err && middleware.length !== 3 ) { |
|
|
|
|
|
return middleware( context, next ); |
|
|
} |
|
|
if ( err && middleware.length === 3 ) { |
|
|
|
|
|
return middleware( err, context, next ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
next( err ); |
|
|
} catch ( error ) { |
|
|
|
|
|
|
|
|
next( error ); |
|
|
} |
|
|
} ); |
|
|
|
|
|
compose( ...liftedMiddlewares )(); |
|
|
} |
|
|
|
|
|
function compose( ...functions ) { |
|
|
return functions.reduceRight( |
|
|
( composed, f ) => ( err ) => f( composed, err ), |
|
|
() => {} |
|
|
); |
|
|
} |
|
|
|
|
|
export function getNormalizedPath( pathname, query ) { |
|
|
|
|
|
|
|
|
|
|
|
if ( pathname.length > 1 && pathname.endsWith( '/' ) ) { |
|
|
pathname = pathname.slice( 0, -1 ); |
|
|
} |
|
|
|
|
|
if ( isEmpty( query ) ) { |
|
|
return pathname; |
|
|
} |
|
|
|
|
|
return pathname + '?' + stringify( query, { sort: ( a, b ) => a.localeCompare( b ) } ); |
|
|
} |
|
|
|
|
|
|
|
|
export function getCacheKey( { path, query, context } ) { |
|
|
return `${ getNormalizedPath( path, query ) }:gdpr=${ context.showGdprBanner }`; |
|
|
} |
|
|
|