Skip to content

Commit

Permalink
refactor promise tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
yuheiy committed May 6, 2024
1 parent 3de4618 commit 0cf8738
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
10 changes: 6 additions & 4 deletions src/pages/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { getBlogDescription } from '../lib/get-blog-description';
import { renderDateToHtml } from '../lib/render-date-to-html';
export async function getStaticPaths() {
return (await getCollection('blog')).map((entry) => ({
params: { slug: entry.slug },
props: { entry },
}));
return getCollection('blog').then((entries) =>
entries.map((entry) => ({
params: { slug: entry.slug },
props: { entry },
})),
);
}
type Props = Awaited<ReturnType<typeof getStaticPaths>>[number]['props'];
Expand Down
32 changes: 16 additions & 16 deletions src/pages/blog.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ import { renderDateToHtml } from '../lib/render-date-to-html';
const title = 'ブログ';
const blogEntries = (await getCollection('blog'))
.toSorted((a, b) => a.data.pubDate.valueOf() - b.data.pubDate.valueOf())
.toReversed();
const allBlogItems = await Promise.all(
blogEntries.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
);
const items = await getCollection('blog')
.then((entries) =>
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
)
.then((promises) => Promise.all(promises));
---

<Layout title={title}>
<PageHeader title={title} />

<main class="wrapper">
<DetailsList headingLevel={2} items={allBlogItems} clampDescription />
<DetailsList headingLevel={2} items={items} clampDescription />
</main>
</Layout>
32 changes: 17 additions & 15 deletions src/pages/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ import { SITE_DESCRIPTION, SITE_TITLE } from '../consts';
import { getBlogDescription } from '../lib/get-blog-description';

export async function GET(context: APIContext) {
const blogEntries = (await getCollection('blog'))
.toSorted((a, b) => a.data.pubDate.valueOf() - b.data.pubDate.valueOf())
.toReversed();

invariant(context.site);

const items = await getCollection('blog')
.then((entries) =>
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
link: `/${entry.slug}`,
title: entry.data.title,
pubDate: entry.data.pubDate,
description,
};
}),
)
.then((promises) => Promise.all(promises));

return rss({
title: SITE_TITLE,
description: SITE_DESCRIPTION,
site: context.site,
items: await Promise.all(
blogEntries.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
link: `/${entry.slug}`,
title: entry.data.title,
pubDate: entry.data.pubDate,
description,
};
}),
),
items,
trailingSlash: false,
});
}
35 changes: 18 additions & 17 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
import type { CollectionEntry } from 'astro:content';
import { getCollection, getEntry } from 'astro:content';
import DetailsList from '../components/DetailsList.astro';
import type { DetailsListItem } from '../components/DetailsList.astro';
import DetailsList from '../components/DetailsList.astro';
import PageHeader from '../components/PageHeader.astro';
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
import { SITE_DESCRIPTION, SITE_TITLE } from '../consts';
import Layout from '../layouts/Layout.astro';
import { getBlogDescription } from '../lib/get-blog-description';
import { renderDateToHtml } from '../lib/render-date-to-html';
Expand Down Expand Up @@ -111,21 +111,22 @@ const projectItems = [
},
] satisfies DetailsListItem[];
const blogEntries = (await getCollection('blog'))
.toSorted((a, b) => a.data.pubDate.valueOf() - b.data.pubDate.valueOf())
.toReversed();
const latestBlogItems = await Promise.all(
blogEntries.slice(0, 3).map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
);
const latestBlogItems = await getCollection('blog')
.then((entries) =>
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.slice(0, 3)
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
)
.then((promises) => Promise.all(promises));
---

<Layout isHome description={SITE_DESCRIPTION}>
Expand Down

0 comments on commit 0cf8738

Please sign in to comment.