Skip to content
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

feat:#14 静态渲染,提升性能 #15

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/themes/default/defaultHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export default function DefaultHeader({props}: { props: SiteConfig }) {
return (
<>
<Head>
<title>{props.webname} - {props.webslogen}</title>
<meta name="keywords" content={props.keywords}/>
<meta name="description" content={props.description}/>
<title>{props?.webname} - {props?.webslogen}</title>
<meta name="keywords" content={props?.keywords}/>
<meta name="description" content={props?.description}/>
</Head>
<header>
<DefaultNavbar props={props}/>
Expand Down
2 changes: 1 addition & 1 deletion components/themes/default/defaultNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function DefaultNavbar({props}: { props: SiteConfig }) {
return (
<Navbar bg="light" expand="lg">
<Container>
<Navbar.Brand href="/">{props.webname}</Navbar.Brand>
<Navbar.Brand href="/">{props?.webname}</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav"/>
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="me-auto">
Expand Down
8 changes: 4 additions & 4 deletions lib/siyuan/siYuanApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export class SiYuanApiAdaptor implements IApi {

// 适配公共属性
let commonPost = new Post()
commonPost.postid = siyuanPost.root_id
commonPost.title = siyuanPost.content
commonPost.description = html
commonPost.mt_keywords = attr.tags
commonPost.postid = siyuanPost.root_id || ""
commonPost.title = siyuanPost.content || ""
commonPost.description = html || ""
commonPost.mt_keywords = attr.tags || ""
// commonPost.dateCreated

return commonPost
Expand Down
58 changes: 41 additions & 17 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {GetServerSideProps, NextPage} from 'next'
import type {GetServerSideProps, GetStaticProps, NextPage} from 'next'
import {API} from "../lib/api";
import {API_TYPE_CONSTANTS} from "../lib/constants";
import {Post} from "../lib/common/post";
Expand All @@ -24,16 +24,48 @@ export default Home

// https://github.com/siyuan-note/siyuan/blob/master/API_zh_CN.md#%E9%89%B4%E6%9D%83
// https://github.com/vercel/next.js/blob/canary/examples/cms-wordpress/pages/index.js
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const query = context.query || {}
if (query.t instanceof Array) {
throw new Error("参数类型错误")
}
// export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
// const query = context.query || {}
// if (query.t instanceof Array) {
// throw new Error("参数类型错误")
// }
//
// let cfg: SiteConfig = new SiteConfig()
// let result: Array<Post> = []
// const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN
// const pageno = query.p
//
// const api = new API(type)
//
// // 配置
// const cfgs = await api.getUsersBlogs() || []
// if (cfgs.length > 0) {
// cfg.userBlog = cfgs[0]
// }
// // 文章
// if (pageno) {
// let num = 1
// if (typeof pageno === "string") {
// num = parseInt(pageno) || 1
// }
// result = await api.getRecentPosts(10, num - 1)
// } else {
// result = await api.getRecentPosts(10)
// }
//
// return {
// props: {
// type: type,
// layoutCfg: JSON.parse(JSON.stringify(cfg)),
// posts: JSON.parse(JSON.stringify(result))
// }
// }
// }

export const getStaticProps: GetStaticProps<Props> = async (context) => {
let cfg: SiteConfig = new SiteConfig()
let result: Array<Post> = []
const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const pageno = query.p
const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN

const api = new API(type)

Expand All @@ -43,15 +75,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
cfg.userBlog = cfgs[0]
}
// 文章
if (pageno) {
let num = 1
if (typeof pageno === "string") {
num = parseInt(pageno) || 1
}
result = await api.getRecentPosts(10, num - 1)
} else {
result = await api.getRecentPosts(10)
}
result = await api.getRecentPosts(10)

return {
props: {
Expand Down
69 changes: 57 additions & 12 deletions pages/post/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {GetServerSideProps, NextPage} from "next";
import {GetStaticProps, NextPage} from "next";
import {Post} from "../../lib/common/post";
import {API} from "../../lib/api";
import {API_TYPE_CONSTANTS} from "../../lib/constants";
import log from "../../lib/logUtil";
import styles from "../../components/themes/default/css/layout.module.css";
import SiteConfig from "../../lib/common/siteconfig";
import DefaultLayout from "../../components/themes/default/defaultLayout";
Expand All @@ -16,13 +15,13 @@ type Props = {
const PostDetail: NextPage<Props> = (props, context) => {

function createMarkup() {
return {__html: props.post.description};
return {__html: props.post?.description};
}

return (
<DefaultLayout props={props.propCfg}>
<main className={styles.main}>
<p>{props.post.mt_keywords}</p>
<p>{props.post?.mt_keywords}</p>
<div dangerouslySetInnerHTML={createMarkup()}/>
</main>
</DefaultLayout>
Expand All @@ -31,12 +30,46 @@ const PostDetail: NextPage<Props> = (props, context) => {

export default PostDetail

export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const query = context.query || {}
if (query.t instanceof Array) {
throw new Error("参数类型错误")
}
// export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
// const query = context.query || {}
// if (query.t instanceof Array) {
// throw new Error("参数类型错误")
// }
//
// const slug = context?.params?.slug
// if (!slug || typeof slug !== "string") {
// throw new Error("文章路径错误")
// }
// let smartSlug = ""
// if (slug.indexOf(".html") > -1) {
// smartSlug = slug.replace(".html", "")
// }
//
// const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN
//
// const api = new API(type)
//
// let cfg: SiteConfig = new SiteConfig()
// // 配置
// const cfgs = await api.getUsersBlogs() || []
// if (cfgs.length > 0) {
// cfg.userBlog = cfgs[0]
// }
//
// // 文章
// log.logInfo("smartSlug=>", smartSlug)
// const post = await api.getPost(smartSlug)
//
// return {
// props: {
// type: type,
// propCfg: JSON.parse(JSON.stringify(cfg)),
// post: JSON.parse(JSON.stringify(post))
// }
// }
// }

export const getStaticProps: GetStaticProps<Props> = async (context) => {
const slug = context?.params?.slug
if (!slug || typeof slug !== "string") {
throw new Error("文章路径错误")
Expand All @@ -46,8 +79,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
smartSlug = slug.replace(".html", "")
}

const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN

const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const api = new API(type)

let cfg: SiteConfig = new SiteConfig()
Expand All @@ -58,7 +90,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
}

// 文章
log.logInfo("smartSlug=>", smartSlug)
// log.logInfo("smartSlug=>", smartSlug)
const post = await api.getPost(smartSlug)

return {
Expand All @@ -68,4 +100,17 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
post: JSON.parse(JSON.stringify(post))
}
}
}

export async function getStaticPaths() {
const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const api = new API(type)
const result = await api.getRecentPosts(10)

// 静态路径
const fpath = result.map(({postid}) => `/post/${postid}.html`) || [];
return {
paths: fpath,
fallback: true,
}
}