Skip to content
Dashboard

Next.js 5: Universal Webpack, CSS Imports, Plugins and Zones

$ npm i next@latest react@latest react-dom@latest

In addition to bumping Next.js, we upgrade the peer dependencies `react` and `react-dom`

Link to headingUniversal Webpack and Next Plugins

module.exports = {
webpack(config, { dev }) {
// modify it!
return config;
}
}

An example of the optional `next.config.js` file

Link to headingCSS, LESS, SASS, SCSS and CSS Modules

import './index.css'
export default () => (
<div>
<p>I love CSS!</p>
</div>
)

An example page (`pages/index.js`) using CSS imports thanks to Universal Webpack

$ npm i --save css-loader style-loader postcss-loader

Next.js which gives you the freedom to pick and choose which loaders you need and to upgrade them to different versions at will.

module.exports = {
webpack(config, options) {
const { dev, isServer } = options
const extractCSSPlugin = new ExtractTextPlugin({
filename: 'static/style.css',
disable: dev
})
config.module.rules.push({
test: /\.css$/,
use: cssLoaderConfig(extractCSSPlugin, {
cssModules,
dev,
isServer
})
})
return config
}
}

Extending the raw webpack config gives you great flexibility and control

const withCss = require('next-css');
module.exports = withCss({ /* extra optional config */ })

All it takes to enable importing `.css` files is to bring in `next-css`

Link to headingTypeScript Support

module.exports = {
webpack(config, options) {
const { dir, defaultLoaders } = options
config.resolve.extensions.push('.ts', '.tsx')
config.module.rules.push({
test: /\.+(ts|tsx)$/,
include: [dir],
exclude: /node_modules/,
use: [
defaultLoaders.babel,
{ loader: 'ts-loader', options: { transpileOnly: true } }
]
})
return config
}
}

All we have to do is enable the `ts-loader`

const withTs = require('next-typescript');
module.exports = withTs({ /* additional config*/ })

Plugins can be trivially composed: they are just functions

Link to headingBetter Support for React Altlibs & Module Overloading

$ npm i @zeit/next-preact preact preact-compat

We install the preact plugin and necessary peer dependencies

const withPreact = require('@zeit/next-preact');
module.exports = withPreact();

Our new next.config.js ready for preact

Link to headingOptional External Sourcemaps in Production

module.exports = {
webpack(config, { dev }) {
if(!dev) {
config.devtool = 'source-map'
}
return config;
}
}

We simply configure the `devtool` option differently when not in development

Link to headingZones

Both of our pages have a seamless experience, but they belong to separate apps
Both of our pages have a seamless experience, but they belong to separate apps
Every time a change happens inside a PR our bot automatically deploys it
Every time a change happens inside a PR our bot automatically deploys it
{
"rules": [
{ "pathname": "/docs", "dest": "our-docs.now.sh" },
{ "pathname": "/api", "dest": "our-docs.now.sh" },
{ "dest": "my-main-website.now.sh" }
]
}

These simple rules allow you to compose microservices and zones together

$ vercel alias -r rules.json my-domain.com

Link to headingFaster Production Build Times

Our main application production build now takes 38 fewer seconds to complete
Our main application production build now takes 38 fewer seconds to complete

Link to headingImproved Caching for Dynamic Imports

/_next/1517592683901/webpack/chun@components_hello1_1345d10fc951cd6717c5676c467579a6.js

/_next/webpack/chun@components_hello1_1345d10fc951cd6717c5676c467579a6-b7874680a9e21fb6eb89.js

Link to headingFragments

import Document, { Head, Main, NextScript } from 'next/document'
export default class extends Document {
render() {
return (
<html>
<Head/>
<body>
<Main />
<NextScript />
</body>
</html>
)
}

`Document` allows you to customize the entire server-rendered output of a page

Link to headingMore Accurate Errors

Errors now show the correct line, file and function name
Errors now show the correct line, file and function name

Link to headingConclusion