Browse Source

Add <meta> tag for search order (#5859)

main
dan 2 years ago
committed by GitHub
parent
commit
5273fe2a9a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/components/Layout/Page.tsx
  2. 24
      src/components/Layout/getRouteMeta.tsx
  3. 5
      src/components/Seo.tsx

8
src/components/Layout/Page.tsx

@ -35,7 +35,7 @@ interface PageProps {
export function Page({children, toc, routeTree, meta, section}: PageProps) { export function Page({children, toc, routeTree, meta, section}: PageProps) {
const {asPath} = useRouter(); const {asPath} = useRouter();
const cleanedPath = asPath.split(/[\?\#]/)[0]; const cleanedPath = asPath.split(/[\?\#]/)[0];
const {route, nextRoute, prevRoute, breadcrumbs} = getRouteMeta( const {route, nextRoute, prevRoute, breadcrumbs, order} = getRouteMeta(
cleanedPath, cleanedPath,
routeTree routeTree
); );
@ -96,12 +96,18 @@ export function Page({children, toc, routeTree, meta, section}: PageProps) {
showSidebar = false; showSidebar = false;
} }
let searchOrder;
if (section === 'learn' || (section === 'blog' && !isBlogIndex)) {
searchOrder = order;
}
return ( return (
<> <>
<Seo <Seo
title={title} title={title}
isHomePage={isHomePage} isHomePage={isHomePage}
image={`/images/og-` + section + '.png'} image={`/images/og-` + section + '.png'}
searchOrder={searchOrder}
/> />
<SocialBanner /> <SocialBanner />
<TopNav <TopNav

24
src/components/Layout/getRouteMeta.tsx

@ -54,12 +54,23 @@ export interface RouteMeta {
route?: RouteItem; route?: RouteItem;
/** Trail of parent routes */ /** Trail of parent routes */
breadcrumbs?: RouteItem[]; breadcrumbs?: RouteItem[];
/** Order in the section */
order?: number;
} }
type TravesalContext = RouteMeta & {
currentIndex: number;
};
export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) { export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) {
const breadcrumbs = getBreadcrumbs(cleanedPath, routeTree); const breadcrumbs = getBreadcrumbs(cleanedPath, routeTree);
const ctx: TravesalContext = {
currentIndex: 0,
};
buildRouteMeta(cleanedPath, routeTree, ctx);
const {currentIndex, ...meta} = ctx;
return { return {
...buildRouteMeta(cleanedPath, routeTree, {}), ...meta,
breadcrumbs: breadcrumbs.length > 0 ? breadcrumbs : [routeTree], breadcrumbs: breadcrumbs.length > 0 ? breadcrumbs : [routeTree],
}; };
} }
@ -68,8 +79,10 @@ export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) {
function buildRouteMeta( function buildRouteMeta(
searchPath: string, searchPath: string,
currentRoute: RouteItem, currentRoute: RouteItem,
ctx: RouteMeta ctx: TravesalContext
): RouteMeta { ) {
ctx.currentIndex++;
const {routes} = currentRoute; const {routes} = currentRoute;
if (ctx.route && !ctx.nextRoute) { if (ctx.route && !ctx.nextRoute) {
@ -78,6 +91,7 @@ function buildRouteMeta(
if (currentRoute.path === searchPath) { if (currentRoute.path === searchPath) {
ctx.route = currentRoute; ctx.route = currentRoute;
ctx.order = ctx.currentIndex;
// If we've found a deeper match, reset the previously stored next route. // If we've found a deeper match, reset the previously stored next route.
// TODO: this only works reliably if deeper matches are first in the tree. // TODO: this only works reliably if deeper matches are first in the tree.
// We should revamp all of this to be more explicit. // We should revamp all of this to be more explicit.
@ -89,14 +103,12 @@ function buildRouteMeta(
} }
if (!routes) { if (!routes) {
return ctx; return;
} }
for (const route of routes) { for (const route of routes) {
buildRouteMeta(searchPath, route, ctx); buildRouteMeta(searchPath, route, ctx);
} }
return ctx;
} }
// iterates the route tree from the current route to find its ancestors for breadcrumbs // iterates the route tree from the current route to find its ancestors for breadcrumbs

5
src/components/Seo.tsx

@ -13,6 +13,7 @@ export interface SeoProps {
// jsonld?: JsonLDType | Array<JsonLDType>; // jsonld?: JsonLDType | Array<JsonLDType>;
children?: React.ReactNode; children?: React.ReactNode;
isHomePage: boolean; isHomePage: boolean;
searchOrder?: number;
} }
export const Seo = withRouter( export const Seo = withRouter(
@ -23,6 +24,7 @@ export const Seo = withRouter(
router, router,
children, children,
isHomePage, isHomePage,
searchOrder,
}: SeoProps & {router: Router}) => { }: SeoProps & {router: Router}) => {
const pageTitle = isHomePage ? 'React' : title + ' – React'; const pageTitle = isHomePage ? 'React' : title + ' – React';
// Twitter's meta parser is not very good. // Twitter's meta parser is not very good.
@ -96,6 +98,9 @@ export const Seo = withRouter(
name="google-site-verification" name="google-site-verification"
content="sIlAGs48RulR4DdP95YSWNKZIEtCqQmRjzn-Zq-CcD0" content="sIlAGs48RulR4DdP95YSWNKZIEtCqQmRjzn-Zq-CcD0"
/> />
{searchOrder != null && (
<meta name="algolia-search-order" content={'' + searchOrder} />
)}
<link <link
rel="preload" rel="preload"
href="/fonts/Source-Code-Pro-Regular.woff2" href="/fonts/Source-Code-Pro-Regular.woff2"

Loading…
Cancel
Save