前端 SSG:别让你的网站加载速度慢得像蜗牛

前端 SSG:别让你的网站加载速度慢得像蜗牛

毒舌时刻

这网站加载速度慢得能让我泡杯咖啡回来还没好。

各位前端同行,咱们今天聊聊前端 SSG(静态站点生成)。别告诉我你还在使用纯客户端渲染,那感觉就像在没有预加载的情况下开车——能开,但起步慢得要命。

为什么你需要 SSG

最近看到一个项目,每次加载都要重新获取数据,用户体验差。我就想问:你是在做网站还是在做实时应用?

反面教材

// 反面教材:纯客户端渲染 // App.jsx import React, { useState, useEffect } from 'react'; function App() { const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { async function fetchPosts() { setLoading(true); try { const response = await fetch('https://api.example.com/posts'); const data = await response.json(); setPosts(data); } catch (error) { console.error('Error fetching posts:', error); } finally { setLoading(false); } } fetchPosts(); }, []); return ( <div> <h1>我的博客</h1> {loading ? <div>加载中...</div> : ( <div> {posts.map(post => ( <div key={post.id}> <h2>{post.title}</h2> <p>{post.content}</p> </div> ))} </div> )} </div> ); } export default App; 

毒舌点评:这代码,就像在每次启动时都要重新启动发动机,慢得要命。

正确姿势

1. Next.js SSG

// 正确姿势:Next.js SSG // 1. 安装依赖 // npx create-next-app@latest // 2. 页面组件 // pages/index.js import React from 'react'; export async function getStaticProps() { // 构建时获取数据 const res = await fetch('https://api.example.com/posts'); const posts = await res.json(); return { props: { posts }, revalidate: 60 // 每 60 秒重新生成 }; } export default function Home({ posts }) { return ( <div> <h1>我的博客</h1> <div> {posts.map(post => ( <div key={post.id}> <h2>{post.title}</h2> <p>{post.content}</p> </div> ))} </div> </div> ); } // 3. 动态路由 // pages/posts/[id].js import React from 'react'; export async function getStaticPaths() { // 构建时生成所有可能的路径 const res = await fetch('https://api.example.com/posts'); const posts = await res.json(); const paths = posts.map(post => ({ params: { id: post.id.toString() } })); return { paths, fallback: false }; } export async function getStaticProps({ params }) { // 构建时获取数据 const res = await fetch(`https://api.example.com/posts/${params.id}`); const post = await res.json(); return { props: { post } }; } export default function Post({ post }) { return ( <div> <h1>{post.title}</h1> <p>{post.content}</p> </div> ); } 

2. Astro SSG

// 正确姿势:Astro SSG // 1. 安装依赖 // npm create astro@latest // 2. 页面组件 // src/pages/index.astro --- // 构建时获取数据 const res = await fetch('https://api.example.com/posts'); const posts = await res.json(); --- <html> <head> <title>我的博客</title> </head> <body> <h1>我的博客</h1> <div> {posts.map(post => ( <div key={post.id}> <h2>{post.title}</h2> <p>{post.content}</p> </div> ))} </div> </body> </html> // 3. 动态路由 // src/pages/posts/[id].astro --- // 构建时获取数据 const { id } = Astro.params; const res = await fetch(`https://api.example.com/posts/${id}`); const post = await res.json(); --- <html> <head> <title>{post.title}</title> </head> <body> <h1>{post.title}</h1> <p>{post.content}</p> </body> </html> 

3. Gatsby SSG

// 正确姿势:Gatsby SSG // 1. 安装依赖 // npm install -g gatsby-cli // gatsby new my-site // 2. 配置数据源 // gatsby-config.js module.exports = { plugins: [ { resolve: 'gatsby-source-rest-api', options: { endpoints: ['https://api.example.com/posts'] } } ] }; // 3. 页面组件 // src/pages/index.js import React from 'react'; import { graphql } from 'gatsby'; export const query = graphql` query { allRestApiPosts { edges { node { id title content } } } } `; export default function Home({ data }) { const posts = data.allRestApiPosts.edges.map(edge => edge.node); return ( <div> <h1>我的博客</h1> <div> {posts.map(post => ( <div key={post.id}> <h2>{post.title}</h2> <p>{post.content}</p> </div> ))} </div> </div> ); } // 4. 动态页面 // src/templates/post.js import React from 'react'; import { graphql } from 'gatsby'; export const query = graphql` query($id: String!) { restApiPosts(id: { eq: $id }) { title content } } `; export default function Post({ data }) { const post = data.restApiPosts; return ( <div> <h1>{post.title}</h1> <p>{post.content}</p> </div> ); } // 5. 创建动态页面 // gatsby-node.js exports.createPages = async ({ graphql, actions }) => { const { createPage } = actions; const result = await graphql(` query { allRestApiPosts { edges { node { id } } } } `); result.data.allRestApiPosts.edges.forEach(edge => { createPage({ path: `/posts/${edge.node.id}`, component: path.resolve('./src/templates/post.js'), context: { id: edge.node.id } }); }); }; 

毒舌点评:这才叫前端 SSG,构建时生成静态页面,加载速度快,用户体验好,再也不用担心页面加载慢的问题了。

Read more

【AI】为什么 OpenClaw 值得折腾?安装体验与架构原理深度解析

【AI】为什么 OpenClaw 值得折腾?安装体验与架构原理深度解析

👨‍💻程序员三明治:个人主页 🔥 个人专栏: 《设计模式精解》《重学数据结构》 🤞先做到 再看见! 目录 * 一、OpenClaw 到底是什么 * 二、OpenClaw快速安装与卸载 * 进入官网:[https://openclaw.ai/](https://openclaw.ai/) * 下滑找到Quick Start,运行下面的命令 * 配置token、网关服务 * 选择模型 * 选择要接入的IM软件 * 选择搜索供应商 * 配置skill * 配置其他的API-KEY * 重启网关服务,并选择龙虾打开方式 * 龙虾启动! * 如何卸载? * 三、OpenClaw 的原理 * 1. 四层架构(Gateway-Node-Channel-Agent): * 2. 记忆系统 * 四、OpenClaw的创新点 * 1. 它把“入口”从网页改成了消息通道 * 2. 它把“

2026年AI Agent实战:从玩具到生产力的落地手册(附源码)

2026年AI Agent实战:从玩具到生产力的落地手册(附源码)

欢迎文末添加好友交流,共同进步! “ 俺はモンキー・D・ルフィ。海贼王になる男だ!” * 前言 * 目录 * 一、AI Agent 的核心架构 * 1.1 什么是AI Agent? * 1.2 2026年Agent技术栈全景 * 二、从零搭建生产级Agent框架 * 2.1 项目结构设计 * 2.2 核心代码:Agent基类 * 2.3 记忆管理系统 * 三、三大核心技术实现 * 3.1 ReAct框架:推理+行动协同 * 3.2 工具调用系统 * 3.3 任务规划器 * 四、实战案例:智能客服Agent * 4.1 场景分析

AI工具链:MLflow实验跟踪

AI工具链:MLflow实验跟踪

AI工具链:MLflow实验跟踪 📝 本章学习目标:本章聚焦职业发展,帮助读者规划AI学习与职业路径。通过本章学习,你将全面掌握"AI工具链:MLflow实验跟踪"这一核心主题。 一、引言:为什么这个话题如此重要 在人工智能快速发展的今天,AI工具链:MLflow实验跟踪已经成为每个AI从业者必须掌握的核心技能。Python作为AI开发的主流语言,其丰富的生态系统和简洁的语法使其成为机器学习和深度学习的首选工具。 1.1 背景与意义 💡 核心认知:Python在AI领域的统治地位并非偶然。其简洁的语法、丰富的库生态、活跃的社区支持,使其成为AI开发的不二之选。掌握Python AI技术栈,是进入AI行业的必经之路。 从NumPy的高效数组运算,到TensorFlow和PyTorch的深度学习框架,Python已经构建了完整的AI开发生态。据统计,超过90%的AI项目使用Python作为主要开发语言,AI岗位的招聘要求中Python几乎是标配。 1.2 本章结构概览 为了帮助读者系统性地掌握本章内容,我将从以下几个维度展开: 📊 概念解析 → 原理推导 → 代码

AI 编程新王 Codex 全面上手指南

AI 编程新王 Codex 全面上手指南 一篇文章带你精通 Codex 四大环境 + 免费使用方法 💡 前言:AI 编程的新时代 AI 编程的竞争正进入“第二轮洗牌期”。 过去几个月,Claude Code 一度成为开发者的宠儿,但频繁的限速、封号、降智问题让不少人头疼。 如今,OpenAI 推出的 Codex 迅速崛起,凭借强大的编程能力和超高性价比,成为“AI 编程新王”。 Codex 是什么? 它是基于 GPT-5 模型打造的专用编程环境,支持命令行、VS Code 插件、SDK 集成、云端操作等多种运行模式。 不论你是写脚本、做项目、还是维护仓库,Codex 都能像“AI 结对程序员”一样协助你高效开发。