/* eslint-disable max-len */
const BotDetector = require("device-detector-js/dist/parsers/bot");
const fs = require("fs");
const DEBUG_BOT = true;
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
// Main firebase function called on firebase.json rewrites rules
exports.dynamicMetaTagsUpdate = functions.https.onRequest(async (request, response) => {
let html = fs.readFileSync("../web/index.html", "utf8");
const botDetector = new BotDetector();
const userAgent = request.headers["user-agent"].toString();
const bot = botDetector.parse(userAgent);
if (bot || DEBUG_BOT) {
try {
const { postID } = request.query;
const postUrl = 'http://___________.fr/blog/post?postID=' + postID;
const docRef = db.collection("posts").doc(postID);
const postData = docRef.get().then((snapshot) => {
if (snapshot.exists()) {
const object = {
postUrl: postUrl,
postTitle: 'Article de ____________.fr ref=' + id + ' dans la catégorie ' + snapshot.child("category").val(),
postDescription: snapshot.child("description").val(),
postImageUrl: snapshot.child("postImageUrl").val()[0],
};
return object;
} else {
}
}).catch((error) => {
console.error(error);
});
const htmlData = await postData;
// HTML to return with updated open graph meta tags
html = `
<!doctype html>
<html lang="en">
<head>
<base href="/">
<meta name="theme-color" content="#313131">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="_____">
<link rel="apple-touch-icon" href="icons/Icon-192.p
<title>${htmlData.postTitle}</title>
<meta name="description" content="${htmlData.postDescription}">
<meta name="title" content="${htmlData.postTitle}">
<meta property="og:locale" content="fr_FR" />
<meta property="og:type" content="website" />
<meta property="og:title" content="${htmlData.postTitle}" />
<meta property="og:description" content="${htmlData.postDescription}" />
<meta property="og:image" content="${htmlData.postImageUrl}" />
<meta property="og:url" content="${htmlData.postUrl}" />
<meta name="twitter:card" content="${htmlData.postDescription}" />
<meta name="twitter:creator" content="@______" />
<meta name="twitter:title" content="${htmlData.postTitle}" />
<meta name="twitter:description" content="${htmlData.postDescription}" />
<link rel="manifest" href="manifest.json">
<script src="custom.js"defer></script>
<script src="sharerbox.js"defer></script>
</head>
<body>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('flutter-first-frame', function () {
navigator.serviceWorker.register('flutter_service_worker.js');
});
}
</script>
<script>window.flutterfire_web_sdk_version = '9.9.0';</script>
<script>
const firebaseConfig = {
apiKey: "____________",
authDomain: "__________",
projectId: "________",
messagingSenderId: "______",
appId: "___________",
measurementId: "__________"
}
firebase.initializeApp(firebaseConfig);
</script>
<script src="main.dart.js" type="application/javascript"></script>
</body>
</html>
`;
return response.send(html);
} catch (e) {
console.log(e);
return response.send(html);
}
}
});