server/unique-redirect.js
/**
 * Builds HTTP middleware that serves redirects for content found in dist/.
 *
 * @return {express.RequestHandler}
 */
Builds HTTP middleware that serves redirects for content found in dist/.
Builds HTTP middleware that serves redirects for content found in dist / .
function buildUniqueRedirectHandler() {
  let replacedHandler = (_req, _res, next) => next();
  buildMatcher()
    .then(match => {
      replacedHandler = (req, res, next) => {
        let {url} = req;
        let enPrefixAdded = false;
        if (!url.startsWith('/en/')) {
          url = path.join('/en', url);
          enPrefixAdded = true;
        }
        let redirectTo = match(url);
        if (!redirectTo) {
          debug && console.warn('unique handler could not match:', url);
          return next();
        }
        if (enPrefixAdded && redirectTo.startsWith('/en/')) {
          redirectTo = redirectTo.substr('/en'.length);
        }
        debug && console.warn('unique handler matched:', url, '=>', redirectTo);
        return doRedirect(res, redirectTo);
      };
    })
    .catch(err => console.error('failed to build uniqueRedirectHandler', err));
  return (req, res, next) => replacedHandler(req, res, next);
}
function buildUniqueRedirectHandler ( ) { let replacedHandler = ( _req , _res , next ) => next ( ) ; buildMatcher ( ) . then ( match => { replacedHandler = ( req , res , next ) => { let { url } = req ; let enPrefixAdded = false ; if ( ! url . startsWith ( '/en/' ) ) { url = path . join ( '/en' , url ) ; enPrefixAdded = true ; } let redirectTo = match ( url ) ; if ( ! redirectTo ) { debug && console . warn ( 'unique handler could not match:' , url ) ; return next ( ) ; } if ( enPrefixAdded && redirectTo . startsWith ( '/en/' ) ) { redirectTo = redirectTo . substr ( '/en' . length ) ; } debug && console . warn ( 'unique handler matched:' , url , '=>' , redirectTo ) ; return doRedirect ( res , redirectTo ) ; } ; } ) . catch ( err => console . error ( 'failed to build uniqueRedirectHandler' , err ) ) ; return ( req , res , next ) => replacedHandler ( req , res , next ) ; }
Builds HTTP middleware that serves redirects for content found in dist/.
Builds HTTP middleware that serves redirects for content found in dist / .
/** @type {express.RequestHandler} */ // Build the handler async, and once it's ready, insert into the 404 handler path. /**
       * @type {express.RequestHandler}
       */ // For now, we only support data in "/en", so check that actual folder for redirects. // If we can't match, continue on to a 404 handler. // Awkwardly remove "/en" if we added it to do our check.