Daniel Birowsky Popeski
Daniel Birowsky Popeski

Reputation: 9286

URL Rewrite based on User-Agent header

You can optimize content by delivering only Open Graph meta tags to the crawler and only the content itself to regular users. [source]

I'm trying to identify Facebot(Facebook's crawler) so I can serve it the meta tags. To do that, I'll need to be able to do rewrites based on User-Agent header.

I went through the Firebase documentation, but it seems to me that I can only do rewrites based on request url.

Another approach that comes to mind is to decide which content to serve within a firebase function. But I can't figure out how to fall back to index.html from a firebase function.

So in short: How would you serve the Open Graph tags just to Facebot using the Firebase ecosystem?

Upvotes: 21

Views: 1090

Answers (1)

tim
tim

Reputation: 181

For anyone still looking for a solution to this: Here is how I currently check whether a user agent is from facebook:

const agent = request.headers["user-agent"]

if (agent.toLowerCase().includes("facebook"))
    response.send(<payload>)
else
    response.redirect(<path-to-index.html>)

The request / response variables are the ones, received through functions.https.onRequest((request, response) => { ... }). For more information, check the Firebase Docs and Express Docs.

Upvotes: 1

Related Questions