next superjson plugin
v0.6.1
export default function Page ( { date } ) {
return < div > Today is { date . toDateString ( ) } </ div > ;
}
// You can also use getInitialProps, getStaticProps
export const getServerSideProps = ( ) => {
return {
props : {
date : new Date ( ) ,
} ,
} ;
} ; // Use "data-superjson" attribute to pass non-serializable props to client components
// No needs to change the propsType of Client Component (It's type-safe!)
export default function ServerComponent ( ) {
const date = new Date ( ) ;
return < ClientComponent date = { date } data-superjson /> ;
}data-superjson属性。 首先安装软件包:
npm install superjson next-superjson-plugin
# or Yarn
yarn add superjson next-superjson-plugin将插件添加到next.config.js
// next.config.js
module . exports = {
experimental : {
swcPlugins : [ [ "next-superjson-plugin" , { } ] ] ,
} ,
} ;您可以使用excluded选项将特定属性排除在序列化之外。
[ 'next-superjson-plugin' , { excluded : [ "someProp" ] } ] , 测序图
参与者Next.js
参与者SWC插件
参与者Superjson
next.js- >> SWC插件:请求转换
SWC插件 - >> SWC插件:转换页面/组件<br>使用Superjson
SWC插件 - >> Next.js:取模块
next.js-> superjson:连接
next.js- >> superjson:序列化props <br>(日期,bigint,set,map ..)
通过SWC插件注意:GetInitialProps <br> GetServersideProps <br> GetStaticProps <br>服务器组件
superjson- >> next.js:应对道具
通过SWC插件注意:页面<br>客户端组件
插件始终确保与Next.js金丝雀版本的兼容性。
留下问题