Skip to content

Commit 5273fe2

Browse files
authored
Add <meta> tag for search order (#5859)
1 parent 9f0f1c1 commit 5273fe2

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/components/Layout/Page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface PageProps {
3535
export function Page({children, toc, routeTree, meta, section}: PageProps) {
3636
const {asPath} = useRouter();
3737
const cleanedPath = asPath.split(/[\?\#]/)[0];
38-
const {route, nextRoute, prevRoute, breadcrumbs} = getRouteMeta(
38+
const {route, nextRoute, prevRoute, breadcrumbs, order} = getRouteMeta(
3939
cleanedPath,
4040
routeTree
4141
);
@@ -96,12 +96,18 @@ export function Page({children, toc, routeTree, meta, section}: PageProps) {
9696
showSidebar = false;
9797
}
9898

99+
let searchOrder;
100+
if (section === 'learn' || (section === 'blog' && !isBlogIndex)) {
101+
searchOrder = order;
102+
}
103+
99104
return (
100105
<>
101106
<Seo
102107
title={title}
103108
isHomePage={isHomePage}
104109
image={`/images/og-` + section + '.png'}
110+
searchOrder={searchOrder}
105111
/>
106112
<SocialBanner />
107113
<TopNav

src/components/Layout/getRouteMeta.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,23 @@ export interface RouteMeta {
5454
route?: RouteItem;
5555
/** Trail of parent routes */
5656
breadcrumbs?: RouteItem[];
57+
/** Order in the section */
58+
order?: number;
5759
}
5860

61+
type TravesalContext = RouteMeta & {
62+
currentIndex: number;
63+
};
64+
5965
export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) {
6066
const breadcrumbs = getBreadcrumbs(cleanedPath, routeTree);
67+
const ctx: TravesalContext = {
68+
currentIndex: 0,
69+
};
70+
buildRouteMeta(cleanedPath, routeTree, ctx);
71+
const {currentIndex, ...meta} = ctx;
6172
return {
62-
...buildRouteMeta(cleanedPath, routeTree, {}),
73+
...meta,
6374
breadcrumbs: breadcrumbs.length > 0 ? breadcrumbs : [routeTree],
6475
};
6576
}
@@ -68,8 +79,10 @@ export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) {
6879
function buildRouteMeta(
6980
searchPath: string,
7081
currentRoute: RouteItem,
71-
ctx: RouteMeta
72-
): RouteMeta {
82+
ctx: TravesalContext
83+
) {
84+
ctx.currentIndex++;
85+
7386
const {routes} = currentRoute;
7487

7588
if (ctx.route && !ctx.nextRoute) {
@@ -78,6 +91,7 @@ function buildRouteMeta(
7891

7992
if (currentRoute.path === searchPath) {
8093
ctx.route = currentRoute;
94+
ctx.order = ctx.currentIndex;
8195
// If we've found a deeper match, reset the previously stored next route.
8296
// TODO: this only works reliably if deeper matches are first in the tree.
8397
// We should revamp all of this to be more explicit.
@@ -89,14 +103,12 @@ function buildRouteMeta(
89103
}
90104

91105
if (!routes) {
92-
return ctx;
106+
return;
93107
}
94108

95109
for (const route of routes) {
96110
buildRouteMeta(searchPath, route, ctx);
97111
}
98-
99-
return ctx;
100112
}
101113

102114
// iterates the route tree from the current route to find its ancestors for breadcrumbs

src/components/Seo.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface SeoProps {
1313
// jsonld?: JsonLDType | Array<JsonLDType>;
1414
children?: React.ReactNode;
1515
isHomePage: boolean;
16+
searchOrder?: number;
1617
}
1718

1819
export const Seo = withRouter(
@@ -23,6 +24,7 @@ export const Seo = withRouter(
2324
router,
2425
children,
2526
isHomePage,
27+
searchOrder,
2628
}: SeoProps & {router: Router}) => {
2729
const pageTitle = isHomePage ? 'React' : title + ' – React';
2830
// Twitter's meta parser is not very good.
@@ -96,6 +98,9 @@ export const Seo = withRouter(
9698
name="google-site-verification"
9799
content="sIlAGs48RulR4DdP95YSWNKZIEtCqQmRjzn-Zq-CcD0"
98100
/>
101+
{searchOrder != null && (
102+
<meta name="algolia-search-order" content={'' + searchOrder} />
103+
)}
99104
<link
100105
rel="preload"
101106
href="/fonts/Source-Code-Pro-Regular.woff2"

0 commit comments

Comments
 (0)