Skip to content

Commit aec197d

Browse files
committed
rss feedを自動生成するように変更
1 parent 2f0a94d commit aec197d

File tree

10 files changed

+92
-13
lines changed

10 files changed

+92
-13
lines changed

lib/$path.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export const pagesPath = {
1515
}
1616
})
1717
},
18+
feed_xml: {
19+
$url: (url?: { hash?: string }) => ({ pathname: '/feed.xml' as const, hash: url?.hash })
20+
},
1821
page: {
1922
_pageNumber: (pageNumber: string | number) => ({
2023
$url: (url?: { hash?: string }) => ({ pathname: '/page/[pageNumber]' as const, query: { pageNumber }, hash: url?.hash })

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"react-dom": "^17.0.1",
3939
"react-scroll": "^1.8.2",
4040
"react-use": "^17.2.4",
41+
"rss": "^1.2.2",
4142
"swr": "^0.5.6"
4243
},
4344
"devDependencies": {
@@ -50,6 +51,7 @@
5051
"@types/node": "^14.14.37",
5152
"@types/react": "^17.0.3",
5253
"@types/react-scroll": "^1.8.2",
54+
"@types/rss": "^0.0.28",
5355
"@typescript-eslint/eslint-plugin": "^4.20.0",
5456
"@typescript-eslint/parser": "^4.20.0",
5557
"babel-jest": "^26.6.3",

pages/feed.xml.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { GetServerSideProps } from 'next'
2+
3+
import generateFeedXml from '~/scripts/generate-rss-feed'
4+
5+
export const getServerSideProps: GetServerSideProps = async ({ res }) => {
6+
const feed = await generateFeedXml()
7+
8+
res.statusCode = 200
9+
res.setHeader('Cache-Control', 's-maxage=86400, stale-while-revalidate') // 24時間のキャッシュ
10+
res.setHeader('Content-Type', 'text/xml')
11+
res.end(feed)
12+
13+
return {
14+
props: {},
15+
}
16+
}
17+
18+
const FeedPage = () => null
19+
20+
export default FeedPage

scripts/generate-rss-feed.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import RSS from 'rss'
2+
3+
import { getContents } from '~/src/utils/getContents'
4+
import { description, returnTitle, SITE_URL } from '~/src/utils/meta'
5+
6+
const generateFeedXml = async () => {
7+
const feed = new RSS({
8+
title: returnTitle(),
9+
description,
10+
feed_url: `${SITE_URL}/feed.xml`,
11+
site_url: SITE_URL,
12+
language: 'ja',
13+
})
14+
15+
const { contents } = await getContents()
16+
contents.forEach((content) => {
17+
feed.item({
18+
title: returnTitle(content.title),
19+
description: content.description,
20+
date: content.publishedAt ?? content.createdAt,
21+
url: `${SITE_URL}/${content.id}`,
22+
})
23+
})
24+
25+
return feed.xml()
26+
}
27+
28+
export default generateFeedXml

scripts/generate-sitemap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const generateSitemap = async () => {
1515
'!pages/_*.tsx',
1616
'!pages/**/[*.tsx',
1717
'!pages/sitemap.xml.tsx',
18+
'!pages/feed.xml.tsx',
1819
'!pages/404.tsx',
1920
'!pages/api',
2021
])

src/components/Layout/Layout.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react'
44
import { Banner as ApiBanner } from '~/src/types/microCMS/api/Banner'
55
import { Blog } from '~/src/types/microCMS/api/Blog'
66
import { Category } from '~/src/types/microCMS/api/Category'
7-
import { DESCRIPTION, SITE_URL, OG_DESCRIPTION, OG_IMAGE, OG_TYPE } from '~/src/utils/meta'
7+
import { DESCRIPTION, SITE_URL, OG_DESCRIPTION, OG_IMAGE, OG_TYPE, description } from '~/src/utils/meta'
88

99
import { Banner } from '../Banner'
1010
import { Categories } from '../Categories'
@@ -27,8 +27,6 @@ export type { ContainerProps as LayoutProps }
2727

2828
type Props = ContainerProps
2929

30-
const description = ''
31-
3230
const Component: React.FC<Props> = ({ banner, categories, children, popularArticles, latestArticles }) => (
3331
<>
3432
<Head>

src/components/Share/Share.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ const Container: React.VFC<ContainerProps> = ({ id, title }) => {
4949
src: `${process.env.NEXT_PUBLIC_BASE_PATH}/images/icon_hatena.svg`,
5050
alt: 'はてなブックマーク',
5151
},
52-
// {
53-
// href: `${SITE_URL}/feed.xml`,
54-
// src: `${process.env.NEXT_PUBLIC_BASE_PATH}/images/icon_feed.svg`,
55-
// alt: 'フィード',
56-
// },
52+
{
53+
href: `${SITE_URL}/feed.xml`,
54+
src: `${process.env.NEXT_PUBLIC_BASE_PATH}/images/icon_feed.svg`,
55+
alt: 'フィード',
56+
},
5757
],
5858
[id, title]
5959
)

src/utils/meta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if (process.env.NEXT_PUBLIC_SITE_URL === undefined) {
1111
export const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL
1212

1313
const title = 'microCMSブログ'
14+
export const description = ''
1415

1516
export const returnTitle = (pageTitle?: string) => {
1617
if (pageTitle !== undefined) {

src/utils/microCMSHeaders.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { MicroCMSReqHeaders } from '../types/microCMS/Headers'
22

3-
if (process.env.API_KEY === undefined) {
4-
throw Error('envファイルにAPI_KEYを設定してください。')
5-
}
6-
73
export const headers: MicroCMSReqHeaders = {
8-
'X-API-KEY': process.env.API_KEY,
4+
'X-API-KEY': process.env.API_KEY ?? '',
95
}

yarn.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,11 @@
10561056
"@types/scheduler" "*"
10571057
csstype "^3.0.2"
10581058

1059+
"@types/rss@^0.0.28":
1060+
version "0.0.28"
1061+
resolved "https://registry.yarnpkg.com/@types/rss/-/rss-0.0.28.tgz#2780ffec484ca3c69cce63e314efd4ca4454f6c9"
1062+
integrity sha512-Kymd0BI1tBOSwOwCN5Y8dtlJegTIF+izS8tJETiivJ7qAJGHhnuZjiunXWqBkgv6dg0ZZWyf0kEfMgkr4YWJzg==
1063+
10591064
"@types/scheduler@*":
10601065
version "0.16.1"
10611066
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.1.tgz#18845205e86ff0038517aab7a18a62a6b9f71275"
@@ -6051,6 +6056,18 @@ [email protected], mime-db@^1.28.0:
60516056
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.46.0.tgz#6267748a7f799594de3cbc8cde91def349661cee"
60526057
integrity sha512-svXaP8UQRZ5K7or+ZmfNhg2xX3yKDMUzqadsSqi4NCH/KomcH75MAMYAGVlvXn4+b/xOPhS3I2uHKRUzvjY7BQ==
60536058

6059+
mime-db@~1.25.0:
6060+
version "1.25.0"
6061+
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.25.0.tgz#c18dbd7c73a5dbf6f44a024dc0d165a1e7b1c392"
6062+
integrity sha1-wY29fHOl2/b0SgJNwNFloeexw5I=
6063+
6064+
6065+
version "2.1.13"
6066+
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88"
6067+
integrity sha1-4HqqnGxrmnyjASxpADrSWjnpKog=
6068+
dependencies:
6069+
mime-db "~1.25.0"
6070+
60546071
mime-types@^2.1.12, mime-types@~2.1.19:
60556072
version "2.1.29"
60566073
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.29.tgz#1d4ab77da64b91f5f72489df29236563754bb1b2"
@@ -7791,6 +7808,14 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
77917808
hash-base "^3.0.0"
77927809
inherits "^2.0.1"
77937810

7811+
rss@^1.2.2:
7812+
version "1.2.2"
7813+
resolved "https://registry.yarnpkg.com/rss/-/rss-1.2.2.tgz#50a1698876138133a74f9a05d2bdc8db8d27a921"
7814+
integrity sha1-UKFpiHYTgTOnT5oF0r3I240nqSE=
7815+
dependencies:
7816+
mime-types "2.1.13"
7817+
xml "1.0.1"
7818+
77947819
rsvp@^4.8.4:
77957820
version "4.8.5"
77967821
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734"
@@ -9485,6 +9510,11 @@ xml2js@^0.4.5:
94859510
sax ">=0.6.0"
94869511
xmlbuilder "~11.0.0"
94879512

9513+
9514+
version "1.0.1"
9515+
resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5"
9516+
integrity sha1-eLpyAgApxbyHuKgaPPzXS0ovweU=
9517+
94889518
xmlbuilder@~11.0.0:
94899519
version "11.0.1"
94909520
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"

0 commit comments

Comments
 (0)