Last active
July 16, 2024 13:41
-
-
Save gregorym/81adc4971274ce19d7b77278e4d8cf33 to your computer and use it in GitHub Desktop.
Next.js - Child process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const paymentId = process.argv[2]; | |
(async () => { | |
// DO WORK HERE | |
})(); | |
// tslint:disable-next-line: no-empty | |
const noop = () => {}; | |
export default noop; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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