Skip to content

Commit

Permalink
feat(route): introduce /nextjs/blog (#16049)
Browse files Browse the repository at this point in the history
* feat(route): introduce `/nextjs/blog`

* fix: set limits to 20 posts

* fix: remove unnecessary cache according to #16038

* fix: cache individual post

* fix: cache item

* docs: fix name

---------
  • Loading branch information
equt authored Jul 2, 2024
1 parent 1abde86 commit e0c22f8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/routes/nextjs/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { DataItem, Route } from '@/types';
import { parseDate } from '@/utils/parse-date';
import cache from '@/utils/cache';
import { load } from 'cheerio';
import ofetch from '@/utils/ofetch';

const handler: Route['handler'] = async () => {
const data = await ofetch('https://nextjs.org/blog');

const $ = load(data);

const item = (await Promise.all(
$('article')
.toArray()
.slice(0, 20)
.map((item) => {
const $ = load(item);
const link = `https://nextjs.org${$('a[href^="/blog"]').attr('href')}`;

return cache.tryGet(`nextjs:blog:${link}`, async () => {
const data = await ofetch(link);

const $ = load(data);

return {
title: $('h1').first().text().trim(),
link,
description: $('div.prose').html() ?? '',
pubDate: parseDate(
$('p[data-version="v1"]')
.first()
.text()
.replace(/st|nd|rd|th/, '')
),
};
});
})
)) as DataItem[];

return {
title: 'Next.js Blog',
link: 'https://nextjs.org/blog',
language: 'en-US',
item,
};
};

export const route: Route = {
path: '/blog',
name: 'Blog',
categories: ['program-update'],
maintainers: ['equt'],
example: '/nextjs/blog',
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
handler,
};
6 changes: 6 additions & 0 deletions lib/routes/nextjs/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Next.js',
url: 'nextjs.org',
};

0 comments on commit e0c22f8

Please sign in to comment.