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

nest-next-renderer

v2.1.0

Published

Render Next.js pages in Nest.js applications

Downloads

15

Readme

Nest Next Renderer

npm version npm downloads/month GitHub license

Module for rendering Next.js pages inside Next.js applications.

Note: At the moment this package works only with Next and Fastify.

Installation

This package requires to be installed in a Next application that is using Fastify as platform (read more).

  1. Make sure you have the peer-dependencies installed: react, react-dom and next.

    Note: If you are using TypeScript, you should install @types/react and @types/react-dom as well.

    In theory, you should install just react, react-dom and next because the rest of the dependencies should already be installed in your project.

  2. Install nest-next-renderer using yarn

    yarn add nest-next-renderer

    or npm

    npm i nest-next-renderer

Usage

Assuming that you have a Next application in the ./client directory with 2 pages (Index and Login) here is how you import the NextRendererModule module:

import { Module } from '@nestjs/common';
import { NextRendererModule } from 'nest-next-renderer';

@Module({
  imports: [
    NextRendererModule.forRoot({
      nextServerOptions: {
        dir: './client',
      },
      /**
       * The level of error pass-through for your application
       * This is useful because Nest doesn't know how to handle Next's routing for assets.
       * So in this case we might want to pass through 404 errors to Next.
       *
       * @default ErrorPassThroughLevel.ALL
       */
      errorPassThrough: ErrorPassThroughLevel.ALL,
    }),
  ],
})
export class AppModule {}

Example of a controller:

import { Body, Controller, Get, Post, Res } from '@nestjs/common';
import { FastifyReply } from 'fastify';
import { UsersService } from './services/users.service';
import { LoginPageProps } from './shared/LoginPageProps';

@Controller()
export class AuthController {
  constructor(
    private readonly userService: UsersService,
  ) {}

  @Get('index')
  async getIndex(@Res() res: FastifyReply) {
    return res.render('/', undefined);
  }

  @Get('login')
  async getIdentifier(@Res() res: FastifyReply) {
    return res.render<LoginPageProps>('/login', undefined);
  }

  @Post('login')
  async postIdentifier(
    @Body('username') username: string,
    @Body('password') password: string,
    @Res() res: FastifyReply,
  ) {
    try {
      // Validate credentials, set cookies etc.
      return res.redirect(302, '/');
    } catch (e) {
      return res.render<LoginPageProps>('/login', {
        error: e.message,
        username,
        password,
      });
    }
  }
}

Contributing

You can contribute to this project by opening an issue or creating a pull request.

Note: If you want to test this library locally by using yarn link, you should know that there will be a conflict between the Nest packages used by this project (@nestjs/common and @nestjs/core) and the ones in your test project. To fix this you have 2 options:

  • use the same modules path in both projects by linking the Nest modules too;

  • paste the path to your test project's nest-next-renderer folder in the .linked.packages file and use the yarn dev while developing. Example:

    # .linked.packages
    /path/to/your/project/node_modules/nest-next-renderer

    Now everytime you change something, the changes will be reflected in your test project.

TODO(s)

  • [ ] Add tests
  • [ ] Add documentation and example (document the default values for the NextRendererModuleOptions)
  • [ ] Add @Render decorator
  • [ ] Make it work with Express or others
  • [ ] Make it possible to render any page without a controller (useFileSystemPublicRoutes + @Get('*') and @Post('*') that calls next.handle)
  • [ ] Generate enum for the view parameter based on the content of the pages folder
  • [ ] Server not working with hot reload (if it's on the consumer side document the proper configuration)