Firebase function not returning edited index HTML content

Viewed 164

I have a Vue app deployed on Firebase Hosting. Until recently we had no need to add dynamic Open Graph tags to it but now we do, and the path we took is a Firebase Function that executes for all the paths when the app should be served.

{
  "hosting": {
    "public": "dist/spa",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "host"
      }
    ]
  },
  "functions": {
    "source": "functions"
  }
}

You can notice in the firebase.json file above that there is a function called host that is executed for all paths. And this is working fine, I can see the execution of the function in the console, I also checked the logs and it is modifying the index.html file as expected, but at the end it looks like the original index.html (which is found under dist/spa) file is returned to the user. The code is bellow:

const functions = require('firebase-functions');
const fs = require('fs');

exports.host = functions.https.onRequest((req, res) => {
  let indexHTML = fs.readFileSync('./hosting/index.html').toString();

  console.log('original: ', indexHTML);

  // const userAgent = req.headers['user-agent'].toLowerCase();
  const ogPlaceholder = '<meta name=functions-insert-dynamic-og>';

  // Cache the response for 10 minutes
  res.set('Cache-Control', 'public, max-age=600, s-maxage=1200');
  indexHTML = indexHTML.replace(ogPlaceholder, getOpenGraph());

  console.log('modified: ', indexHTML);

  res.status(200).send(indexHTML);
});

const desc =
  'Desc here';
const imageURL =
  'imageUrlHere';
const title = 'Title Here';

const getOpenGraph = () => {
  return `<meta property="og:title" content="${title}">
          <meta property="og:site_name" content="SiteName">
          <meta property="og:url" content="URL">
          <meta property="og:description" content="${desc}">
          <meta property="og:type" content="website">
          <meta property="og:image" content="${imageURL}">`;
};

During our build process the Vue template index file is copied over to function/hosting. That file is read using fs and a placeholder string is replaced with the appropriate Open Graph tags. But the source that is served is still the original one...

1 Answers

You might try adding a second root folder to your project so that it no longer builds your single root folder into lib/ and instead builds it in the subdirectory. Everything worked again once you modified firebase.json with the new URL to index.js.

Before executing Firebase deploy, you can try running npm run build in your functions folder to confirm that your source code is compiled.

If you want to look at the real logs to figure out what's wrong, use the command firebase functions:log. This command will display the exact problem, which could be as simple as a missing firebase.json.

Another suggestion would be to delete firebase.json and start over.

Related