Skip to content

Commit

Permalink
feat: add minified RSS
Browse files Browse the repository at this point in the history
  • Loading branch information
ouuan committed Oct 28, 2024
1 parent 1d3bed6 commit d8d49e9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 18 deletions.
2 changes: 1 addition & 1 deletion functions/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function trackFeeds(request: Request) {
export const onRequestGet: PagesFunction = (context) => {
const { request } = context;

if (/\/feed\.(?:xml|atom|json)\/*$/.test(new URL(request.url).pathname)) {
if (/\/feed(?:\.min)?\.(?:xml|atom|json)\/*$/.test(new URL(request.url).pathname)) {
context.waitUntil(trackFeeds(request));
}

Expand Down
18 changes: 18 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ export default defineApp({
href: '/feed.json',
title: `JSON Feed - ${site.title}`,
},
{
rel: 'alternate',
type: 'application/rss+xml',
href: '/feed.min.xml',
title: `RSS Feed (no full text) - ${site.title}`,
},
{
rel: 'alternate',
type: 'application/atom+xml',
href: '/feed.min.atom',
title: `Atom Feed (no full text) - ${site.title}`,
},
{
rel: 'alternate',
type: 'application/json',
href: '/feed.min.json',
title: `JSON Feed (no full text) - ${site.title}`,
},
{ rel: 'dns-prefetch', href: 'https://plausible.ouuan.moe' },
{ rel: 'preconnect', href: 'https://blog-visitor-count.ouuan.moe' },
{ rel: 'stylesheet', href: '/vendors/katex/katex.css' },
Expand Down
16 changes: 10 additions & 6 deletions src/composables/useFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCopyrightYearString } from './useCopyrightYear';
import { usePosts } from './usePosts';
import { useTagFilter } from './useTags';

export default function useFeed(tag?: string) {
export default function useFeed({ tag, min }: { tag?: string, min?: boolean } = {}) {
const { site } = usePage();
const {
author,
Expand All @@ -17,7 +17,10 @@ export default function useFeed(tag?: string) {
} = site;
const yearString = useCopyrightYearString();

const link = tag ? new URL(`/tag/${tag}`, url).href : url;
const tagPath = tag ? `/tag/${tag}` : '';
const minSuffix = min ? '.min' : '';

const link = new URL(tagPath, url).href;

const options: FeedOptions = {
title: tag ? `${title}: 标签: ${tag}` : title,
Expand All @@ -29,9 +32,9 @@ export default function useFeed(tag?: string) {
copyright: `Copyright © ${yearString} ${site.author}
Licensed under CC BY-SA 4.0`,
feedLinks: {
atom: new URL(`${tag ? `/tag/${tag}` : ''}/feed.atom`, url).href,
rss: new URL(`${tag ? `/tag/${tag}` : ''}/feed.xml`, url).href,
json: new URL(`${tag ? `/tag/${tag}` : ''}/feed.json`, url).href,
atom: new URL(`${tagPath}/feed${minSuffix}.atom`, url).href,
rss: new URL(`${tagPath}/feed${minSuffix}.xml`, url).href,
json: new URL(`${tagPath}/feed${minSuffix}.json`, url).href,
},
author: {
name: author,
Expand All @@ -42,6 +45,7 @@ Licensed under CC BY-SA 4.0`,
const items = usePosts({
filter: tag ? useTagFilter(tag) : () => true,
pageIndex: 1,
perPage: min ? 5 : undefined,
}).value.map<FeedItem>((post) => {
let category: FeedItem['category'];
if (Array.isArray(post.frontmatter.tags)) {
Expand All @@ -58,9 +62,9 @@ Licensed under CC BY-SA 4.0`,
id: new URL(post.href, url).href,
link: new URL(post.href, url).href,
description: h(post, { excerpt: true }),
content: post,
date: post.frontmatter.published,
category,
...(min ? {} : { content: post }),
};
}).sort((lhs, rhs) => rhs.date.valueOf() - lhs.date.valueOf());

Expand Down
19 changes: 13 additions & 6 deletions src/pages/feed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ import useFeed from '~/composables/useFeed';
export default definePageComponent({
getStaticPaths() {
return ['rss', 'atom', 'json'].map((format) => ({
params: { filename: format === 'rss' ? 'feed.xml' : `feed.${format}` },
props: { format },
}));
return ['rss', 'atom', 'json'].flatMap(
(format) => [true, false].map((min) => {
const suffix = format === 'rss' ? 'xml' : format;
const filename = min ? `feed.min.${suffix}` : `feed.${suffix}`;
return {
params: { filename },
props: { format, min },
};
}),
);
},
});
</script>

<script setup lang="ts">
defineProps<{
const props = defineProps<{
format: 'rss' | 'atom' | 'json';
min: boolean;
}>();
const { options, items } = useFeed();
const { options, items } = useFeed(props);
</script>
18 changes: 18 additions & 0 deletions src/pages/tag/[tag].vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ useHead({
href: `/tag/${props.tag}/feed.json`,
title: `JSON Feed - ${title}`,
},
{
rel: 'alternate',
type: 'application/rss+xml',
href: `/tag/${props.tag}/feed.min.xml`,
title: `RSS Feed (no full text) - ${title}`,
},
{
rel: 'alternate',
type: 'application/atom+xml',
href: `/tag/${props.tag}/feed.min.atom`,
title: `Atom Feed (no full text) - ${title}`,
},
{
rel: 'alternate',
type: 'application/json',
href: `/tag/${props.tag}/feed.min.json`,
title: `JSON Feed (no full text) - ${title}`,
},
],
});
</script>
19 changes: 14 additions & 5 deletions src/pages/tag/[tag]/feed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ const tags = useTags();
export default definePageComponent({
getStaticPaths() {
return ['rss', 'atom', 'json'].flatMap((format) => tags.value.map((tag) => ({
params: { tag, filename: format === 'rss' ? 'feed.xml' : `feed.${format}` },
props: { tag, format },
})));
return ['rss', 'atom', 'json'].flatMap(
(format) => [true, false].flatMap(
(min) => tags.value.map((tag) => {
const suffix = format === 'rss' ? 'xml' : format;
const filename = min ? `feed.min.${suffix}` : `feed.${suffix}`;
return {
params: { tag, filename },
props: { tag, format, min },
};
}),
),
);
},
});
</script>
Expand All @@ -30,7 +38,8 @@ export default definePageComponent({
const props = defineProps<{
tag: string;
format: 'rss' | 'atom' | 'json';
min: boolean;
}>();
const { options, items } = useFeed(props.tag);
const { options, items } = useFeed(props);
</script>

0 comments on commit d8d49e9

Please sign in to comment.