Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 534
Daniel Birowsky Popeski Avatar asked Nov 23 '25 15:11

Daniel Birowsky Popeski


1 Answers

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.

like image 65
tim Avatar answered Nov 27 '25 12:11

tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!