mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
85 lines
2.3 KiB
TypeScript
85 lines
2.3 KiB
TypeScript
import RSS from 'rss'
|
|
import type { GetServerSideProps } from 'next'
|
|
import { getBlockTitle, getPageProperty } from 'notion-utils'
|
|
|
|
import * as config from 'lib/config'
|
|
import { getSiteMap } from 'lib/get-site-map'
|
|
import { getCanonicalPageUrl } from 'lib/map-page-url'
|
|
import { getSocialImageUrl } from 'lib/get-social-image-url'
|
|
|
|
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
|
if (req.method !== 'GET') {
|
|
res.statusCode = 405
|
|
res.setHeader('Content-Type', 'application/json')
|
|
res.write(JSON.stringify({ error: 'method not allowed' }))
|
|
res.end()
|
|
return { props: {} }
|
|
}
|
|
|
|
const siteMap = await getSiteMap()
|
|
const ttlMinutes = 60
|
|
const ttlSeconds = ttlMinutes * 60
|
|
|
|
const feed = new RSS({
|
|
title: config.name,
|
|
site_url: config.host,
|
|
feed_url: `${config.host}/feed.xml`,
|
|
ttl: ttlMinutes
|
|
})
|
|
|
|
for (const pagePath of Object.keys(siteMap.canonicalPageMap)) {
|
|
const pageId = siteMap.canonicalPageMap[pagePath]
|
|
const recordMap = siteMap.pageMap[pageId]
|
|
if (!recordMap) continue
|
|
|
|
const keys = Object.keys(recordMap?.block || {})
|
|
const block = recordMap?.block?.[keys[0]]?.value
|
|
if (!block) continue
|
|
|
|
const title = getBlockTitle(block, recordMap) || config.name
|
|
const description =
|
|
getPageProperty<string>('Description', block, recordMap) ||
|
|
config.description
|
|
const url = getCanonicalPageUrl(config.site, recordMap)(pageId)
|
|
const lastUpdatedTime = getPageProperty<number>(
|
|
'Last Updated',
|
|
block,
|
|
recordMap
|
|
)
|
|
const publishedTime = getPageProperty<number>('Published', block, recordMap)
|
|
const date = lastUpdatedTime
|
|
? new Date(lastUpdatedTime)
|
|
: publishedTime
|
|
? new Date(publishedTime)
|
|
: undefined
|
|
const socialImageUrl = getSocialImageUrl(pageId)
|
|
|
|
feed.item({
|
|
title,
|
|
url,
|
|
date,
|
|
description,
|
|
enclosure: socialImageUrl
|
|
? {
|
|
url: socialImageUrl,
|
|
type: 'image/jpeg'
|
|
}
|
|
: undefined
|
|
})
|
|
}
|
|
|
|
const feedText = feed.xml({ indent: true })
|
|
|
|
res.setHeader(
|
|
'Cache-Control',
|
|
`public, max-age=${ttlSeconds}, stale-while-revalidate=${ttlSeconds}`
|
|
)
|
|
res.setHeader('Content-Type', 'text/xml')
|
|
res.write(feedText)
|
|
res.end()
|
|
|
|
return { props: {} }
|
|
}
|
|
|
|
export default () => null
|