Skip to content

Instantly share code, notes, and snippets.

@chris-schra
Last active March 1, 2023 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-schra/4c26924bcc9d6229fb5a3720f9d50b17 to your computer and use it in GitHub Desktop.
Save chris-schra/4c26924bcc9d6229fb5a3720f9d50b17 to your computer and use it in GitHub Desktop.
Custom bootstrap for next js server
let entryModified = false;
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
if(dev && config.entry && !entryModified)
{
require('@babel/register')({
extensions: ['.js', '.jsx', '.ts', '.tsx']
})
entryModified = true;
const oldEntry = config.entry;
config.entry = async () => {
return require("./server/bootstrap").default().then().then(oldEntry)
}
}
},
}
{
"scripts": {
"start": "ts-node --project tsconfig.server.json server/index.ts"
}
}
// this is actually ./server/bootstrap.ts
const mongoose = require('mongoose');
const bootstrapServer = async () => {
// do your bootstrap stuff here
};
export default bootstrapServer;
// this is actually ./server/index.ts
bootstrapServer().then(() => {
console.log("server bootstrapped");
startServer({ dir }, port, host)
.then(async (app:any) => {
await app.prepare();
// NOTE: you can do your own logging implementation here
console.log(`server is running at ${host}:${port}`);
})
.catch((err:any) => {
console.error(err)
process.exit(1)
})
})
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"target": "ES2017"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment