{"id":15462947,"url":"https://github.com/xinpianchang/next-koa","last_synced_at":"2025-07-03T17:04:47.880Z","repository":{"id":68475706,"uuid":"209276456","full_name":"xinpianchang/next-koa","owner":"xinpianchang","description":"A koa middleware for next.js with some common tools","archived":false,"fork":false,"pushed_at":"2023-10-25T09:59:12.000Z","size":75,"stargazers_count":13,"open_issues_count":1,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-16T22:08:46.176Z","etag":null,"topics":["koa","koa2","middleware","next-koa","nextjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xinpianchang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-09-18T10:01:43.000Z","updated_at":"2022-07-20T11:17:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"a24a74c9-fed3-4f38-b1c9-f619ade179cd","html_url":"https://github.com/xinpianchang/next-koa","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinpianchang%2Fnext-koa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinpianchang%2Fnext-koa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinpianchang%2Fnext-koa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinpianchang%2Fnext-koa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xinpianchang","download_url":"https://codeload.github.com/xinpianchang/next-koa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250222278,"owners_count":21394848,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["koa","koa2","middleware","next-koa","nextjs"],"created_at":"2024-10-02T00:06:14.787Z","updated_at":"2025-04-22T10:42:52.386Z","avatar_url":"https://github.com/xinpianchang.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Koa2 \u0026 Next.js hydration packages\n\n  [![NPM Version][npm-image]][npm-url]\n\n# Usage\n* Firstly setup a koa server entry\n``` javascript\n// server/index.js\n\nconst NextKoa = require('next-koa')\nconst Koa = require('koa')\nconst Router = require('koa2-router')\nconst path = require('path')\n\nconst app = new Koa()\nconst router = new Router()\nconst nextApp = NextKoa({\n  dev: process.env.NODE_ENV !== 'production',\n  dir: path.resolve(__dirname, '..')\n})\n\n// console nextConfig\nconsole.log(nextApp.nextConfig)\n\napp.use(nextApp.middleware)\napp.use((ctx, next) =\u003e {\n  ctx.state.homepage = '/'\n  return next()\n})\napp.use(router)\n\n// using renderer of next.js to emit pages/about.tsx\n// the state can be captured by next-koa/getstate package\n// and is rendered as ctx.state merged by this data\n// here data usually is a plain object\nrouter.get('/about', ctx =\u003e ctx.render('about', { title: 'about us' }))\n\n// if nextConfig.useFileSystemPublicRoutes is missing or true\n// then you can get any page under `pages` by directly fetching\n// the pathname without defining the koa routes\n\napp.listen(3000)\n```\n\n* Then write your own next.js pages\n```jsx\n// pages/about.tsx\n\nimport React from 'react'\nimport Head from 'next/head'\nimport Link from 'next/link'\nimport getInitialState from 'next-koa/getstate'\n\nexport default class AboutPage extends React.Component {\n  static async getInitialProps(ctx) {\n    // getInitalState fetches data both on client/server\n    const state = await getInitialState(ctx)\n    // { title: 'about us', homepage: '/' }\n    return state\n  }\n  render() {\n    return \u003c\u003e\n      \u003cHead\u003e\n        \u003ctitle\u003e{this.props.title}\u003c/title\u003e\n      \u003c/Head\u003e\n      \u003cLink href={this.props.homepage}\u003e\n        \u003ca\u003eHomepage\u003c/a\u003e\n      \u003c/Link\u003e\n    \u003c/\u003e\n  }\n}\n\n```\n\n* If you want next.js layout feature, just like this\n```jsx\n// pages/_app.tsx\nimport App from 'next-koa/app'\n\nexport default class CustomApp extends App {\n}\n```\n\n* in order to make `next-koa/app` being packed by webpack,\\\nwe should use this plugin to include this module \n```js\n// next.config.js\nconst withNextKoaPlugin = require('next-koa/plugin')\nmodule.exports = withNextKoaPlugin({\n  // ...config\n})\n```\n\n* Now we can export a Layout\n```tsx\n// layout/index.tsx\nimport React from 'react'\nimport { withLayout } from 'next-koa/layout'\n\nexport default withLayout(({ children }: { children: React.ReactNode }) =\u003e {\n  return \u003csection className='layout'\u003e\n    \u003cnav\u003e\n      \u003cul\u003e\n        {...}\n      \u003c/ul\u003e\n    \u003c/nav\u003e\n    \u003cmain className='container'\u003e\n      {children}\n    \u003c/main\u003e\n  \u003c/section\u003e\n})\n```\n\n* then we can use the layout above to decorate any pages\n```tsx\n// pages/index.tsx\nimport React from 'react'\nimport withCustomLayout from '../layout'\n\nconst IndexPage: React.FC\u003cany\u003e = //...\n\nexport default withCustomLayout(IndexPage)\n```\n\n[npm-image]: https://img.shields.io/npm/v/next-koa.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/next-koa\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxinpianchang%2Fnext-koa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxinpianchang%2Fnext-koa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxinpianchang%2Fnext-koa/lists"}