next.js初始化參數(shù)設(shè)置getServerSideProps應(yīng)用學(xué)習(xí)
使用
getServerSideProps 是 next.js 中的一項(xiàng)特色功能,可以讓我們在給頁面設(shè)置一些初始的 props 參數(shù)。
getServerSideProps 是定義在頁面中的 API,但是其執(zhí)行環(huán)境是 node 端,而不是客戶端,一般常見使用場景為:
- 頁面前置權(quán)限校驗(yàn)
- 頁面必備參數(shù)獲取
使用時(shí)需要在對應(yīng)的 page 文件中 export getServerSideProps
const Page = props => {
return <div>page</div>;
};
export async function getServerSideProps(context) {
return {
props: {}
};
}
export default Page;
這樣便可以從頁面組件中直接使用 props 來獲取 getServerSideProps 注入的 props 了。
ts 定義
如果是在 TS 中 next.js 也提供了 GetServerSideProps 接口來方便智能提示。
import { GetServerSideProps } from 'next';
export const getServerSideProps: GetServerSideProps = async context => {
return {
props: {}
};
};
context
getServerSideProps 中的 context 參數(shù)包含了常用的請求的 req、res、params、query 等參數(shù),還包含了 preview、previewData、resolvedUrl、locale 等參數(shù)
實(shí)現(xiàn)
當(dāng) getServerSideProps 所在頁面為 SSR 服務(wù)端渲染時(shí),getServerSideProps 中的數(shù)據(jù)將會被放到全局的 _NEXT_DATA 中,用于 hydrate。
而非 SSR 情況下,進(jìn)入該頁面 next.js 將會自動發(fā)請求到: _next/data/development/{url}.json?{query},其中 development 為開發(fā)環(huán)境下地址段,該請求的返回值為:
{
"pageProps": "返回的 props 數(shù)據(jù)內(nèi)容",
"__N_SSP": true
}
從而將 getServerSideProps 返回值在頁面掛載時(shí)注入進(jìn)去。
請求 API
需要注意 getServerSideProps 為 node server 端代碼,無法在其中直接請求內(nèi)部 API,因?yàn)闀也坏降刂罚ㄍ獠?API 可以請求,認(rèn)為是這段代碼是獨(dú)立的 node 端代碼就行了)。如果想要調(diào)用內(nèi)部 API 可以將對應(yīng)的 API handler 拆解,作為方法調(diào)用。
// api/test.ts
export const getContent = async () => {
return content;
};
export default async function handler(req: NextApiRequest, res: NextApiResponse<Response<T[]>>) {
res.status(200).json({
code: 0,
data: await getContent()
});
}
// pages/page.tsx
import { getContent } from './api/test.ts';
export const getServerSideProps: GetServerSideProps = async context => {
return {
props: await getContent()
};
};
問題
還有一點(diǎn)需要注意的是,雖然 getServerSideProps 為 server 端代碼,但是客戶端打包時(shí)好似仍然會將對應(yīng)的代碼打包到頁面中,所以應(yīng)當(dāng)盡量避免其中有過于復(fù)雜的邏輯或引入一些較大的包。
特殊處理 - 404、跳轉(zhuǎn)、異常
getServerSideProps 返回值除了可以設(shè)置 props 外還可以使用 notFound 來強(qiáng)制頁面跳轉(zhuǎn)到 404。
export async function getServerSideProps(context) {
const data = await getData();
if (!data) {
return {
notFound: true
};
}
return {
props: { data }
}
或者是使用 redirect 來將頁面重定向。
export async function getServerSideProps(context) {
const data = await getData();
if (!data) {
return {
redirect: {
destination: '/',
permanent: false
}
};
}
return {
props: { data }
};
}
并且如果 getServerSideProps 報(bào)錯了,next.js 將會直接跳轉(zhuǎn)到 500 頁面,又省下一段異常處理代碼,可喜可賀。
總結(jié)
通過 next.js 的 getServerSideProps,我們在開發(fā)中可以很好的協(xié)調(diào)前后端數(shù)據(jù),一些頁面初始化數(shù)據(jù)、頁面鑒權(quán)可以直接在 getServerSideProps 中進(jìn)行處理,這樣可以大大簡化頁面邏輯,還保障前后端的統(tǒng)一性,更多關(guān)于next.js 初始化參數(shù)設(shè)置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javaScript實(shí)現(xiàn)游戲倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了javaScript實(shí)現(xiàn)游戲倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
JavaScript實(shí)現(xiàn)Tab欄切換功能詳解
這篇文章主要介紹了JavaScript實(shí)現(xiàn)Tab欄切換的實(shí)現(xiàn)方式,是面向?qū)ο蟮膶懛ǎ疚慕o大家分享詳細(xì)案例代碼,需要的朋友可以參考下2022-10-10
javascript 實(shí)現(xiàn)鍵盤上下左右功能的小例子
這篇文章介紹了javascript 實(shí)現(xiàn)鍵盤上下左右功能的小例子,有需要的朋友可以參考一下2013-09-09
button沒寫type=button會導(dǎo)致點(diǎn)擊時(shí)提交
點(diǎn)擊了一個彈窗中的按鈕,想到彈窗消失了,經(jīng)測試后發(fā)現(xiàn)button 沒寫type=button會導(dǎo)致點(diǎn)擊時(shí)提交2014-03-03
JSP防止網(wǎng)頁刷新重復(fù)提交數(shù)據(jù)的幾種方法
這篇文章主要介紹了JSP防止網(wǎng)頁刷新重復(fù)提交數(shù)據(jù)的幾種方法,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

