Skip to content

updating #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Deploy

on:
#schedule:
# - cron: '5 23 * * 0' # 08:05(Mon) JST
schedule:
- cron: '5 23 * * 0' # 08:05(Mon) JST
workflow_dispatch:

jobs:
Expand Down
4 changes: 4 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Allow: /

Sitemap: https://https://astro-notion-blog-454.pages.dev/sitemap.xml
1 change: 1 addition & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if (database.Icon && database.Icon.Type === 'file') {
<meta name="twitter:description" content={siteDescription} />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content={ogImage || siteOGImage} />
<meta name="google-site-verification" content="TcZnkLcogaFHubmWllXmhijoPPo1qWw4hkcHqU809XQ" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
Expand Down
26 changes: 26 additions & 0 deletions src/lib/generate-sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type SitemapItem = {
loc: string
lastmod: Date
}

function formatDate(date: Date): string {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}

export function generateSitemap(items: SitemapItem[]) {
const itemsStr = items
.map(
({ loc, lastmod }) =>
`<url><loc>${loc}</loc><lastmod>${formatDate(lastmod)}</lastmod></url>`
)
.join('')

return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${itemsStr}
</urlset>
`
}
24 changes: 24 additions & 0 deletions src/pages/sitemap.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { generateSitemap } from '../lib/generate-sitemap'
import { getAllPosts } from '../lib/notion/client'
import { getPostLink } from '../lib/blog-helpers'

export async function GET() {
const [posts] = await Promise.all([getAllPosts()])

const sitemapItems = posts.map((post) => {
const updateDate = post.UpdateDate ? new Date(post.UpdateDate) : null
const lastmod = updateDate || new Date(post.Date)

return {
loc: new URL(getPostLink(post.Slug), import.meta.env.SITE).toString(),
lastmod,
}
})

const sitemap = generateSitemap(sitemapItems)

const res = new Response(sitemap)
res.headers.set('Content-Type', 'text/xml')

return res
}