From d4a8856fa17a7a0a864c0d9f1b6e257647519649 Mon Sep 17 00:00:00 2001 From: Finn Woelm Date: Sat, 31 Oct 2020 22:47:08 +0100 Subject: [PATCH] Fix function timeout for API endpoint As described in https://github.com/netlify/next-on-netlify/issues/66#issuecomment-719988804 --- pages/api/test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pages/api/test.js b/pages/api/test.js index f600064..a80c4b5 100644 --- a/pages/api/test.js +++ b/pages/api/test.js @@ -1,6 +1,20 @@ import admin from "firebase-admin"; const test = async (req, res) => { + // Get function context to avoid timeout + // See: https://github.com/netlify/next-on-netlify/issues/66#issuecomment-719988804 + const functionContext = req?.functionContext; + + // If we are currently in a Netlify function (deployed on netlify.app or + // locally with netlify dev), do not wait for empty event loop. + // See: https://stackoverflow.com/a/39215697/6451879 + // Skip during next dev. + if (functionContext) { + console.log("Setting callbackWaitsForEmptyEventLoop: false"); + functionContext.callbackWaitsForEmptyEventLoop = false; + } + + // Initialize firebase app if (!admin.apps.length) { admin.initializeApp({ credential: admin.credential.cert({