I'm using Async/Await in a firebase function as follows
const functions = require("firebase-functions");
const puppeteer = require('puppeteer');
async function getHtml(url) {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox',]
});
const page = await browser.newPage();
await page.goto(url,
{ waitUntil: ['networkidle0', 'networkidle2', 'load', 'domcontentloaded'] });
const k = await page.content()
await browser.close();
return k
};
// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started
exports.api = functions.runWith({ memory: '512MB' })
.https.onRequest((request, response) => {
// console.log(request.query.url)
functions.logger.info("Hello logs!", { structuredData: true });
getHtml(request.query.url)
.then(function (res) {
response.send(res);
console.log(res)
})
.catch(function (err) {
response.send(err);
})
})
;
This fails the pre-deploy with the error Parsing error: Unexpected token function
for the line async function getHtml(url) {
I just assumed that it's an issue with the eslint which is why I removed the predeploy "predeploy": [ "npm --prefix \"$RESOURCE_DIR\" run lint"]
in the firebase json which helped me deploy successfully but returned an empty response on testing out the deployed code.
Everything on my local system works flawlessly with firebase serve
Any help would be much appriciated!
Edit:
Steps to reproduce
- run
firebase init
in a directory - Select
functions...
,Javascript
and (optionally)select no for eslint - install puppeteer using npm and paste the code above in the
/functions/index.js
file - run
firebase serve
and test the code with a url parameter on your local system. This will respond with a rendered html page. - Run
firebase deploy
Now, on going to the hosted URL and appending a url parameter should just return {}
instead of the html