Fix issue with sendBeacon request.

This commit is contained in:
Mike Cao 2022-03-10 19:01:33 -08:00
parent 48db7708de
commit 17790aa5a8
6 changed files with 23 additions and 22 deletions

View File

@ -16,7 +16,8 @@
"rules": {
"react/display-name": "off",
"react/react-in-jsx-scope": "off",
"react/prop-types": "off"
"react/prop-types": "off",
"import/no-anonymous-default-export": "off"
},
"globals": {
"React": "writable"

View File

@ -3,11 +3,6 @@ import { getSession } from './session';
import { getAuthToken } from './auth';
import { unauthorized, badRequest, serverError } from './response';
const corsOptions = {
origin: '*',
credentials: true,
};
export function createMiddleware(middleware) {
return (req, res) =>
new Promise((resolve, reject) => {
@ -20,7 +15,7 @@ export function createMiddleware(middleware) {
});
}
export const useCors = createMiddleware(cors(corsOptions));
export const useCors = createMiddleware(cors());
export const useSession = createMiddleware(async (req, res, next) => {
let session;

View File

@ -86,3 +86,11 @@ export async function getClientInfo(req, { screen }) {
return { userAgent, browser, os, ip, country, device };
}
export function getJsonBody(req) {
if (req.headers['content-type'].indexOf('text/plain') !== -1) {
return JSON.parse(req.body);
}
return req.body;
}

View File

@ -1,9 +1,9 @@
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';
import { getClientInfo } from 'lib/request';
import { getJsonBody, getClientInfo } from 'lib/request';
import { uuid, isValidUuid, parseToken } from 'lib/crypto';
export async function getSession(req) {
const { payload } = req.body;
const { payload } = getJsonBody(req);
if (!payload) {
throw new Error('Invalid request');

View File

@ -2,7 +2,7 @@ import isbot from 'isbot';
import ipaddr from 'ipaddr.js';
import { savePageView, saveEvent } from 'lib/queries';
import { useCors, useSession } from 'lib/middleware';
import { getIpAddress } from 'lib/request';
import { getJsonBody, getIpAddress } from 'lib/request';
import { ok, badRequest } from 'lib/response';
import { createToken } from 'lib/crypto';
import { removeTrailingSlash } from 'lib/url';
@ -39,10 +39,11 @@ export default async (req, res) => {
await useSession(req, res);
const {
body: { type, payload },
session: { website_id, session_id },
} = req;
const { type, payload } = getJsonBody(req);
let { url, referrer, event_type, event_value } = payload;
if (process.env.REMOVE_TRAILING_SLASH) {

View File

@ -47,7 +47,7 @@ import { removeTrailingSlash } from '../lib/url';
const post = (url, data, callback) => {
const req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.setRequestHeader('Content-Type', 'text/plain');
req.onreadystatechange = () => {
if (req.readyState === 4) {
@ -114,20 +114,16 @@ import { removeTrailingSlash } from '../lib/url';
const sendEvent = (value, type) => {
const payload = getPayload();
payload.event_type = type;
payload.event_value = value;
const blob = new Blob(
[
JSON.stringify({
type: 'event',
payload,
}),
],
{ type: 'application/json' },
);
const data = JSON.stringify({
type: 'event',
payload,
});
navigator.sendBeacon(`${root}/api/collect`, blob);
navigator.sendBeacon(`${root}/api/collect`, data);
};
const addEvents = node => {