const { onRequest } = require('firebase-functions/v2/https');
const next = require('next');

const app = next({
  dev: false,
  conf: { distDir: '.next' },
});

const handle = app.getRequestHandler();

// Prepare the app on module load
const prepared = app.prepare();

exports.nextjsFunc = onRequest(
  {
    region: 'us-central1',
    memory: '2GiB',
    timeoutSeconds: 300,
    maxInstances: 10,
    minInstances: 0,
  },
  async (req, res) => {
    await prepared;
    return handle(req, res);
  }
);
