From 7d3bc66a503e3b8e1c70e92464692de244b23b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A9=E7=A9=BABlond?= Date: Mon, 1 Feb 2021 11:50:39 +0800 Subject: [PATCH] fix pjax, tested on my website MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 天空Blond --- tracker/index.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/tracker/index.js b/tracker/index.js index fc8458b0..8f58fae3 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -134,19 +134,25 @@ import { removeTrailingSlash } from '../lib/url'; const handlePush = (state, title, url) => { removeEvents(); + // url is not guaranteed + // thus calling `url.toString()` might causes exception, then break the pjax update + // track only if url is validate + if (url) { + currentRef = currentUrl; + const newUrl = url.toString(); - currentRef = currentUrl; - const newUrl = url.toString(); - - if (newUrl.substring(0, 4) === 'http') { - currentUrl = '/' + newUrl.split('/').splice(3).join('/'); - } else { - currentUrl = newUrl; + if (newUrl.substring(0, 4) === 'http') { + currentUrl = '/' + newUrl.split('/').splice(3).join('/'); + } else { + currentUrl = newUrl; + } + // this function under some framework might be called multiple times + // thus only track if currentUrl and currentRef are not same + if (currentUrl !== currentRef) { + trackView(currentUrl, currentRef); + setTimeout(addEvents, 300); + } } - - trackView(currentUrl, currentRef); - - setTimeout(addEvents, 300); }; /* Global */