Next.js & SSR & CSR & SG

getStaticPaths, getStaticProps, getServerSideProps

getStaticProps (Static Generation): Fetch data at build time.

getStaticPaths (Static Generation): Specify dynamic routes to pre-render based on data.

getServerSideProps (Server-side Rendering): Fetch data on each request.

https://nextjs.org/docs/basic-features/data-fetching

React SSR

https://reactjs.org/docs/react-dom-server.html

  1. support both server and browser environments
renderToString()
renderToStaticMarkup()
  1. depend on a package (stream) & only support the server environment
renderToNodeStream()
renderToStaticNodeStream()

// ES modules
import ReactDOMServer from 'react-dom/server'; // CommonJS
var ReactDOMServer = require('react-dom/server');

如何支持 UMD 模块导入?看源码

https://www.cnblogs.com/xgqfrms/p/13728515.html

Next.js

https://nextjs.org/

routing system

An intuitive page-based routing system (with support for dynamic routes)

https://nextjs.org/docs/basic-features/pages

https://nextjs.org/docs/routing/dynamic-routes

SSR

Server Side Render

CSR

Client Side Render

SG

Static Generation

Site Generator

gatsby

https://www.gatsbyjs.org/

SSG

Static Site Generator

https://nextjs.org/docs/basic-features/pages#static-generation-recommended

GR ???

PR

pre-rendering

https://nextjs.org/docs/basic-features/pages#pre-rendering

demo


const log = console.log; log(`Article page`) // This function gets called at build time
// export async function getStaticPaths() {
// // Call an external API endpoint to get posts
// // const res = await fetch('https://.../posts')
// // const posts = await res.json()
// const routes = [
// {
// id: 1,
// },
// {
// id: 2,
// },
// {
// id: 3,
// },
// ]; // const posts = await Promise.resolve(routes); // // Get the paths we want to pre-render based on posts
// const paths = posts.map((post) => `/articles/${post.id}`); // log(`paths =`, paths) // // We'll pre-render only these paths at build time.
// // { fallback: false } means other routes should 404.
// return {
// paths,
// fallback: false,
// };
// } // This also gets called at build time
// export async function getStaticProps({ params }) {
// log(`params = `, params)
// // { id: '2' }
// // params contains the post `id`.
// // If the route is like /posts/1, then params.id is 1
// // const res = await fetch(`https://.../posts/${params.id}`)
// // const articles = await res.json()
// const blogs = [
// {
// title: "article 1",
// },
// {
// title: "article 2",
// },
// {
// title: "article 3",
// },
// ];
// const articles = await Promise.resolve(blogs); // const {
// id,
// } = params;
// // Pass post data to the page via props
// log(`getStaticProps`, params)
// return {
// props: {
// id,
// article: articles[`${id - 1}`],
// },
// };
// }

const log = console.log; log(`Article page`) // This gets called on every request
export async function getServerSideProps({ params }) {
log(`ServerSide params = `, params)
// Fetch data from external API
// const res = await fetch(`https://.../data`)
// const data = await res.json()
const blogs = [
{
title: "article 1",
},
{
title: "article 2",
},
{
title: "article 3",
},
];
const articles = await Promise.resolve(blogs); const {
id,
} = params;
log(`getServerSideProps`, params)
return {
props: {
id,
article: articles[`${id - 1}`],
},
};
} function Article(props) {
log(`props = `, props)
// log(`props.url`, props.url)
// const {
// articles,
// } = props;
const {
// url: {
// query: {
// id,
// },
// },
id,
article,
} = props;
return (
<div className="posts-box">
<div className="posts-title">articles Page</div>
<div>article id = {id}</div>
<div>{JSON.stringify(article)}</div>
<style jsx>{`
.posts-box {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
`}</style>
<style jsx global>{`
.posts-title {
font-size: 23px;
color: #f0f;
}
`}</style>
</div>
);
} export default Article;

refs

https://www.cnblogs.com/xgqfrms/p/10720612.html



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. 深入理解MySql子查询IN的执行和优化
  2. MessageBox.Show()的各种用法
  3. 标准化css属性顺序
  4. CF 121E Lucky Array 【树状数组】
  5. 《Android开发艺术探索》读书笔记 (9) 第9章 四大组件的工作过程
  6. 单例模式 - OK
  7. NYOJ 1068 ST(段树 为段更新+间隔总和)
  8. 用shell统计访问日志里每个ip访问次数【转】
  9. JTree实例
  10. 1754: [Usaco2005 qua]Bull Math
  11. Spring Cloud 注册中心Eureka
  12. javascript内存管理(堆和栈)和javascript运行机制
  13. OO第二单元作业小结
  14. centos安装python3.7和yum报错解决方法
  15. Liunx百宝箱(Centos补充)
  16. Docker摘要
  17. 【SQL】SQL整表复制
  18. BASH 环境
  19. java 线程(七)等待与唤醒
  20. Rplidar学习(三)—— ROS下进行rplidar调试

热门文章

  1. https://www.cnblogs.com/wclwcw/p/7515515.html
  2. get uuid
  3. Kubernetes TensorFlow 默认 特定 集群管理器 虚拟化技术
  4. Location和Content-Location
  5. react报错 TypeError: Cannot read property &#39;setState&#39; of undefined
  6. Java——序列化与反序列化
  7. Linux和Xshell安装
  8. OsgEarth开发笔记(一):Osg3.6.3+OsgEarth3.1+vs2019x64开发环境搭建(上)
  9. MapReduce统计每个用户的使用总流量
  10. P1108 低价购买(DP)