next plugin preval
v1.2.6
ビルド時に(データフェッチの場合)非同期機能(データフェッチの場合)を前にし、JSONのようにインポートする
// data.preval.js (or data.preval.ts)
// step 1: create a data.preval.js (or data.preval.ts) file
import preval from 'next-plugin-preval' ;
// step 2: write an async function that fetches your data
async function getData ( ) {
const { title , body } = await /* your data fetching function */ ;
return { title , body } ;
}
// step 3: export default and wrap with `preval()`
export default preval ( getData ( ) ) ; // Component.js (or Component.ts)
// step 4: import the preval
import data from './data.preval' ;
// step 5: use the data. (it's there synchronously from the build step!)
const { title , body } = data ;
function Component ( ) {
return (
< >
< h1 > { title } </ h1 >
< p > { body } </ p >
</ >
) ;
}
export default Component ; next.jsが静的データを提供する主要なメカニズムはgetStaticPropsです。これは優れた機能であり、多くのユースケースに適したツールです。ただし、 getStaticPropsでカバーされていない静的データの他のユースケースがあります。
getStaticPropsやや厄介なメカニズムです。新しいページごとに、同じ静的データを再フェッチする必要があるためです。たとえば、 getStaticPropsを使用してヘッダーのコンテンツを取得すると、そのデータはすべてのページの変更で再獲得されます。getStaticProps APIルートでは機能しませんがnext-plugin-preval機能します。next-plugin-preval JSONのインポートのように動作するため、標準の静的資産をインポートするためにBundlersが持っている最適化を活用できます。これには、標準のコードスプリッティングとデスピングが含まれます。具体的な例については、レシピを参照してください。
yarn add next-plugin-preval
または
npm i next-plugin-preval
// next.config.js
const createNextPluginPreval = require ( 'next-plugin-preval/config' ) ;
const withNextPluginPreval = createNextPluginPreval ( ) ;
module . exports = withNextPluginPreval ( /* optionally add a next.js config */ ) ; extension .preval.tsまたは.preval.jsを使用してファイルを作成し、 preval()にラップされた約束をエクスポートします。
// my-data.preval.js
import preval from 'next-plugin-preval' ;
async function getData ( ) {
return { hello : 'world' ; }
}
export default preval ( getData ( ) ) ;次に、そのファイルをどこにでもインポートします。約束の結果が返されます。
// component.js (or any file)
import myData from './my-data.preval' ; // ? this is effectively like importing JSON
function Component ( ) {
return (
< div >
< pre > { JSON . stringify ( myData , null , 2 ) } </ pre >
</ div >
) ;
}
export default Component ; .prevalファイルをインポートするとき、JSONをインポートしているようなものです。 next-plugin-prevalビルド中に機能を実行し、JSON Blobをモジュールとしてインラインにします。
これは、コードを取得してコンパイルし、node.jsの内部で実行するWebパックローダーを介して機能します。
require('next')を介してこのコンテキストをock笑するために最善を尽くします。ほとんどのデータクエリでは、これで十分ですが、何かが外れているように見える場合は問題を開いてください。 // header-data.preval.js
import preval from 'next-plugin-preval' ;
async function getHeaderData ( ) {
const headerData = await /* your data fetching function */ ;
return headerData ;
}
export default preval ( getHeaderData ( ) ) ; // header.js
import headerData from './header-data.preval' ;
const { title } = headerData ;
function Header ( ) {
return < header > { title } </ header > ;
}
export default Header ; // products.preval.js
import preval from 'next-plugin-preval' ;
async function getProducts ( ) {
const products = await /* your data fetching function */ ;
// create a hash-map for O(1) lookups
return products . reduce ( ( productsById , product ) => {
productsById [ product . id ] = product ;
return productsById ;
} , { } ) ;
}
export default preval ( getProducts ( ) ) ; // /pages/api/products/[id].js
import productsById from '../products.preval.js' ;
const handler = ( req , res ) => {
const { id } = req . params ;
const product = productsById [ id ] ;
if ( ! product ) {
res . status ( 404 ) . end ( ) ;
return ;
}
res . json ( product ) ;
} ;
export default handler ; // states.preval.js
import preval from 'next-plugin-preval' ;
async function getAvailableStates ( ) {
const states = await /* your data fetching function */ ;
return states ;
}
export default preval ( getAvailableStates ( ) ) ; // state-picker.js
import { useState , useEffect } from 'react' ;
function StatePicker ( { value , onChange } ) {
const [ states , setStates ] = useState ( [ ] ) ;
useEffect ( ( ) => {
// ES6 dynamic import
import ( './states.preval' ) . then ( ( response ) => setStates ( response . default ) ) ;
} , [ ] ) ;
if ( ! states . length ) {
return < div > Loading… </ div > ;
}
return (
< select value = { value } onChange = { onChange } >
{ states . map ( ( { label , value } ) => (
< option key = { value } value = { value } >
{ label }
</ option >
) ) }
</ select >
) ;
}メモに記載されているように、次のPlugin-Prevalの結果はビルドを離れた後は変わりません。ただし、データの取得関数を抽出し、プレビューモードに基づいて条件付きで呼び出す場合でも、プレビューモードを機能させることができます( context.previewを介して。プレビューモードがアクティブでない場合は、Prevalファイルにデフォルトできます。
// get-data.js
// 1. extract a data fetching function
async function getData ( ) {
const data = await /* your data fetching function */ ;
return data
} // data.preval.js
import preval from 'next-plugin-preval' ;
import getData from './getData' ;
// 2. use that data fetching function in the preval
export default preval ( getData ( ) ) ; // /pages/some-page.js
import data from './data.preval' ;
import getData from './get-data' ;
export async function getStaticProps ( context ) {
// 3. conditionally call the data fetching function defaulting to the prevalled version
const data = context . preview ? await getData ( ) : data ;
return { props : { data } } ;
} next-data-hooks - getStaticProps Reactフックとして使用するパターンを作成します。プレビューモードまたはISRが必要な場合、サイト全体のデータケースに最適です。