mirror of
https://github.com/kremalicious/umami.git
synced 2025-01-25 17:51:11 +01:00
Fix issue with sendBeacon request.
This commit is contained in:
parent
48db7708de
commit
17790aa5a8
@ -16,7 +16,8 @@
|
|||||||
"rules": {
|
"rules": {
|
||||||
"react/display-name": "off",
|
"react/display-name": "off",
|
||||||
"react/react-in-jsx-scope": "off",
|
"react/react-in-jsx-scope": "off",
|
||||||
"react/prop-types": "off"
|
"react/prop-types": "off",
|
||||||
|
"import/no-anonymous-default-export": "off"
|
||||||
},
|
},
|
||||||
"globals": {
|
"globals": {
|
||||||
"React": "writable"
|
"React": "writable"
|
||||||
|
@ -3,11 +3,6 @@ import { getSession } from './session';
|
|||||||
import { getAuthToken } from './auth';
|
import { getAuthToken } from './auth';
|
||||||
import { unauthorized, badRequest, serverError } from './response';
|
import { unauthorized, badRequest, serverError } from './response';
|
||||||
|
|
||||||
const corsOptions = {
|
|
||||||
origin: '*',
|
|
||||||
credentials: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
export function createMiddleware(middleware) {
|
export function createMiddleware(middleware) {
|
||||||
return (req, res) =>
|
return (req, res) =>
|
||||||
new Promise((resolve, reject) => {
|
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) => {
|
export const useSession = createMiddleware(async (req, res, next) => {
|
||||||
let session;
|
let session;
|
||||||
|
@ -86,3 +86,11 @@ export async function getClientInfo(req, { screen }) {
|
|||||||
|
|
||||||
return { userAgent, browser, os, ip, country, device };
|
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;
|
||||||
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';
|
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';
|
import { uuid, isValidUuid, parseToken } from 'lib/crypto';
|
||||||
|
|
||||||
export async function getSession(req) {
|
export async function getSession(req) {
|
||||||
const { payload } = req.body;
|
const { payload } = getJsonBody(req);
|
||||||
|
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
throw new Error('Invalid request');
|
throw new Error('Invalid request');
|
||||||
|
@ -2,7 +2,7 @@ import isbot from 'isbot';
|
|||||||
import ipaddr from 'ipaddr.js';
|
import ipaddr from 'ipaddr.js';
|
||||||
import { savePageView, saveEvent } from 'lib/queries';
|
import { savePageView, saveEvent } from 'lib/queries';
|
||||||
import { useCors, useSession } from 'lib/middleware';
|
import { useCors, useSession } from 'lib/middleware';
|
||||||
import { getIpAddress } from 'lib/request';
|
import { getJsonBody, getIpAddress } from 'lib/request';
|
||||||
import { ok, badRequest } from 'lib/response';
|
import { ok, badRequest } from 'lib/response';
|
||||||
import { createToken } from 'lib/crypto';
|
import { createToken } from 'lib/crypto';
|
||||||
import { removeTrailingSlash } from 'lib/url';
|
import { removeTrailingSlash } from 'lib/url';
|
||||||
@ -39,10 +39,11 @@ export default async (req, res) => {
|
|||||||
await useSession(req, res);
|
await useSession(req, res);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
body: { type, payload },
|
|
||||||
session: { website_id, session_id },
|
session: { website_id, session_id },
|
||||||
} = req;
|
} = req;
|
||||||
|
|
||||||
|
const { type, payload } = getJsonBody(req);
|
||||||
|
|
||||||
let { url, referrer, event_type, event_value } = payload;
|
let { url, referrer, event_type, event_value } = payload;
|
||||||
|
|
||||||
if (process.env.REMOVE_TRAILING_SLASH) {
|
if (process.env.REMOVE_TRAILING_SLASH) {
|
||||||
|
@ -47,7 +47,7 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
const post = (url, data, callback) => {
|
const post = (url, data, callback) => {
|
||||||
const req = new XMLHttpRequest();
|
const req = new XMLHttpRequest();
|
||||||
req.open('POST', url, true);
|
req.open('POST', url, true);
|
||||||
req.setRequestHeader('Content-Type', 'application/json');
|
req.setRequestHeader('Content-Type', 'text/plain');
|
||||||
|
|
||||||
req.onreadystatechange = () => {
|
req.onreadystatechange = () => {
|
||||||
if (req.readyState === 4) {
|
if (req.readyState === 4) {
|
||||||
@ -114,20 +114,16 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
|
|
||||||
const sendEvent = (value, type) => {
|
const sendEvent = (value, type) => {
|
||||||
const payload = getPayload();
|
const payload = getPayload();
|
||||||
|
|
||||||
payload.event_type = type;
|
payload.event_type = type;
|
||||||
payload.event_value = value;
|
payload.event_value = value;
|
||||||
|
|
||||||
const blob = new Blob(
|
const data = JSON.stringify({
|
||||||
[
|
type: 'event',
|
||||||
JSON.stringify({
|
payload,
|
||||||
type: 'event',
|
});
|
||||||
payload,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
{ type: 'application/json' },
|
|
||||||
);
|
|
||||||
|
|
||||||
navigator.sendBeacon(`${root}/api/collect`, blob);
|
navigator.sendBeacon(`${root}/api/collect`, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
const addEvents = node => {
|
const addEvents = node => {
|
||||||
|
Loading…
Reference in New Issue
Block a user