File size: 4,381 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import { getLanguageRouteParam } from '@automattic/i18n-utils';
import { makeLayout, ssrSetupLocale } from 'calypso/controller';
import {
excludeSearchFromCanonicalUrlAndHrefLangLinks,
setHrefLangLinks,
setLocalizedCanonicalUrl,
} from 'calypso/controller/localized-links';
import { CategoryGalleryServer } from 'calypso/my-sites/patterns/components/category-gallery/server';
import { PatternGalleryServer } from 'calypso/my-sites/patterns/components/pattern-gallery/server';
import { PatternLibrary } from 'calypso/my-sites/patterns/components/pattern-library';
import { ReadymadeTemplateDetails } from 'calypso/my-sites/patterns/components/readymade-template-details';
import { PatternsContext } from 'calypso/my-sites/patterns/context';
import { getPatternCategoriesQueryOptions } from 'calypso/my-sites/patterns/hooks/use-pattern-categories';
import { getPatternsQueryOptions } from 'calypso/my-sites/patterns/hooks/use-patterns';
import { QUERY_PARAM_SEARCH } from 'calypso/my-sites/patterns/lib/filter-patterns-by-term';
import {
PatternTypeFilter,
type RouterContext,
type RouterNext,
type Pattern,
} from 'calypso/my-sites/patterns/types';
import { PatternsWrapper } from 'calypso/my-sites/patterns/wrapper';
import { serverRouter } from 'calypso/server/isomorphic-routing';
import performanceMark from 'calypso/server/lib/performance-mark';
import { getCurrentUserLocale } from 'calypso/state/current-user/selectors';
function renderPatterns( context: RouterContext, next: RouterNext ) {
performanceMark( context, 'renderPatterns' );
context.primary = (
<PatternsContext.Provider
value={ {
category: context.params.category ?? '',
isGridView: !! context.query.grid,
section: context.hashstring,
patternTypeFilter:
context.params.type === 'layouts' ? PatternTypeFilter.PAGES : PatternTypeFilter.REGULAR,
searchTerm: context.query[ QUERY_PARAM_SEARCH ] ?? '',
} }
>
<PatternsWrapper>
<PatternLibrary
categoryGallery={ CategoryGalleryServer }
patternGallery={ PatternGalleryServer }
/>
</PatternsWrapper>
</PatternsContext.Provider>
);
next();
}
function fetchCategoriesAndPatterns( context: RouterContext, next: RouterNext ) {
performanceMark( context, 'fetchCategoriesAndPatterns' );
const { cachedMarkup, queryClient, lang, params, store } = context;
// Bypasses fetching if the rendered page is cached, or if any query parameters were passed in the URL
if ( cachedMarkup || Object.keys( context.query ).length > 0 ) {
next();
return;
}
const locale = getCurrentUserLocale( store.getState() ) || lang || 'en';
performanceMark( context, 'getPatternCategories', true );
// Fetches the list of categories first, then fetches patterns if a specific category was requested
queryClient
.fetchQuery( getPatternCategoriesQueryOptions( locale ) )
.then( ( categories ) => {
if ( ! params.category ) {
return;
}
const categoryNames = categories.map( ( category ) => category.name );
if ( ! categoryNames.includes( params.category ) ) {
throw {
status: 404,
message: 'Category Not Found',
};
}
performanceMark( context, 'getPatterns', true );
return queryClient.fetchQuery< Pattern[] >(
getPatternsQueryOptions( locale, params.category )
);
} )
.then( () => {
next();
} )
.catch( ( error ) => {
next( error );
} );
}
function renderReadymadeTemplateDetails( context: RouterContext, next: RouterNext ) {
performanceMark( context, 'renderReadymadeTemplateDetails' );
context.primary = (
<PatternsWrapper>
<ReadymadeTemplateDetails slug={ context.params.slug } />
</PatternsWrapper>
);
next();
}
export default function ( router: ReturnType< typeof serverRouter > ) {
const langParam = getLanguageRouteParam();
router(
[
`/${ langParam }/patterns/:category?`,
`/${ langParam }/patterns/:type(layouts)/:category?`,
'/patterns/:category?',
'/patterns/:type(layouts)/:category?',
],
ssrSetupLocale,
excludeSearchFromCanonicalUrlAndHrefLangLinks,
setHrefLangLinks,
setLocalizedCanonicalUrl,
fetchCategoriesAndPatterns,
renderPatterns,
makeLayout
);
router(
[ '/patterns/:type(site-layouts)/:slug' ],
ssrSetupLocale,
excludeSearchFromCanonicalUrlAndHrefLangLinks,
setHrefLangLinks,
setLocalizedCanonicalUrl,
renderReadymadeTemplateDetails,
makeLayout
);
}
|