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在next-plugin-preval时对API路线不起作用。next-plugin-preval行为就像导入JSON一样,您可以利用捆绑器对导入标准静态资产的优化。这包括标准的代码拆分和撤销。请参阅食谱以获取具体示例。
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 */ ) ; 用扩展名.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作为模块。
这可以通过webpack加载程序进行操作,该webpack加载程序将您的代码汇编,并将其运行在Node.js中。
require('next')嘲笑此上下文。对于大多数数据查询,这应该足够,但是如果似乎有些问题,请打开一个问题。 // 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 >
) ;
}正如注释中所述,下一浮游生物离开构建后不会改变。但是,如果提取数据获取功能并根据预览模式有条件调用(通过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时,非常适合网站范围的数据案例。