• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

xinpianchang/next-koa: A koa middleware for next.js with some common tools

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

xinpianchang/next-koa

开源软件地址:

https://github.com/xinpianchang/next-koa

开源编程语言:

TypeScript 80.9%

开源软件介绍:

Koa2 & Next.js hydration packages

NPM Version

Usage

  • Firstly setup a koa server entry
// server/index.js

const NextKoa = require('next-koa')
const Koa = require('koa')
const Router = require('koa2-router')
const path = require('path')

const app = new Koa()
const router = new Router()
const nextApp = NextKoa({
  dev: process.env.NODE_ENV !== 'production',
  dir: path.resolve(__dirname, '..')
})

// console nextConfig
console.log(nextApp.nextConfig)

app.use(nextApp.middleware)
app.use((ctx, next) => {
  ctx.state.homepage = '/'
  return next()
})
app.use(router)

// using renderer of next.js to emit pages/about.tsx
// the state can be captured by next-koa/getstate package
// and is rendered as ctx.state merged by this data
// here data usually is a plain object
router.get('/about', ctx => ctx.render('about', { title: 'about us' }))

// if nextConfig.useFileSystemPublicRoutes is missing or true
// then you can get any page under `pages` by directly fetching
// the pathname without defining the koa routes

app.listen(3000)
  • Then write your own next.js pages
// pages/about.tsx

import React from 'react'
import Head from 'next/head'
import Link from 'next/link'
import getInitialState from 'next-koa/getstate'

export default class AboutPage extends React.Component {
  static async getInitialProps(ctx) {
    // getInitalState fetches data both on client/server
    const state = await getInitialState(ctx)
    // { title: 'about us', homepage: '/' }
    return state
  }
  render() {
    return <>
      <Head>
        <title>{this.props.title}</title>
      </Head>
      <Link href={this.props.homepage}>
        <a>Homepage</a>
      </Link>
    </>
  }
}
  • If you want next.js layout feature, just like this
// pages/_app.tsx
import App from 'next-koa/app'

export default class CustomApp extends App {
}
  • in order to make next-koa/app being packed by webpack,
    we should use this plugin to include this module
// next.config.js
const withNextKoaPlugin = require('next-koa/plugin')
module.exports = withNextKoaPlugin({
  // ...config
})
  • Now we can export a Layout
// layout/index.tsx
import React from 'react'
import { withLayout } from 'next-koa/layout'

export default withLayout(({ children }: { children: React.ReactNode }) => {
  return <section className='layout'>
    <nav>
      <ul>
        {...}
      </ul>
    </nav>
    <main className='container'>
      {children}
    </main>
  </section>
})
  • then we can use the layout above to decorate any pages
// pages/index.tsx
import React from 'react'
import withCustomLayout from '../layout'

const IndexPage: React.FC<any> = //...

export default withCustomLayout(IndexPage)



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
kirkwood-cis-171-master/java-koans发布时间:2022-07-10
下一篇:
kaelzhang/express-to-koa: Use express middlewares in Koa2, the one that really w ...发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap