Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Next.js API include a file to all api endpoints

Is it possible (and how) to add some kind of "bootstrap" file (file with side-effects) as a very first file to be included in all APIs in Next.js?

The original use-case is that I've winston logger in a file which I have to put in all API endpoints, but this is quite poor DX. I just want to say "hey, put this file before all endpoints, so I know (in this case logger) everything is setup".

Thanks

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

So, I've found the solution in a custom webpack configuration:

import withPlugins from 'next-compose-plugins';
import { patchWebpackConfig } from 'next-global-css';
import images from 'next-images';
import withTM from 'next-transpile-modules';
import { merge } from 'webpack-merge';

export default withPlugins([
    images,
    withTM([]),
], {
    swcMinify:                   true,
    images:                      {
        formats: ['image/avif', 'image/webp']
    },
    productionBrowserSourceMaps: false,
    // experimental:                {
    //  outputStandalone: true,
    // },
    webpack:                     (config, {
        webpack,
        buildId,
        isServer,
        ...options
    }) => {
        config.plugins.push(
            new webpack.DefinePlugin({
                'process.env.BUILD_ID': JSON.stringify(buildId),
            }),
        );
        if (isServer) {
            return merge(config, {
                entry() {
                    return config.entry().then(entry => {
                        // this is the interesting part: prefix entry with a bootstrap file, in my case logger side-effect file
                        Object.keys(entry).map(key => {
                            entry[key] = {import: ['./src/service/bootstrap.ts', ...entry[key]]};
                        });
                        return entry;
                    });
                }
            });
        }
        return patchWebpackConfig(config, options);
    },
    reactStrictMode:             true,
    staticPageGenerationTimeout: 15,
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!