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

next-to-netlify

v0.1.10

Published

Next.js API routes adaptor for Netlify

Downloads

47

Readme

Next to Netlify

Build status

Installation

yarn add next-to-netlify

or

npm install next-to-netlify

Ensure to install it not as a development dependency, as it is partially used on runtime code.

Motivation

Over a year ago Next.js released version 9 with API Routes support. Netlify has also supported their vision of serverless functions. The issue? They have very diverging APIs. Next.js even states API Routes do not work with next export in a caveats section, and it's understandable. However, with a bit of adaptation from generated API Routes entrypoints it's possible to make it play nicely to be deployed as Netlify functions.

API conflicts, their resolutions, and usage

✅ Function signature

Next.js functions are similar to middleware frameworks handlers such as express, for those familiar: it receives a req object, and a res object. No async or return needed: res has the API to send responses.

Netlify, however, exposes a slightly extended version of an AWS Lambda: it's arguments are an event and a context - optionally also a callback - and the return must follow a very specific format.

This distinction is not new, and many existing projects try to reduce the gap and ensure reusability of code from one format to the other. Most notably, serverless-http, which has being allowing express based applications to run on AWS.

Therefore, the first missing part for integrating Next.js API Routes and Netlify functions is to create an adaptor which can handle calls from both above APIs.

Here is an usage example:

import { adaptor } from 'next-to-netlify/adaptor'

export const handler = adaptor((req, res) => {
  res.status(200).send({ name: `Hello, ${req.query.name}` })
})

export default handler

Warning: notice that both handler and a default are exported from the file. This is necessary because while Next.js expect the handle to be exported as default, Netlify - following AWS Lambda convention - expects it to be exported as handler.

I've explored ways to reduce the need of this adaptation, and a Webpack loader is possibly a solution. It could not only wrap the default exposed handler, but also re-export it as handler. However, it would be more prone to errors and more complex in terms of abstraction and configuration.

✅ Function endpoints

Next.js has a defined convention on where to keep API Routes files, and how they become available through URL: files live under /pages/api, and URLs match /api/[name-of-function] format.

Netlify, in the other hand, expects you define the functions path in a netlify.toml (or via UI), and makes the functions available at /.netlify/functions/[name-of-function].

This can be solved in a couple of opinionated ways:

  1. Redirects config on Next.js from /.netlify/functions/* to /api/*
  2. Redirects on _redirects file from /api/* to /.netlify/functions/*
  3. Imperative use of an adapted endpoint

This module supports numbers 1 and 2 above via next.config.js:

const withNetlify = require('next-to-netlify/config')

module.exports = withNetlify({})

or, with next-compose-plugins:

const withPlugins = require('next-compose-plugins')
const netlify = require('next-to-netlify/config')
const sass = require('@zeit/next-sass')

module.exports = withPlugins([[netlify], [sass]])

This is all that's necessary for option 1 from before, and now any API call from the application code would have to be sent to Netlify URL pattern.

For option 3, besides the above config, you can import the dynamic endpoint as follows:

import { api } from 'next-to-netlify'

fetch(`${api}/name-of-function`) // usage

❌ Dynamic API Routes

Thought theoretically possible, I didn't explore so far with the possibility of using Next.js Dynamic API Routes on Netlify. It should be fine, as Next.js uses the dynamic parts as simple query params in the end.