|
|
import { isDefaultLocale, isMagnificentLocale } from '@automattic/i18n-utils'; |
|
|
import { ssrSetupLocale } from 'calypso/controller'; |
|
|
import { setDocumentHeadMeta } from 'calypso/state/document-head/actions'; |
|
|
import { getDocumentHeadMeta } from 'calypso/state/document-head/selectors'; |
|
|
|
|
|
const VALID_QUERY_KEYS = [ 'client_id', 'signup_flow', 'redirect_to' ]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setShouldServerSideRenderLogin( context, next ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isLocaleValidForSSR = |
|
|
isDefaultLocale( context.lang ) || isMagnificentLocale( context.lang ); |
|
|
|
|
|
context.serverSideRender = |
|
|
|
|
|
Object.keys( context.query ).every( ( key ) => VALID_QUERY_KEYS.includes( key ) ) && |
|
|
isLocaleValidForSSR && |
|
|
isRedirectToValidForSsr( context.query.redirect_to ); |
|
|
|
|
|
next(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isRedirectToValidForSsr( redirectToQueryValue ) { |
|
|
if ( redirectToQueryValue === undefined ) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
const redirectToDecoded = decodeURIComponent( redirectToQueryValue ); |
|
|
return ( |
|
|
redirectToDecoded.startsWith( 'https://wordpress.com/theme' ) || |
|
|
|
|
|
redirectToDecoded.startsWith( 'https://wordpress.com/go' ) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function ssrSetupLocaleLogin( context, next ) { |
|
|
if ( context.serverSideRender ) { |
|
|
ssrSetupLocale( context, next ); |
|
|
return; |
|
|
} |
|
|
|
|
|
next(); |
|
|
} |
|
|
|
|
|
export function setMetaTags( context, next ) { |
|
|
const pathSegments = context.pathname.replace( /^[/]|[/]$/g, '' ).split( '/' ); |
|
|
const hasQueryString = Object.keys( context.query ).length > 0; |
|
|
const hasMag16LocaleParam = isMagnificentLocale( context.params?.lang ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( hasQueryString || pathSegments.length > ( hasMag16LocaleParam ? 2 : 1 ) ) { |
|
|
const meta = getDocumentHeadMeta( context.store.getState() ) |
|
|
|
|
|
.filter( ( { name } ) => name !== 'robots' ) |
|
|
|
|
|
.concat( { |
|
|
name: 'robots', |
|
|
content: 'noindex', |
|
|
} ); |
|
|
|
|
|
context.store.dispatch( setDocumentHeadMeta( meta ) ); |
|
|
} |
|
|
|
|
|
next(); |
|
|
} |
|
|
|