Skip to content

Commit

Permalink
feat(sitemap): introduce sitemap (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Feb 15, 2021
1 parent a973b5d commit 6a54b8f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ const nextConfig = {
{
destination: '/api/manifest',
source: '/manifest.webmanifest'
},
{
destination: '/api/sitemap',
source: '/sitemap.xml'
}
]
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"react-dom": "^17.0.1",
"react-focus-lock": "^2.5.0",
"react-remove-scroll": "^2.4.1",
"sharp": "^0.27.1"
"sharp": "^0.27.1",
"sitemap": "^6.3.6"
},
"description": "The source code for the Inaba Jinja.",
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
User-agent: *
Disallow:

Sitemap: https://inaba-jinja.com/sitemap.xml
54 changes: 54 additions & 0 deletions src/pages/api/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { NextApiHandler } from 'next'
import type { SitemapItemLoose } from 'sitemap'
import { EnumChangefreq, SitemapStream } from 'sitemap'

import pkg from '@/../package.json'
import getFortunes from '@/contentful/getFortunes'

const STATIC_PAGES: SitemapItemLoose[] = [
{
changefreq: EnumChangefreq.MONTHLY,
priority: 1.0,
url: '/'
},
{
changefreq: EnumChangefreq.MONTHLY,
priority: 0.5,
url: '/about'
},
{
changefreq: EnumChangefreq.MONTHLY,
priority: 0.5,
url: '/privacy'
}
]

const sitemapHandler: NextApiHandler<string> = async (_req, res) => {
const smStream = new SitemapStream({
hostname: pkg.homepage
})

for (const page of STATIC_PAGES) {
smStream.write(page)
}

const fortunes = await getFortunes().catch((): string[] => [])
for (const id of fortunes) {
smStream.write({
changefreq: EnumChangefreq.MONTHLY,
priority: 0.7,
url: `/kuji/${id}`
})
}

res.status(200)
res.setHeader('Content-Type', 'application/xml')
res.setHeader('Cache-Control', 'max-age=604800')

smStream.end()
smStream.pipe(res).on('close', () => {
res.end()
})
}

export default sitemapHandler
26 changes: 24 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,7 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256"
integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg==

"@types/node@*", "@types/node@^14.14.28":
"@types/node@*", "@types/node@^14.14.25", "@types/node@^14.14.28":
version "14.14.28"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.28.tgz#cade4b64f8438f588951a6b35843ce536853f25b"
integrity sha512-lg55ArB+ZiHHbBBttLpzD07akz0QPrZgUODNakeC09i62dnrywr9mFErHuaPlB6I7z+sEbK+IYmplahvplCj2g==
Expand Down Expand Up @@ -1860,6 +1860,13 @@
dependencies:
"@types/node" "*"

"@types/sax@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@types/sax/-/sax-1.2.1.tgz#e0248be936ece791a82db1a57f3fb5f7c87e8172"
integrity sha512-dqYdvN7Sbw8QT/0Ci5rhjE4/iCMJEM0Y9rHpCu+gGXD9Lwbz28t6HI2yegsB6BoV1sShRMU6lAmAcgRjmFy7LA==
dependencies:
"@types/node" "*"

"@types/source-list-map@*":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
Expand Down Expand Up @@ -2316,6 +2323,11 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0"
readable-stream "^2.0.6"

arg@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/arg/-/arg-5.0.0.tgz#a20e2bb5710e82950a516b3f933fee5ed478be90"
integrity sha512-4P8Zm2H+BRS+c/xX1LrHw0qKpEhdlZjLCgWy+d78T9vqa2Z2SiD2wMrYuWIAFy5IZUD7nnNXroRttz+0RzlrzQ==

argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
Expand Down Expand Up @@ -8803,7 +8815,7 @@ sane@^4.0.3:
minimist "^1.1.1"
walker "~1.0.5"

sax@~1.2.4:
sax@^1.2.4, sax@~1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
Expand Down Expand Up @@ -9054,6 +9066,16 @@ sisteransi@^1.0.5:
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
integrity sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==

sitemap@^6.3.6:
version "6.3.6"
resolved "https://registry.yarnpkg.com/sitemap/-/sitemap-6.3.6.tgz#5d0238a5b713882f77c6088bb22dea9a0ecd7356"
integrity sha512-L+gSUOJrUuXP3XHnJ0VcV0rmBImwm0aVmJb0YGoafG7YcIblNdRs9vKpeyFXlcbY2S8x6e+kMpNlty6lqdBopg==
dependencies:
"@types/node" "^14.14.25"
"@types/sax" "^1.2.1"
arg "^5.0.0"
sax "^1.2.4"

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
Expand Down

0 comments on commit 6a54b8f

Please sign in to comment.