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

@gasket/plugin-nextjs

v7.0.7

Published

Adds Next support to your application

Downloads

1,374

Readme

@gasket/plugin-nextjs

This plugin adds Next.js to your application.

Installation

npm i @gasket/plugin-nextjs

Update your gasket file plugin configuration:

// gasket.js

+ import pluginNextjs from '@gasket/plugin-nextjs';

export default makeGasket({
  plugins: [
+   pluginNextjs
  ]
});

Adding a Sitemap

When creating a new application with this plugin, you will be prompted with a question in the CLI asking if you would like to add a sitemap to your application.

Answering yes to this question will install next-sitemap as a dependency, generate a next-sitemap.config.js file, and add a sitemap npm script to your package.json. next-sitemap is an npm package that generates sitemaps and a robots.txt file for Next.js applications. Learn more by reading the next-sitemap docs.

Configuration

  • Instead of adding a dedicated next.config.js, the nextConfig property within gasket.js is used. Everything you can configure in the next.config can be added here.

It is also possible for apps to config Next.js using the gasket.js file. To do so, specify a nextConfig object property in the same form as what you would set for custom configurations or using Next.js plugins.

For general Webpack configurations, it is recommend to utilize features of the Gasket webpack plugin.

Example configuration

export default makeGasket({
  plugins: [
    pluginNextjs
  ]
  nextConfig: {
    poweredByHeader: false,
    useFileSystemPublicRoutes: false
  }
});

Example with plugins

import withSass from '@zeit/next-sass';
import withCss from '@zeit/next-css';

export default makeGasket({
  plugins: [
    pluginNextjs
  ]
  nextConfig: withCss(
    withSass({
      /* config options here */
    })
  )
});

Internationalized Routing

When using this plugin along with @gasket/plugin-intl to determine and provide locale files, be sure to set the i18n config for defaultLocale and supported locales in the intl plugin config, instead of the nextConfig. This will be used by the Gasket Intl plugin, and also passed along with the Next config for i18n routing.

// gasket.js
export default makeGasket({
  intl: {
+    defaultLocale: 'en-US',
+    locales: ['en-US', 'fr', 'nl-NL']
  },
  nextConfig: {
    i18n: {
-    locales: ['en-US', 'fr', 'nl-NL'],
-    defaultLocale: 'en-US',
    domains: [
      {
        domain: 'example.com',
        defaultLocale: 'en-US',
      },
      {
        domain: 'example.nl',
        defaultLocale: 'nl-NL',
      },
      {
        domain: 'example.fr',
        defaultLocale: 'fr',
      },
    ],
    }
  }
});

Also note when using @gasket/plugin-intl to determine the locale, that the NEXT_LOCALE cookie will have no effect. You can, of course, hook the intlLocale lifecycle in an app to enable that behavior if desired.

Actions

getNextConfig

This action returns the current nextConfig object which allows plugins to configure the Next.js application.

Place this in a next.config.js file which Next.js will load.

// next.config.js

import gasket from './gasket.js';
export default gasket.actions.getNextConfig();

getNextRoute

This action is intended for use in server contexts where you need to know how a request will route to a Next.js page. This async function returns null if the manifest could not be parsed or if the requested URL does not match a route. If a match is found, an object with these properties is returned:

| Property | Type | Description | |----------|------|-------------| | page | String | The page name/path used to identify a page within next.js | | regex | RegExp | The regular expression that matches URLs to the page | | routeKeys | Object | For dynamic routes the mapping of URL placeholders to parsed route parameters | | namedRegex | RegExp | Like regex, but named capturing groups are included to populate dynamic routing parameters |

import gasket from '../gasket.js';

async function someMiddleware(req, res, next) {
  const route = await gasket.actions.getNextRoute();
  if (route) {
    const { groups } = req.url.match(route.namedRegex);
    console.log(`Matched ${route.page} with parameters`, groups);
  }

  next();
}

Lifecycles

next

Executed when the next server has been created. It will receive a reference to the created next instance.

export default {
  name: 'sample-plugin',
  hooks: {
    /**
     * Modify the Next app instance
     * @param  {Gasket} gasket The Gasket API
     * @param  {Next} next Next app instance
     */
    next: function next(gasket, next) {
      next.setAssetPrefix('https://my.cdn.com/dir/');
    }
  }
};

nextConfig

Executed before the next server has been created. It will receive a reference to the next config. This will allow you to modify the next config before the next server is created.

export default {
  name: 'sample-plugin',
  hooks: {
    /**
     * Modify the Next config
     * @param  {Gasket} gasket The Gasket API
     * @param  {Object} config Next config
     * @returns {Object} config
     */
    nextConfig(gasket, config) {
      return {
        ...config,
        modification: 'newValue'
      };
    }
  }
};

nextExpress

Provides access to both next and express instances which allows next.render calls in express-based routes.

export default {
  name: 'sample-plugin',
  hooks: {
    nextExpress: function (gasket, { next, express }) {
      express.post('/contact-form', (req, res) => {
        // DO STUFF WITH RECEIVED DATA
        //
        // And once we're done, we can `next.render` to render the `pages/thanks`
        // file as a response.
        next.render(req, res, '/thanks', req.params);
      });
    }
  }
};

nextFastify

Provides access to both next and fastify instances which allows next.render calls in express-based routes.

export default {
  name: 'sample-plugin',
  hooks: {
    nextFastify: function (gasket, { next, fastify }) {
      fastify.post('/contact-form', (req, res) => {
        // DO STUFF WITH RECEIVED DATA
        //
        // And once we're done, we can `next.render` to render the `pages/thanks`
        // file as a response.
        next.render(req, res, '/thanks', req.params);
      });
    }
  }
};

nextPreHandling

Enables execution of custom logic just prior to an HTTP request being handed to next.js for processing. Hooks receive the request, response, and next server (not to be confused with the next function used by express-like middleware):

export default {
  name: 'sample-plugin',
  hooks: {
      async nextPreHandling(gasket, {
          req,
          res,
          nextServer
      }) {
          await doPreRenderingLogic(req, res);
      }
  }
};

License

MIT