diff --git a/assets/js/counter.js b/assets/js/counter.js index 54a77a75..4742697f 100644 --- a/assets/js/counter.js +++ b/assets/js/counter.js @@ -18,14 +18,22 @@ fetch("/api/hits?slug=" + slug) .then((response) => response.json()) .then((data) => { - // finally inject the hits and hide the loading spinner - var spinner = document.getElementById("hit-spinner"); - var counter = document.getElementById("hit-counter"); + if (typeof data.hits !== "undefined") { + // finally inject the hits and hide the loading spinner + var spinner = document.getElementById("hit-spinner"); + var counter = document.getElementById("hit-counter"); - spinner.style.display = "none"; - wrapper.title = data.pretty_hits + " " + data.pretty_unit; - counter.appendChild(document.createTextNode(data.pretty_hits)); + spinner.style.display = "none"; + wrapper.title = data.pretty_hits + " " + data.pretty_unit; + counter.appendChild(document.createTextNode(data.pretty_hits)); + } else { + // something went horribly wrong, initiate coverup + wrapper.style.display = "none"; + } }) - .catch((error) => {}); + .catch((error) => { + // something went horribly wrong, initiate coverup + wrapper.style.display = "none"; + }); } })(); diff --git a/functions/hits.js b/functions/hits.js index e2f6d581..f662206f 100644 --- a/functions/hits.js +++ b/functions/hits.js @@ -1,3 +1,5 @@ +"use strict"; + const faunadb = require("faunadb"), q = faunadb.query; const numeral = require("numeral"); @@ -9,12 +11,8 @@ require("dotenv").config(); require("encoding"); exports.handler = async (event, context) => { - const { slug } = event.queryStringParameters; - const client = new faunadb.Client({ - secret: process.env.FAUNADB_SERVER_SECRET, - }); - // some rudimentary error handling + const { slug } = event.queryStringParameters; if (!slug || slug === "/") { return { statusCode: 400, @@ -24,51 +22,62 @@ exports.handler = async (event, context) => { }; } - const result = await client.query( - q.Let( - { - match: q.Match(q.Index("hits_by_slug"), slug), - }, - q.If( - q.Exists(q.Var("match")), - q.Let( - { - ref: q.Select("ref", q.Get(q.Var("match"))), - hits: q.ToInteger(q.Select("hits", q.Select("data", q.Get(q.Var("match"))))), - }, - q.Update(q.Var("ref"), { + try { + const client = new faunadb.Client({ + secret: process.env.FAUNADB_SERVER_SECRET, + }); + + const result = await client.query( + q.Let( + { + match: q.Match(q.Index("hits_by_slug"), slug), + }, + q.If( + q.Exists(q.Var("match")), + q.Let( + { + ref: q.Select("ref", q.Get(q.Var("match"))), + hits: q.ToInteger(q.Select("hits", q.Select("data", q.Get(q.Var("match"))))), + }, + q.Update(q.Var("ref"), { + data: { + hits: q.Add(q.Var("hits"), 1), + }, + }) + ), + q.Create(q.Collection("hits"), { data: { - hits: q.Add(q.Var("hits"), 1), + slug: slug, + hits: 1, }, }) - ), - q.Create(q.Collection("hits"), { - data: { - slug: slug, - hits: 1, - }, - }) + ) ) - ) - ); + ); - client.close(); + // send client the new hit count + return { + statusCode: 200, + headers: { + "Cache-Control": "private, no-cache, no-store, must-revalidate", + Expires: "0", + Pragma: "no-cache", + }, + body: JSON.stringify({ + slug: result.data.slug, + hits: result.data.hits, + pretty_hits: numeral(result.data.hits).format("0,0"), + pretty_unit: pluralize("hit", result.data.hits), + }), + }; + } catch (error) { + console.error(error); - const hits = result.data.hits; - - // send client the new hit count - return { - statusCode: 200, - headers: { - "Cache-Control": "private, no-cache, no-store, must-revalidate", - Expires: "0", - Pragma: "no-cache", - }, - body: JSON.stringify({ - slug: slug, - hits: hits, - pretty_hits: numeral(hits).format("0,0"), - pretty_unit: pluralize("hit", hits), - }), - }; + return { + statusCode: 400, + body: JSON.stringify({ + message: error.message, + }), + }; + } };