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-export-api

v0.1.11

Published

Next.js API Routes adaptor for a Netlify static website

Downloads

4

Readme

Next Export API

Build status codecov

Installation

yarn add next-export-api

or

npm install next-export-api

Ensure to install it as a production dependency, not a development one.

Add next-export-api to the build script. You can use postbuild if you are using build on the package.json:

 {
   "name": "example",
   "scripts": {
     "build": "next build && next export",
+    "postbuild": "next-export-api",
     "dev": "next dev",
     "start": "next start"
   },
   "dependencies": {
     "next": "9.5.5",
     "react": "16.14.0",
     "react-dom": "16.14.0"
   }
 }

Configure netlify.toml to define functions directory:

[build]
  command = "yarn build"
  publish = "out"
  functions = "out/api"

Both publish and functions above are configurable, and will be respected by next-export-api command.

Please check the example folder for a ready to deploy sample project.

Motivation

Over a year ago Next.js released version 9 with API Routes support. Netlify has also long supported their vision of serverless functions. The issue? They have very diverging APIs. Next.js even states, in a caveats section, it's API Routes do not work with next export projects - the most straightforward way of deploying Next.js projects on Netlify. However, with a bit of adaptation from generated API Routes entrypoints files, it's possible to make them capable of running on Netlify functions environment.

Similar projects

This project is heavily inspired by...

The difference to these projects is next-export-api focuses in allowing the use of API Routs on a Next.js Static HTML Exported project.

How does it work

Similarly to next-on-netlify, this project takes advantage of Next.js own building system, and operates on it's generated code. As for API Routes, Next.js does generate a self-contained file for each /pages/api entrypoint, meaning we can import those files or of the foreseen context of a Next.js server.

The process is like the following:

  1. Next.js builds into the .next directory;

  2. Next.js exports static site into (configurable) out directory;

  3. next-export-api reads page manifests from the .next build, and...

    1. Creates one file for each API route under out/api

      These files are thin wrappers over Next.js original ones, only adapting for execution on AWS using well supported serverless-http.

    2. Creates _redirects rules for each of the API routes above, mapping /api/* calls to /.netlify/functions/* as expected by Netlify