How Defer works
Defer is a zero-infrastructure Node.js background jobs platform.
Defer build your project from GitHub and execute your background functions on its infrastructure.
Take the following Next.js project:
- defer/
- importContacts.ts
- pages/
- api/
- import.ts
- styles/
- .env.example
- package.json
- tsconfig.json
As soon as a commit is pushed on your application, Defer will get the up-to-date source code of the corresponding Git Branch and compile the background functions from the defer/
folder (more information in “Builds”).
When your API calls a background function, as follows:
import type { NextApiRequest, NextApiResponse } from "next";
import importContacts from "../../defer/importContacts";
type Data = {
ok: boolean;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
await importContacts([]);
res.status(200).json({ ok: true });
}
The call to the importContacts()
function is actually replaced with a call to Defer's API to trigger an execution of importContacts()
on Defer's Platform:
For more information on how background functions get executed, please see “Executions”.
FAQ
Your background functions run synchronously in dev mode.
No, the @defer/client
apply a round-robin retry mechanism when contacting the Defer API.
Once received, the Defer jobs get persisted to disk.
You will get immediately get notified and your background functions will continue to run on the last successful build.
Unless configured, execution do not get retried by default. For more information on the executions lifecyle, please refer to “Executions”.