Skip to content

feat: add ISR settings #109

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 13 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ yarn-error.log*

public/feed.xml
public/sitemap.xml
public/sitemap-*.xml
public/robots.txt
116 changes: 58 additions & 58 deletions lib/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Promise from 'bluebird'
import fg from 'fast-glob'
// import fg from 'fast-glob'
import fs from 'fs-extra'
import path from 'path'

Expand All @@ -16,11 +16,11 @@ import {
sortPostByDate,
} from './noteHelper'

const cachedDir = path.join(process.cwd(), './.next/cache/posts')
const notesCachedDir = path.join(process.cwd(), './.next/cache/notes')
// const cachedDir = path.join(process.cwd(), './.next/cache/posts')
// const notesCachedDir = path.join(process.cwd(), './.next/cache/notes')

fs.ensureDirSync(cachedDir)
fs.ensureDirSync(notesCachedDir)
// fs.ensureDirSync(cachedDir)
// fs.ensureDirSync(notesCachedDir)

const getHashedKey = (
year: string,
Expand All @@ -36,12 +36,12 @@ export const fetchPostData = async (noteId: string) => {
return
}

const encodedId = encodeURIComponent(noteId)
const notePath = path.join(notesCachedDir, `${encodedId}.md`)
// const encodedId = encodeURIComponent(noteId)
// const notePath = path.join(notesCachedDir, `${encodedId}.md`)

if (fs.existsSync(notePath)) {
return fs.readFileSync(notePath, 'utf8')
}
// if (fs.existsSync(notePath)) {
// return fs.readFileSync(notePath, 'utf8')
// }

const fullContent = await fetch(
`${config.hackmdBaseUrl}/${noteId}/download`,
Expand All @@ -50,22 +50,22 @@ export const fetchPostData = async (noteId: string) => {
}
).then((r) => r.text())

fs.writeFileSync(notePath, fullContent, 'utf8')
// fs.writeFileSync(notePath, fullContent, 'utf8')

return fullContent
}

export const getAllPostsWithSlug = async () => {
// cached json results
const currentEntries = await fg(`${cachedDir}/*.json`)
if (currentEntries.length > 0) {
return currentEntries
.map((entry) => {
return JSON.parse(fs.readFileSync(entry, 'utf-8'))
})
.filter(filterNotDraft)
.sort(sortPostByDate)
}
// const currentEntries = await fg(`${cachedDir}/*.json`)
// if (currentEntries.length > 0) {
// return currentEntries
// .map((entry) => {
// return JSON.parse(fs.readFileSync(entry, 'utf-8'))
// })
// .filter(filterNotDraft)
// .sort(sortPostByDate)
// }

const data = await fetch(
`${config.hackmdBaseUrl}/api/@${process.env.HACKMD_PROFILE}/overview`,
Expand Down Expand Up @@ -100,31 +100,31 @@ export const getAllPostsWithSlug = async () => {
.filter(Boolean)
.filter(filterNotDraft)

posts.forEach((post: Post) => {
const { date } = post
const filename = getHashedKey(date.year, date.month, date.day, post.slug)

// console.log(post.date)

fs.writeFileSync(
path.join(cachedDir, `${filename}.json`),
JSON.stringify(
{
id: post.note!.id,
meta: post.meta,
title: post.note!.title,
content: post?.content,
date: post.date,
slug: post.slug,
tags: post.tags,
publishedAt: post.publishedAt,
},
null,
2
),
'utf-8'
)
})
// posts.forEach((post: Post) => {
// const { date } = post
// const filename = getHashedKey(date.year, date.month, date.day, post.slug)

// console.log(post.date)

// fs.writeFileSync(
// path.join(cachedDir, `${filename}.json`),
// JSON.stringify(
// {
// id: post.note!.id,
// meta: post.meta,
// title: post.note!.title,
// content: post?.content,
// date: post.date,
// slug: post.slug,
// tags: post.tags,
// publishedAt: post.publishedAt,
// },
// null,
// 2
// ),
// 'utf-8'
// )
// })

return posts.sort(sortPostByDate)
}
Expand All @@ -144,22 +144,22 @@ export const getPostData = async (params: {
day: string
slug: string
}) => {
const filename = getHashedKey(
params.year,
params.month,
params.day,
params.slug
)
// const filename = getHashedKey(
// params.year,
// params.month,
// params.day,
// params.slug
// )

// const posts = await getAllPostsWithSlug()
const posts = await getAllPostsWithSlug()

// const post = posts.find((post) => {
// return post.slug === params.slug
// })
const post = posts.find((post: Post) => {
return post.slug === params.slug
})

const post = JSON.parse(
fs.readFileSync(path.join(cachedDir, `${filename}.json`), 'utf-8')
)
// const post = JSON.parse(
// fs.readFileSync(path.join(cachedDir, `${filename}.json`), 'utf-8')
// )

return {
id: post.id,
Expand Down
6 changes: 6 additions & 0 deletions next-sitemap.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: `https://${process.env.DOMAIN}`,
changefreq: 'monthly',
priority: 1.0,
generateRobotsTxt: true,
exclude: ['/server-sitemap.xml'],
robotsTxtOptions: {
additionalSitemaps: [`https://${process.env.DOMAIN}/server-sitemap.xml`],
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "rimraf .next/cache/posts && rimraf .next/cache/notes && next build",
"build": "rimraf .next/cache/posts && rimraf .next/cache/notes && next build && next-sitemap",
"postbuild": "next-sitemap",
"start": "next start",
"lint": "next lint"
Expand Down
1 change: 1 addition & 0 deletions pages/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ export async function getStaticProps() {
props: {
content,
},
revalidate: 300,
}
}
1 change: 1 addition & 0 deletions pages/blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ export async function getStaticProps() {
props: {
posts: posts.map((post: PostsProps) => omit(post, ['content'])),
},
revalidate: 300,
}
}
3 changes: 2 additions & 1 deletion pages/blog/[year]/[month]/[day]/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export async function getStaticProps({ params }: PostProps) {
noteId: id,
meta,
},
revalidate: 300,
}
}

Expand All @@ -232,6 +233,6 @@ export async function getStaticPaths() {

return {
paths,
fallback: false,
fallback: 'blocking',
}
}
1 change: 1 addition & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ export async function getStaticProps() {
props: {
posts: posts.map((post: PostsProps) => omit(post, ['content'])),
},
revalidate: 300,
}
}
1 change: 1 addition & 0 deletions pages/oldProjects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ export async function getStaticProps() {
props: {
projects: loadProjects(),
},
revalidate: 300,
}
}
1 change: 1 addition & 0 deletions pages/projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export async function getStaticProps() {
props: {
content,
},
revalidate: 300,
}
}
24 changes: 24 additions & 0 deletions pages/server-sitemap.xml/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { GetServerSideProps } from 'next'
import { getServerSideSitemapLegacy } from 'next-sitemap'

import { formatPostsAsParams, getAllPostsWithSlug } from '@/lib/post'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
const blogPaths = formatPostsAsParams(
await getAllPostsWithSlug().catch((err) => err.message)
)

const blogPathsResult = blogPaths.map((item) => {
const { year, month, day, slug } = item.params
return {
loc: `https://${process.env.DOMAIN}/blog/${year}/${month}/${day}/${slug}`,
lastmod: new Date().toISOString(),
priority: 0.7,
}
})

return getServerSideSitemapLegacy(ctx, blogPathsResult)
}

// Default export to prevent next.js errors
export default function Sitemap() {}