Skip to content

Instantly share code, notes, and snippets.

@mixdev
mixdev / README.md
Created December 22, 2022 01:53 — forked from babldev/README.md
NextJS Background Task

NextJS Background Task Example

An example of how to add a background task (e.g. a queue consumer) to an existing NextJS project.

Setup

  • Add your background task file
  • Add a new worker-tsconfig.json, specifically specifying "module": "commonjs" and targeting only the worker source files.
  • Add convenience functions for building & running to package.json

Then to build once:

//file: ./pages/api/checkHTTPMethod.ts
/* this code will allow only GET method requests on this route */
import { Middleware, use } from 'next-api-route-middleware';
import type { NextApiRequest, NextApiResponse } from 'next';
export const allowMethods = (allowedMethods: string[]): Middleware => {
return async function (req, res, next) {