Nesxt
A Next.js Integration Module for NestJS
Overview
Nesxt is a integration module that overrides the default render method of NestJS by implementing Next.js under the hood, as an template engine, allowing you to render reactive interfaces on demand. Hence, every technique for using the MVVM (Model-View-ViewModel) pattern in React will be seamless integrated and available to Nest.
Features
- A per-page basis routing system
- Pre-rendering, both static generation (SSG) and server-side rendering (SSR)
- Extendable API endpoints
- Static Files Serving
- Automatic Code Splitting and Lazy Loading
- Client-side routing with optimised prefetching
- Built-in CSS and Sass support, and support for any CSS-in-JS library
- Custom Error Handlers
- Development environment which supports Hot Module Replacement
- Cross-Platform Support
Summary
Installation
$ yarn add nesxt
Examples
See full examples here.
Usage
Setup
Simply import NesxtModule
in your Nest application.
;;
API Spec
The forRootAsync()
method takes an options object with some useful properties.
Property | Type | Description |
---|---|---|
viewsDir |
null/string | Views directory, relative to pages dir. Default: "views" |
dev |
boolean | Setup the development environment. Default: "true" |
rootPath |
string | Static files root directory. Default: "public" |
serveRoot |
string | Root path under which static app will be served. Default: "" |
renderPath |
string/RegExp | Path to static files (concat with the serveRoot ). Default: * |
exclude |
string[] | Paths to exclude when serving static files. |
serveStaticOptions |
Object | Serve static options (static files) |
Rendering Pages
The NesxtModule
overrides the Express/Fastify render. However, to make things easier, we introduce the @Router
decorator, that can be imported from nesxt
and add it to the method that will render the page. The path for the page is relative to the /pages
directory. This is useful to keep your routes and controllers in separated files.
;
Additionally, the render function is made available on the res
object.
;
The render function takes in the view, as well as the initial props passed to the page.
render =any;
Handling Errors
By default, errors will be handled by next's, which uses the (customizable) _error page.
Custom error handler
A custom error handler can be set to override or enhance it's default behavior.
ErrorHandler Typedef
;
Setting ErrorHandler
You can set the error handler by getting the NesxtModule from nest's container.
// in main.ts file after registering the NesxtModule
Advanced Usage
Passing server data to the Next.js client.
src/app.router.ts
pages/views/Home.tsx
;; ; Home.getInitialProps = ; ;
Configuration
Update your typescript settings to support the Nesxt environment.
tsconfig.json
tsconfig.build.json
Versioning
The major version of nesxt
corresponds to the major version of next
.
Contributing
Thank you for being interested on making this package better. We encourage everyone to help improving this project with some new features, bug fixes and performance issues. Please take a little bit of your time to read our guides, so this process can be faster and easier.
Contribution Guidelines
Take a moment to read about our Contribution Guidelines so you can understand how to submit an issue, commit and create pull requests.
Code of Conduct
We expect you to follow our Code of Conduct. You can read it to understand what kind of behaviour will and will not be tolerated.
Stay in touch
- Author - Mauri Costa
License
Nesxt is MIT licensed.