From 8eaacfea18545fcb3537f2e5466acf1032f89d35 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Fri, 15 Feb 2019 11:59:39 -0800 Subject: [PATCH] use text/plain on /api/metrics --- app/metrics.js | 8 ++++++-- package.json | 1 + server/routes/index.js | 5 +++-- server/routes/metrics.js | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/metrics.js b/app/metrics.js index 3c02e2a7..0dcabe10 100644 --- a/app/metrics.js +++ b/app/metrics.js @@ -41,13 +41,17 @@ function submitEvents() { events }) ], - { type: 'application/json' } + { type: 'text/plain' } // see http://crbug.com/490015 ); events.splice(0); if (!navigator.sendBeacon) { return; } - navigator.sendBeacon('/api/metrics', data); + try { + navigator.sendBeacon('/api/metrics', data); + } catch (e) { + console.error(e); + } } async function addEvent(event_type, event_properties) { diff --git a/package.json b/package.json index 7a17b6cc..21ce59b3 100644 --- a/package.json +++ b/package.json @@ -136,6 +136,7 @@ "dependencies": { "@google-cloud/storage": "^2.4.2", "aws-sdk": "^2.400.0", + "body-parser": "^1.18.3", "choo": "^6.12.1", "cldr-core": "^34.0.0", "convict": "^4.4.1", diff --git a/server/routes/index.js b/server/routes/index.js index 3bc99b87..ce6d6f52 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -1,5 +1,5 @@ const crypto = require('crypto'); -const express = require('express'); +const bodyParser = require('body-parser'); const helmet = require('helmet'); const uaparser = require('ua-parser-js'); const storage = require('../storage'); @@ -69,7 +69,8 @@ module.exports = function(app) { res.set('Cache-Control', 'no-cache'); next(); }); - app.use(express.json()); + app.use(bodyParser.json()); + app.use(bodyParser.text()); app.get('/', language, pages.index); app.get('/oauth', language, pages.blank); app.get('/legal', language, pages.legal); diff --git a/server/routes/metrics.js b/server/routes/metrics.js index 0d8015c1..059e330e 100644 --- a/server/routes/metrics.js +++ b/server/routes/metrics.js @@ -2,7 +2,7 @@ const { sendBatch, clientEvent } = require('../amplitude'); module.exports = async function(req, res) { try { - const data = req.body; + const data = JSON.parse(req.body); // see http://crbug.com/490015 const deltaT = Date.now() - data.now; const events = data.events.map(e => clientEvent(