Skip to Content
HelloWorld

Create: 2023-03-23

Update: 2024-01-04
迁移至 Nextra 3

Update: 2025-03-03
升级至 Nextra 4

迁移至 Nextra 3

Update:2024-02-04 添加 sitemap
更新部分配置以迁移到 nextra3

git clone https://github.com/shuding/nextra-docs-template/

清除相关配置,删除 components , next-env.d.ts , tsconfig.json , pnpm-lock.yaml
仅保留 next.config.js , package.json , theme.config.tsx

添加 sitemap 使谷歌索引正常运行

npm install next-sitemap

配置 sitemap

next-sitemap.config.js
/** @type {import('next-sitemap').IConfig} */ module.exports = { siteUrl: "https://aifaistrue.tk", generateRobotsTxt: true, autoLastmod: false, //静态生成lastmod会同一时间遂Pass exclude: ["*/_meta"], //排除mate文件 };

配置 package.json

package.json
{ "name": "nextra-docs", "version": "0.0.1", "description": "Nextra docs", "scripts": { "dev": "next dev -p 8081", "build": "next build && next-sitemap --config next-sitemap.config.js", "start": "next start" }, "dependencies": { "next": "latest", "next-sitemap": "latest", "nextra": "alpha", "nextra-theme-docs": "alpha", "react": "latest", "react-dom": "latest" } }
npm update

修改主题配置

theme.config.jsx
import { useRouter } from "next/router"; import { useConfig } from "nextra-theme-docs"; export default { logo: <span>Life.Exec(Me);</span>, docsRepositoryBase: "https://aifaistrue.tk/docs", feedback: { content: null }, //删除右侧反馈 editLink: { content: null }, //删除右侧编辑此页面 sidebar: { toggleButton: true }, // 允许收缩目录 toc: { title: "此页面", backToTop: true }, //侧边栏本地化,增加一键回顶 search: { placeholder: "搜索" }, //搜索背景本地化 //配置图标及头标签 head: function useHead() { const config = useConfig(); const { route } = useRouter(); const title = config.title + (route === "/" ? "" : " - Life.Exec(Me);"); return ( <> <title>{title}</title> <meta property="og:title" content={title} /> <link rel="icon" href="/favicon.ico" /> </> ); }, footer: { //页尾版权信息 content: ( <span> <a href="https://nextra.site/" target="_blank"> Power By Nextra </a> <br /> 2023 - {new Date().getFullYear()} ©{" "} <a href="https://aifaistrue.tk" target="_blank"> aifaistrue.tk </a> </span> ), }, };

修改 next.config.js 到 next.config.mjs

next.config.mjs
import nextra from "nextra"; /** * @type {import('next').NextConfig} */ const nextConfig = { distDir: "build", //导出目录 output: "export", //配置静态导出 trailingSlash: true, //配置输出时导出路由 html images: { unoptimized: true, //关闭图像优化 }, }; const withNextra = nextra({ theme: "nextra-theme-docs", themeConfig: "./theme.config.jsx", //latex: true, defaultShowCopyCode: true, //默认显示复制按钮 }); export default withNextra(nextConfig);

修改 pages/_meta.jsx , 配置 map

pages/_meta.jsx
export default { contact: { title: "Contact ↗", type: "page", href: "https://aifaistrue.tk", newWindow: true, }, index: { title: "Index", theme: { breadcrumb: false, toc: false, layout: "full", }, display: "hidden", }, };

创建 styles/global.css

styles/global.css
/* 修改滑动条的样式 */ ::-webkit-scrollbar { width: 12px; height: 12px; } ::-webkit-scrollbar-thumb { background-color: #dcdcdc; border-radius: 10px; } ::-webkit-scrollbar-track { background-color: #f5f5f5; }

创建 pages/_app.jsx 导入 css

pages/_app.jsx
import "../styles/global.css"; export default function App({ Component, pageProps }) { return <Component {...pageProps} />; }

markdown 文档可直接迁移至 pages/下

更新至 Nextra 4

创建 app 文件夹

不使用4.4版本更新的复制页面功能

app/[[...mdxPath]]/page.jsx
import { generateStaticParamsFor, importPage } from "nextra/pages"; import { useMDXComponents as getMDXComponents } from "../../mdx-components"; export const generateStaticParams = generateStaticParamsFor("mdxPath"); export async function generateMetadata(props) { const params = await props.params; const { metadata } = await importPage(params.mdxPath); return metadata; } const Wrapper = getMDXComponents().wrapper; export default async function Page(props) { const params = await props.params; const { default: MDXContent, toc, metadata, } = await importPage(params.mdxPath); return ( <Wrapper toc={toc} metadata={metadata}> <MDXContent {...props} params={params} /> </Wrapper> ); }
app/layout.jsx
import { Footer, Layout, Navbar, LastUpdated } from "nextra-theme-docs"; import { Head, Search } from "nextra/components"; import { getPageMap } from "nextra/page-map"; import "nextra-theme-docs/style.css"; export const metadata = { title: { template: "%s - Life.Exec(Me);", }, }; const navbar = <Navbar logo={<span>Life.Exec(Me);</span>} />; const footer = ( <Footer> <span> <a href="https://nextra.site/" target="_blank"> Power By Nextra </a> <br /> 2023 - {new Date().getFullYear()} ©{" "} <a href="https://aifaistrue.tk" target="_blank"> aifaistrue.tk </a> </span> </Footer> ); export default async function RootLayout({ children }) { return ( <html lang="zh-CN" dir="/" suppressHydrationWarning> <Head> {/* Your additional tags should be passed as `children` of `<Head>` element */} </Head> <body> <Layout navbar={navbar} pageMap={await getPageMap()} docsRepositoryBase="https://aifaistrue.tk/docs" footer={footer} editLink={null} feedback={{ content: "" }} search={<Search placeholder={"搜索"} />} toc={{ title: "此页面", backToTop: "回到顶部" }} // ... Your additional layout options > {children} </Layout> </body> </html> ); }

将原本的 pages 文件夹重命名为 content
删除 theme.config.jsx

mdx-components.js
import { useMDXComponents as getThemeComponents } from 'nextra-theme-docs' // nextra-theme-blog or your custom theme // Get the default MDX components const themeComponents = getThemeComponents() // Merge components export function useMDXComponents(components) { return { ...themeComponents, ...components } }
next.config.mjs
import nextra from 'nextra' const withNextra = nextra({ // ... Other Nextra config options defaultShowCopyCode: true }) // You can include other Next.js configuration options here, in addition to Nextra settings: export default withNextra({ distDir: "build", //导出目录 output: "export", //配置静态导出 trailingSlash: true, //配置输出时导出路由 html images: { unoptimized: true, //关闭图像优化 }, })
package.json
{ "name": "nextra-docs", "version": "0.0.1", "description": "Nextra docs", "scripts": { "dev": "next dev -p 8081", "build": "next build && next-sitemap --config next-sitemap.config.js && pagefind --site .next/server/app --output-path build/_pagefind", "start": "next start" }, "dependencies": { "next": "latest", "next-sitemap": "latest", "nextra": "latest", "nextra-theme-docs": "latest", "react": "latest", "react-dom": "latest" }, "devDependencies": { "pagefind": "latest" } }

其余的保持不变

持续更新中