README
next-compression
Next.js plugin to compress static assets at build time and serve them instead of having to compress on-the-fly.
Installation
$ npm i --save @moxy/next-compression
Usage
next.config.js
Setup the plugin in the next.config.js
file:
const withCompression = require('@moxy/next-compression');
module.exports = withCompression({ ...nextConfig });
This plugin will automatically disable itself if you disable compress
in your next.config.js
.
Express
Express middleware used to serve the previously compressed files, by leveraging express-static-gzip.
First, you need to setup a custom express server. Then, simply add the middleware like so:
// server.js
const { compressionMiddleware } = require('@moxy/next-compression');
// ....
app.prepare().then(() => {
const server = express();
server.use(compressionMiddleware({
requestPath: '/',
fsPath: '/directory/static'
serveStatic: {
maxAge: 365 * 24 * 60 * 60 * 1000, // 1 year
immutable: true,
}
}));
});
Available options
Option | Description | Type | Default |
---|---|---|---|
requestPath | Defines the request path that will activate the express-static-gzip middleware |
string | /_next/static/ |
fsPath | Defines the file system path from where the express-static-gzip middleware will serve the compressed files. |
string | .next/static/ |
All options from express-static-gzip options are also available.
Default options
{
fsPath: '/.next/static/',
requestPath: '/_next/static/',
orderPreference: ['br', 'gzip'],
enableBrotli: true,
serveStatic: {
maxAge: 365 * 24 * 60 * 60 * 1000, // 1 year
immutable: true,
}
}
Tests
Any parameter passed to the test
command, is passed down to Jest.
$ npm test
$ npm test -- --watch # during development
After running the tests, a coverage folder will be created containing the test coverage info.
License
Released under the MIT License.