Site: https://nextjs.org
Docs: https://nextjs.org/docs
React: https://reactjs.org/tutorial/tutorial.html
简单了解
一个 React 框架, 提供了创建 Web 应用程序的构建程序.
创建项目
npx create-next-app@latest |
运行项目
npm run dev |
组件
常用
Router
import { useRouter } from 'next/router'
Link
import Link from 'next/link';
Image
import Image from 'next/image'
Head
import Head from 'next/head';
Script
import Script from 'next/script';
日期处理
安装库
npm install date-fns |
例子
import { parseISO, format } from 'date-fns'; |
CSS
使用 CSS Modules, CSS文件名必须以
.module.css
.
安装 Tailwind CSS
-> https://tailwindcss.com/
npm install -D tailwindcss autoprefixer postcss |
添加 postcss.config.js
到项目根目录.
// postcss.config.js |
添加 tailwind.config.js
到项目根目录.
// tailwind.config.js |
组件动态导入
import dynamic from 'next/dynamic'; |
引入组件, 服务器上不需要加载, 设置ssr
为false
.
const CodeSampleModal = dynamic(() => import('../components/CodeSampleModal'), { |
包装组件
{ |
数据获取
Data Fetching: Overview | Next.js (nextjs.org)
getStaticProps
- 生成静态页面路径
- 在函数内部, 可以获取外部数据并将其作为变量传递到页面.
- 开发模式, 每个请求都会运行; 生产环境中,只在构建时运行.
- 只能在
page
文件中使用.(pages文件夹下的.js
,.jsx
,.ts
,.tsx
文件)
export default function Home(props) { ... } |
从API
获取数据
export async function getSortedPostsData() { |
从数据库获取数据
import someDatabaseSDK from 'someDatabaseSDK' |
getStaticProps
- 生成静态页面内容
export async function getStaticProps(context) { |
getServerSideProps
- 服务器端渲染数据, 每个请求都会运行
- Data Fetching: Overview | Next.js (nextjs.org)
export async function getServerSideProps(context) { |
SWR
用于数据请求的 React Hooks 库 – SWR
- 使用 SWR,组件将会不断地、自动获得最新数据流。
- UI 也会一直保持快速响应。
import useSWR from 'swr'; |
API Routes
API Routes: Introduction | Next.js (nextjs.org)
pages/api/hello.js - localhost:3000/api/hello
export default function handler(req, res) { |
req
是 http. IncomingMessage 的一个实例,加上一些预构建的中间件。res
是 http. ServerResponse 的一个实例,外加一些 helper 函数。
搜索引擎优化
谷歌爬虫工作流
Find URLs - 查找URL 从站点地图(XML), 页面链接等.
Add to Crawl Queue - 添加到爬取队列等待爬取.
HTTP Request - 发送请求URL, 根据响应状态码做不同的处理.
200 - 请求成功
301/308 - 永久重定向 308为推荐的处理(不允许更改请求方法)
// pages/about.js
export async function getStaticProps(context) {
return {
redirect: {
destination: '/',
permanent: true, // triggers 308
},
};
}302 - 临时重定向
404 - 不存在页面
指定返回 404 状态码export async function getStaticProps(context) {
return {
notFound: true, // triggers 404
};
}定制 404 页面
// pages/404.js
export default function Custom404() {
return <h1>404 - Page Not Found</h1>;
}410 - 永久删除的内容(通知爬虫不再抓取)
500 - 服务器错误
定制 500 页面// pages/500.js
export default function Custom500() {
return <h1>500 - Server-side error occurred</h1>;
}503 - 预计网站会关闭一段时间时(可以保持排名)
Render Queue - 页面存在
JavaScript
内容 添加到渲染队列(需要更长的时间).Ready to be indexed - 满足条件则添加索引到数据库.
robots.txt 文件
直接放在项目根目录即可
//robots.txt |
站点地图 - Sitemaps
- 方便爬取
- 有丰富内容的站点必备
使用 getServerSideProps
动态生成
//pages/sitemap.xml.js |
特定的元标签 - Special Meta Tags
https://developers.google.com/search/docs/advanced/robots/robots_meta_tag#directives
import Head from 'next/head'; |
规范标签 - Canonical Tags
What are Canonical Tags? - Crawling and Indexing | Learn Next.js (nextjs.org)
渲染和排名 - Rendering and Ranking
渲染策略 - Rendering Strategies
静态站点生成(SSG) - Static site generation
预渲染页面, 在构建时生成HTML, 有助于页面性能提升.服务器端呈现(SSR) - Server-Side Rendering
也是预渲染页面, 不同是在每次请求时生成, 这对于具有非常动态的页面非常有用.增量静态再生(ISR) - Incremental Static Regeneration
可以在构建站点之后创建或更新静态页面, 适合拥有大量的页面的网站, 在扩展到数百万页面的同时保留静态的好处。客户端呈现(CSR) - Client-Side Rendering
使用JavaScript
在浏览器中完全渲染网站, 不利于搜索引擎, 适用于数据量大的仪表板、账户页面或任何不需要出现在任何搜索引擎索引中的页面。
网址结构 - URL Structure
使 URL 具有语义
如:/learn/basics/create-nextjs-app
比/learn/course-1/lesson-1
写法更好.逻辑性和一致性
如, 对路径分组, 产品放产品路径下.使用关键词
如: Next文章URL中包含next
关键字.不使用参数构建 URL
大多数情况下,它们不具有语义,搜索引擎可能会混淆它们,从而降低它们在搜索结果中的排名.
元数据 - Metadata
- 标题 - Title
最重要的 SEO 元素之一. - 描述 - Description
不影响排名, 但影响用户点击率. - 打开图表 - Open Graph protocol
用于添加分享时内容丰富程度, 不影响排面, 但有利于点击.
Next.js 中的例子:
import Head from 'next/head'; |
结构化数据 - Structured Data
schema.org
网站指标
Web 指标
Chrome UX Report - Chrome Developers
- Largest Contentful Paint (LCP) :最大内容绘制,测量加载性能。为了提供良好的用户体验,LCP 应在页面首次开始加载后的2.5 秒内发生。
- First Input Delay (FID) :首次输入延迟,测量交互性。为了提供良好的用户体验,页面的 FID 应为100 毫秒或更短。
- Cumulative Layout Shift (CLS) :累积布局偏移,测量视觉稳定性。为了提供良好的用户体验,页面的 CLS 应保持在 0.1. 或更少。
使用谷歌开发者工具 Lighthouse 生成分析报告
- 打开隐私窗口
- 进入 https://nextjs.org/
- 按
F12
调用开发者工具 -> 打开Lighthouse
选项卡 -> 点击Analyze page load
按钮 - 等待分析完成, 出现分析结果.
测试工具
- PageSpeed Insights
- Chrome User Experience Report
- Search Console
TypeScript
...
使用vercel部署项目
https://vercel.com/?utm_source=next-site&utm_medium=learnpages&utm_campaign=next-website