Skip to content

Commit

Permalink
Merge pull request #27 from Blazity/rss
Browse files Browse the repository at this point in the history
Rss
  • Loading branch information
Pierniki authored Sep 18, 2023
2 parents a108884 + ec7a849 commit eb9a0b2
Show file tree
Hide file tree
Showing 8 changed files with 2,550 additions and 3,670 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@vercel/otel": "^0.3.0",
"algoliasearch": "^4.19.1",
"class-variance-authority": "^0.7.0",
"feed": "^4.2.2",
"graphql": "^16.8.0",
"graphql-request": "^6.1.0",
"lodash": "^4.17.21",
Expand Down
14 changes: 12 additions & 2 deletions src/app/[lang]/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Metadata } from "next"
import { notFound } from "next/navigation"
import { RichText } from "components/RichText/RichText"
import { HygraphClient } from "hygraphClient"
import { Locale } from "i18n"
import { useLocale } from "store"

type CustomPageProps = {
params: { slug: string }
params: { slug: string; lang: Locale }
}

export async function generateMetadata({ params: { slug } }: CustomPageProps): Promise<Metadata | null> {
Expand All @@ -19,7 +21,15 @@ export async function generateMetadata({ params: { slug } }: CustomPageProps): P
}
}

export default async function Web({ params: { slug } }: CustomPageProps) {
export async function generateStaticParams() {
const { getPagesConfig } = HygraphClient()
const { pages } = await getPagesConfig({})

return pages
}

export default async function Web({ params: { slug, lang } }: CustomPageProps) {
useLocale.setState({ locale: lang })
const { getPageContent } = HygraphClient()
const { pages } = await getPageContent({ slug })
const page = pages[0]
Expand Down
15 changes: 14 additions & 1 deletion src/app/[lang]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import Head from "next/head"
import { DynamicLangSelect } from "components/LangSelect/DynamicLangSelect"
import { DynamicSearchDialog } from "components/Search/DynamicSearchDialog"
import type { Locale } from "i18n"
import { i18n, type Locale } from "i18n"
import "../../styles/tailwind.css"
import { useLocale } from "store"
import { GoogleAnalytics } from "./GoogleAnalytics"
import Providers from "./Providers"

export default function Layout({ children, params }: { children: React.ReactNode; params: { lang: string } }) {
const lang = params.lang as Locale
const locales = i18n.locales

useLocale.setState({ locale: lang })

return (
<html lang={lang}>
<Head>
{locales.map((local) => (
<link
key={local}
rel="alternate"
type="application/rss+xml"
title={`RSS Feed for ${local} Content`}
href={`/${local}.xml`}
/>
))}
</Head>
<GoogleAnalytics />
<Providers lang={lang}>
<body>
Expand Down
3 changes: 3 additions & 0 deletions src/app/api/algolia-webhook/publish/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import generateRssFeed from "@utils/generateRSSFeed"
import { revalidatePath } from "next/cache"
import { NextRequest, NextResponse } from "next/server"
import { z } from "zod"
Expand All @@ -22,6 +23,8 @@ async function handleAlgoliaPublishWebhook(req: NextRequestWithValidBody<z.infer
content: slateToText(content),
slug,
})
await generateRssFeed(locale)


return { title, locale }
})
Expand Down
29 changes: 29 additions & 0 deletions src/hygraphClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ const getArticles = graphql(`
}
`)

const getRecentArticlesWithMetadata = graphql(`
query getArticlesWithMeta($locales: [Locale!]!) {
articles(locales: $locales, first: 50, orderBy: updatedAt_ASC) {
author {
name
}
createdAt
locale
slug
title
updatedAt
coverImage {
url
}
}
}
`)

const getPagesConfig = graphql(`
query getPagesSlug {
pages {
slug
locale
}
}
`)

const getPageContent = graphql(`
query getPageContent($locales: [Locale!]!, $slug: String!) {
pages(locales: $locales, where: { slug: $slug }) {
Expand Down Expand Up @@ -108,6 +135,8 @@ export const HygraphClient = () => {
}

return {
getRecentArticlesWithMetadata: makeRequest(getRecentArticlesWithMetadata),
getPagesConfig: makeRequest(getPagesConfig),
getPageContent: makeRequest(getPageContent),
getArticles: makeRequest(getArticles),
getArticleSummary: makeRequest(getArticleSummary),
Expand Down
45 changes: 45 additions & 0 deletions src/utils/generateRSSFeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Feed } from "feed"
import fs from "fs"
import { HygraphClient } from "hygraphClient"

export default async function generateRssFeed(locale: string) {
const site_url = process.env.VERCEL_URL ?? "localhost:3000"

const { getRecentArticlesWithMetadata } = HygraphClient()
const { articles } = await getRecentArticlesWithMetadata({ locales: [locale] })

const feedOptions = {
title: "Blog posts | RSS Feed",
description: "Welcome to this blog posts!",
id: site_url,
link: site_url,
language: locale,
image: `${site_url}/logo.png`,
favicon: `${site_url}/favicon.ico`,
copyright: `All rights reserved ${new Date().getFullYear()}`,
generator: "Feed for Node.js",
feedLinks: {
rss2: `${site_url}/rss.xml`,
},
}

const feed = new Feed(feedOptions)

articles.forEach((article) => {
const date = article?.updatedAt ? new Date(article?.updatedAt) : new Date()
feed.addItem({
title: article?.title,
id: `${site_url}/blog/${article?.slug}`,
link: `${site_url}/blog/${article?.slug}`,
description: "test",
copyright: `All rights reserved ${new Date().getFullYear()}`,
date: date,
author: [{ name: article?.author?.name }],
image: article?.coverImage?.url,
})
})

fs.writeFile(`./public/${locale}.xml`, feed.rss2(), (err) => {
if (err) return console.log(err)
})
}
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
"jsx": "preserve",
"incremental": true,
"baseUrl": "src",
"paths": {
"@components/*": ["./components/*"],
"@ui/*": ["./components/common/ui/*"],
"@pages/*": ["./pages/*"],
"@hooks/*": ["./hooks/*"],
"@api/*": ["./api/*"],
"@utils/*": ["./utils/*"]
},
"plugins": [
{
"name": "next"
Expand Down
Loading

0 comments on commit eb9a0b2

Please sign in to comment.