From 39dae0aaff4c323553cb66d1d8dd80f70c98953e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 26 Nov 2020 11:25:09 -0500 Subject: [PATCH] fix: #8955, popstate to purged topic should go to homepage --- public/src/ajaxify.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index 76b1a545ba..32bcfdf8da 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -423,16 +423,34 @@ ajaxify = window.ajaxify || {}; $(document).ready(function () { $(window).on('popstate', function (ev) { ev = ev.originalEvent; + let url = ev.state.url; + const execute = function () { + ajaxify.go(url, function () { + $(window).trigger('action:popstate', { url: url }); + }, true); + }; if (ev !== null && ev.state) { - if (ev.state.url === null && ev.state.returnPath !== undefined) { + if (url === null && ev.state.returnPath !== undefined) { window.history.replaceState({ url: ev.state.returnPath, }, ev.state.returnPath, config.relative_path + '/' + ev.state.returnPath); - } else if (ev.state.url !== undefined) { - ajaxify.go(ev.state.url, function () { - $(window).trigger('action:popstate', { url: ev.state.url }); - }, true); + } else if (url !== undefined) { + if (url.startsWith('topic/')) { + // Check topic exists + fetch(`${config.relative_path}/${url}`, { + method: 'HEAD', + cache: 'no-cache', + }).then((res) => { + if (res.status === 404) { + url = ''; + } + + execute(); + }); + } else { + execute(); + } } } });