Skip to content

Instantly share code, notes, and snippets.

@pejalo
Created June 1, 2017 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pejalo/080a6dbf0a5cb1c0ce5e5528c2523265 to your computer and use it in GitHub Desktop.
Save pejalo/080a6dbf0a5cb1c0ce5e5528c2523265 to your computer and use it in GitHub Desktop.
Firebase function for dynamic routing via redirect (essentials)
const admin = require('firebase-admin');
function buildHtmlWithPost (post) {
const string = '<!DOCTYPE html><head>' \
'<title>' + post.title + ' | Example Website</title>' \
'<meta property="og:title" content="' + post.title + '">' \
'<meta property="twitter:title" content="' + post.title + '">' \
'<link rel="icon" href="https://example.com/favicon.png">' \
'</head><body>' \
'<script>window.location="https://example.com/?post=' + post.id + '";</script>' \
'</body></html>';
return string;
}
module.exports = function(req, res) {
const path = req.path.split('/');
const postId = path[2];
admin.database().ref('/posts').child(postId).once('value').then(snapshot => {
const post = snapshot.val();
post.id = snapshot.key;
const htmlString = buildHtmlWithPost(post);
res.status(200).end(htmlString);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment