DEV: Update pushState and replaceState development patches (#12863)

This updates the preview_theme_id preservation logic to use more recent, robust, browser APIs. It also adds support for preserving the `?pp=async-flamegraph` parameter which is proposed in https://github.com/MiniProfiler/rack-mini-profiler/pull/494
This commit is contained in:
David Taylor 2021-04-30 11:28:47 +01:00 committed by GitHub
parent 486550c6fe
commit c1f969135f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,31 +9,26 @@ export default {
initialize(container) { initialize(container) {
const messageBus = container.lookup("message-bus:main"); const messageBus = container.lookup("message-bus:main");
if ( // Preserve preview_theme_id=## and pp=async-flamegraph parameters across pages
window.history && const params = new URLSearchParams(window.location.search);
window.location.search.indexOf("?preview_theme_id=") === 0 const previewThemeId = params.get("preview_theme_id");
) { const flamegraph = params.get("pp") === "async-flamegraph";
// force preview theme id to always be carried along if (flamegraph || previewThemeId !== null) {
const themeId = parseInt( ["replaceState", "pushState"].forEach((funcName) => {
window.location.search.slice(18).split("&")[0], const originalFunc = window.history[funcName];
10
);
if (!isNaN(themeId)) {
const patchState = function (f) {
const patched = window.history[f];
window.history[f] = function (stateObj, name, url) { window.history[funcName] = (stateObj, name, rawUrl) => {
if (url.indexOf("preview_theme_id=") === -1) { const url = new URL(rawUrl, window.location);
const joiner = url.indexOf("?") === -1 ? "?" : "&"; if (previewThemeId !== null) {
url = `${url}${joiner}preview_theme_id=${themeId}`; url.searchParams.set("preview_theme_id", previewThemeId);
} }
if (flamegraph) {
url.searchParams.set("pp", "async-flamegraph");
}
return patched.call(window.history, stateObj, name, url); return originalFunc.call(window.history, stateObj, name, url.href);
};
}; };
patchState("replaceState"); });
patchState("pushState");
}
} }
// Custom header changes // Custom header changes