Skip to content

Commit

Permalink
Remove search code from /articles
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa committed Feb 4, 2024
1 parent 1e3993c commit 63ab2d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 63 deletions.
10 changes: 1 addition & 9 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,10 @@ export default {
},

articles: {
empty: {
title: "No Results",
body: "The requested URL /articles?q={{term}} was not found on this server.",
},

title: "Articles",

meta: {
title: {
default: "Articles of sergiodxa",
search: 'Search results for "{{term}}" on sergiodxa\' blog',
},
title: "Articles of sergiodxa",
},

header: {
Expand Down
15 changes: 4 additions & 11 deletions app/routes/_.articles/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ import { isEmpty } from "~/utils/arrays";

const SearchResultSchema = z.object({ path: z.string(), title: z.string() });

export async function queryArticles(
context: AppLoadContext,
query: string | null,
) {
export async function queryArticles(context: AppLoadContext) {
let cache = new Cache.KVStore(context.kv.cache, context.waitUntil);
let db = database(context.db);

let key = query ? `articles:search:${query}` : "articles:list";

let result = await cache.fetch(
key,
"articles:list",
async () => {
let articles = query
? await Article.search({ db }, query)
: await Article.list({ db });
let articles = await Article.list({ db });

return JSON.stringify(
articles.map((article) => {
Expand All @@ -36,7 +29,7 @@ export async function queryArticles(

let data = SearchResultSchema.array().parse(JSON.parse(result));

if (isEmpty(data)) context.waitUntil(cache.delete(key));
if (isEmpty(data)) context.waitUntil(cache.delete("articles:list"));

return data;
}
66 changes: 23 additions & 43 deletions app/routes/_.articles/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
json,
} from "@remix-run/cloudflare";
import { useLoaderData } from "@remix-run/react";
import { z } from "zod";

import { PageHeader } from "~/components/page-header";
import { Subscribe } from "~/components/subscribe";
Expand All @@ -21,43 +20,35 @@ export async function loader({ request, context }: LoaderFunctionArgs) {

let url = new URL(request.url);

let term = z
.string()
.transform((v) => v.toLowerCase())
.nullable()
.parse(url.searchParams.get("q"));

let headers = new Headers({
"cache-control": "max-age=1, s-maxage=1, stale-while-revalidate",
});

let t = await new I18n().getFixedT(request);

try {
let articles = await queryArticles(context, term);

let meta: MetaDescriptor[] = [];

if (term === "") {
meta.push({ title: t("articles.meta.title.default") });
} else {
meta.push({ title: t("articles.meta.title.search", { term }) });
}

meta.push({
tagName: "link",
rel: "alternate",
type: "application/rss+xml",
href: "/articles.rss",
});

meta.push({
tagName: "link",
rel: "canonical",
href: new URL("/articles", url).toString(),
});

return json({ term: term ?? undefined, meta, articles }, { headers });
let articles = await queryArticles(context);

return json(
{
articles,
meta: [
{ title: t("articles.meta.title") },
{
tagName: "link",
rel: "alternate",
type: "application/rss+xml",
href: "/articles.rss",
},
{
tagName: "link",
rel: "canonical",
href: new URL("/articles", url).toString(),
},
] satisfies MetaDescriptor[],
},
{ headers },
);
} catch (error) {
if (error instanceof Error) {
throw json({ message: error.message }, 500);
Expand All @@ -69,20 +60,9 @@ export async function loader({ request, context }: LoaderFunctionArgs) {
export const meta: MetaFunction<typeof loader> = ({ data }) => data?.meta ?? [];

export default function Articles() {
let { articles, term } = useLoaderData<typeof loader>();
let { articles } = useLoaderData<typeof loader>();
let t = useT("articles");

let count = articles.length;

if (count === 0) {
return (
<main className="mx-auto max-w-screen-sm space-y-4">
<h2 className="text-3xl font-bold">{t("empty.title")}</h2>
<p>{t("empty.body", { term })}</p>
</main>
);
}

return (
<main className="mx-auto max-w-screen-sm space-y-2">
<PageHeader t={t} />
Expand Down

0 comments on commit 63ab2d4

Please sign in to comment.