假设我单击一个链接并将自己转发到/about
路由。这个/about
路由只有getServerSideProps
和一些简单的JSX定义在其中:
// /about page
import Head from 'next/head'
import Users from '../components/Users'
export default function About() {
return (
<div>
<Head>
<title>About Page</title>
<link rel="icon" href="favicon.ico" />
</Head>
<Users /> // Some React Component that happens to be on this page
</div>
)
}
export async getServerSideProps = /* code that calls some API */
来自文档:
当您在客户端请求此页面时,页面转换通过next/link(文档(或next/router(文档(,next.js向运行getServerSideProps的服务器发送API请求。它将返回JSON,其中包含运行getServerSideProps的结果
这意味着当我单击指向/about
页面的链接时,我将只获得包含页面数据的JSON。但是如何获得实际的代码?我得到了数据,但客户是如何得到我页面的这一部分的:
<div>
<Head>
<title>About Page</title>
<link rel="icon" href="favicon.ico" />
</Head>
<Users /> // Some React Component that happens to be on this page
</div>
为了呈现页面,需要使用此JSX代码。从文档中我推断,当使用客户端路由时,它不会传输到客户端。那么,它是从哪里来的呢?
您添加的文档部分仅在处理getServerProps
方面是正确的。
客户端在导航到其他页面时,首先加载页面js文件,然后它意识到它有getServerProps
,调用它,当数据来自服务器时,渲染页面。