npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-nextjs

v9.0.0

Published

Use React.js as a template engine in Koa.js with Next.js

Downloads

12

Readme

koa-nextjs

The main goal of this project is to allow developers to write user interfaces using React and serve them with server-side rendering (SSR) and then re-hydrate it in the browser.

Also enables hot-module replacement (HMR) because it uses Next.js under the scenes. It's possible to have full control over the Next.js settings.

Installation

Install this module and its peer-dependencies with:

yarn add koa-nextjs koa koa-router next react react-dom

Usage

Server-side

// index.js

const path = require('path');
const Koa = require("koa");
const RenderEngine = require("koa-nextjs");

async function main() {
  const app = new Koa();

  const engine = await RenderEngine.start({
    // See: https://nextjs.org/docs#custom-server-and-routing
    options: {
      dev: process.env.NODE_ENV !== "production",
      quiet: true,
      // `pages` directory should be inside /client
      dir: path.join(__dirname, "client"),
      conf: {
        distDir: "build", // relative to `options.dir`
        useFileSystemPublicRoutes: false,
      },
    },
  });

  // engine.router is an instance of `koa-router`
  app.use(engine.router.routes());
  // See: https://github.com/koajs/koa/blob/master/docs/api/index.md#appcontext
  app.context.render = engine.renderer();

  app.use(async ctx => {
    // Remember to always `await`
    await ctx.render({
      page: "main/Home", // path for a React component in `/client/pages/main/Home.js`
      props: {
        // only plain (serializable) JS primitives or objects.
        user: { name: "" },
      },
      options: {
        // custom Next.js options
      },
    })
  })
}

main().catch(err => {
  console.error(err);
  process.exit(1);
});

Client-side

// client/pages/main/Home.js

import React from "react";
import { withSSR } from "koa-nextjs/react"; // <-- don't forget this

const Home = ({ name, ...props }) => (
  <div {...props}>
    <h1>Hello</h1>
    <p>
      Name: <span>{name}</span>
    </p>
    <p>
      <a href="/about">Go to about</a>
    </p>
  </div>
);

export default withSSR()(Home); // <-- don't forget this

Example

See /example for a complete example with styled-components.

Gotchas

  • Hot reloading will not work for server-side files. Use nodemon and make sure to only watch server files.

  • Do not use import Link from "next/link" neither any pre-fetching option. Stick with old-school anchor navigation: <a href="/other-route" />

License

MIT