Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit serialized post props to reduce search size #160

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { FC } from 'react';
import type { Post } from '../utils/posts.ts';
import type { SimplePost } from '../utils/posts.ts';
import { useCallback, useMemo, useState } from 'react';
import { PostHint } from './post/PostHint.tsx';
import Fuse from 'fuse.js';

export interface SearchProps {
posts: Post[];
posts: SimplePost[];
}

const loadSearchFromQueryParams = () => {
Expand Down Expand Up @@ -55,7 +55,9 @@ export const Search: FC<SearchProps> = ({ posts }) => {
{searchValue !== '' && <p className="text-center">No results found</p>}
</>
)}
{results.map((post, index) => <PostHint key={index} post={post.item} />)}
{results.map(({ item: post }, index) => (
<PostHint key={index} url={post.url} title={post.title} excerpt={post.excerpt} />
))}
</div>
</section>
</>
Expand Down
12 changes: 5 additions & 7 deletions src/components/post/PostHint.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import type { FC } from 'react';
import type { Post } from '../../utils/posts';
import type { SimplePost } from '../../utils/posts';
import Link from '../Link';

export interface PostHintProps {
post: Post;
}
export type PostHintProps = Pick<SimplePost, 'url' | 'title' | 'excerpt'>

export const PostHint: FC<PostHintProps> = ({ post }) => (
export const PostHint: FC<PostHintProps> = ({ url, title, excerpt }) => (
<article>
<dl>
<dt className="entry-title">
<Link href={post.url} rel="bookmark">{post.data.title}</Link>
<Link href={url} rel="bookmark">{title}</Link>
</dt>
<dd className="entry-summary">
<p>{post.excerpt}</p>
<p>{excerpt}</p>
</dd>
</dl>
</article>
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/TaxonomyPage.astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let currentYear: string | null = null;
return (
<div>
{shouldShowYear && <h3 class="article-date-group">&mdash; {year} &mdash;</h3>}
<PostHint post={post} />
<PostHint url={post.url} title={post.data.title} excerpt={post.excerpt} />
</div>
);
})}
Expand Down
6 changes: 3 additions & 3 deletions src/pages/search.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Container } from '../components/Container';
import { SectionTitle } from '../components/SectionTitle';
import { Search } from '../components/Search';
import Layout from '../layouts/Layout.astro';
import { getAllPosts } from '../utils/posts';
import { getAllPosts, SimplePost } from '../utils/posts';

const posts = await getAllPosts().then((posts) =>
const posts: SimplePost[] = await getAllPosts().then((posts) =>
// Get rid of the `render` function, which cannot be serialized
(posts as any[]).map(({ render, ...post }) => post)
posts.map(({ body, excerpt, url, data }) => ({ body, excerpt, url, title: data.title }))
);
---

Expand Down
15 changes: 8 additions & 7 deletions src/utils/posts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getCollection, z } from 'astro:content';
import { format, parse } from 'date-fns';
import { decode } from 'html-entities';
import type { TaxonomiesType } from '../components/types';
import { renderMarkdown } from './markdown';

export const PAGE_SIZE = 5;
Expand Down Expand Up @@ -41,6 +40,10 @@ export interface Post {
data: PostMeta;
}

export type SimplePost = Pick<Post, 'body' | 'excerpt' | 'url'> & {
title: string;
}

const postExcerpt = (body: string) => {
const excerpt = renderMarkdown(body)
.split('\n')
Expand Down Expand Up @@ -85,14 +88,12 @@ export const getLatestPosts = async () => {
return posts.slice(0, PAGE_SIZE);
};

const getTaxonomiesFactory = (taxonomy: TaxonomiesType) => async (): Promise<string[]> => {
const posts = await getAllPosts();
return [...new Set(posts.flatMap(({ data }) => data[taxonomy]))].sort();
};

export const getCategories = () => categories;

export const getTags = getTaxonomiesFactory('tags');
export const getTags = async (): Promise<string[]> => {
const posts = await getAllPosts();
return [...new Set(posts.flatMap(({ data }) => data.tags))].sort();
};

export type PostFilter = { category: CategoryType } | { tag: string };

Expand Down