Integrating Next.js

In this recipe we will integrate next.js - The popular React framework that comes with a simple SSR solution built in.

Warning: This recipe is highly unstable as long as the following issues within next.js are not properly solved, integrate it at your own risk!

Table of Contents

Installation of dependencies

Since next.js is a framework, we luckily don't need to install a lot of new dependencies, change into the main application package, e.g. packages/my-fancy-ui, and execute the following command

yarn add next uglifyjs-webpack-plugin

Adding the next scripts

Afterwards we add the lifecycle scripts into the same package, replace the existing ones with the ones provided by next.js, e.g.

{
  "scripts": {
    "dev": "next src",
    "build": "next build src",
    "start": "next start src"
  }
}

Adding the next config

Currently next.js crashes if you use newer features of EcmaScript, therefore we need to add a next.config.js in the main application package and override the internally used UglifyJsPlugin with a newer version.

// next.config.js
const Uglify = require('uglifyjs-webpack-plugin');

module.exports = {
  webpack: (config, { buildId, dev }) => {
    config.plugins = config.plugins.filter(
        (plugin) => (plugin.constructor.name !== 'UglifyJsPlugin')
    )

    config.plugins.push(
      new Uglify()
    );

    return config;
  }
}

Cleaning up the scaffold

Since next.js comes with it's own server and rendering, we can safely execute the following commands from the root of the scaffold, as always please replace the my-fancy-ui with your scaffolds name.

# Remove the old entry points since next.js has entries placed in pages/
rm packages/my-fancy-ui/src/app.* packages/my-fancy-ui/src/index.*
# Remove the hypernova rendering microservice
rm -rf packages/my-fancy-ui-hypernova
# Remove the regular http web server
rm -rf packages/my-fancy-ui-server

results matching ""

    No results matching ""