Skip to content

Instantly share code, notes, and snippets.

@gregorym
Last active July 16, 2024 13:41
Show Gist options
  • Save gregorym/81adc4971274ce19d7b77278e4d8cf33 to your computer and use it in GitHub Desktop.
Save gregorym/81adc4971274ce19d7b77278e4d8cf33 to your computer and use it in GitHub Desktop.
Next.js - Child process
import { NextApiResponse } from 'next';
import { fork } from "child_process";
import path from 'path';
import fs from 'fs';
async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const paymentId = req.body.paymentId as string;
fork("./.next/server/collection.worker.js", [paymentId], { cwd: process.cwd() });
} catch(e) {
console.error(e);
return res.status(500).json({});
}
res.status(200).json({});
}
export default handler;
const paymentId = process.argv[2];
(async () => {
// DO WORK HERE
})();
// tslint:disable-next-line: no-empty
const noop = () => {};
export default noop;
const path = require('path');
const { merge } = require('webpack-merge');
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
if (isServer) {
return merge(config, {
entry () {
return config.entry().then((entry) => {
return Object.assign({}, entry, { 'collection.worker': path.resolve(process.cwd(), 'workers/collection.worker.ts') })
})
}
});
} else {
return config;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment