Skip to content

Commit

Permalink
Try revalidateTag
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Aug 4, 2023
1 parent 323e9e8 commit 1f0585e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/app/posts/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ export async function generateMetadata({
title: frontmatter.title,
description: excerpt,
openGraph: {
title: {
default: siteMetadata.title,
template: `%s | ${siteMetadata.title}`,
},
title: frontmatter.title,
description: excerpt,
url: `${siteMetadata.origin}/posts/${slug}`,
siteName: siteMetadata.title,
Expand Down
24 changes: 19 additions & 5 deletions src/app/rss.xml/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import RSS from 'rss';
import { revalidateTag } from 'next/cache';
import { getPosts } from '@/internals/posts';
import siteMetadata from '@/siteMetadata';

export const revalidate = false; // Cache forever like a static file
// export const revalidate = 0; // Cache forever like a static file

export async function GET() {
const feed = new RSS({
Expand All @@ -16,11 +17,24 @@ export async function GET() {

for (const post of posts) {
const postUrl = new URL(`/posts/${post.slug}`, siteMetadata.origin).href;
const html = await fetch(postUrl, { redirect: 'follow' }).then((res) =>
res.text(),
const html = await fetch(postUrl, {
next: { tags: ['rss'] },
}).then((response) => response.text());
const isBuilding = !html.includes(
`<meta property="og:site_name" content="${siteMetadata.title}"/>`,
);
const contentHtml =
html.match(/<main(?:[^>]*?)>([\s\S]+?)<\/main>/)?.[1] || '';

if (isBuilding) {
revalidateTag('rss');
return new Response('Site building..., please try again later.', {
status: 503,
headers: {
'Retry-After': '60',
},
});
}

const contentHtml = html.match(/<main(?:[^>]*?)>([\s\S]+?)<\/main>/)?.[1];
const image = html.match(
/<meta property="og:image" content="([\s\S]+?)"\s?\/?>/,
)?.[1];
Expand Down

0 comments on commit 1f0585e

Please sign in to comment.