From 349608fe497748a9d8d0d06d60e084da997a4e77 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sun, 25 Feb 2018 15:19:38 +0100 Subject: [PATCH] Do feature detection instead of disabling iframes. (#3880) --- lib/router/router.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/router/router.js b/lib/router/router.js index 30acfefd..36bd3b26 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -7,6 +7,13 @@ import PQueue from '../p-queue' import { loadGetInitialProps, getURL, warn, execOnce } from '../utils' import { _rewriteUrlForNextExport } from './' +const historyUnavailableWarning = execOnce(() => { + warn(`Warning: window.history is not available.`) +}) +const historyMethodWarning = execOnce((method) => { + warn(`Warning: window.history.${method} is not available`) +}) + export default class Router { constructor (pathname, query, as, { pageLoader, Component, ErrorComponent, err } = {}) { // represents the current component key @@ -185,9 +192,17 @@ export default class Router { } changeState (method, url, as, options = {}) { - if (window.frameElement) { - execOnce(warn)(`Warning: You're using Next.js inside an iFrame. Browser history is disabled.`) - } else if (method !== 'pushState' || getURL() !== as) { + if (typeof window.history === 'undefined') { + historyUnavailableWarning() + return + } + + if (typeof window.history[method] === 'undefined') { + historyMethodWarning(method) + return + } + + if (method !== 'pushState' || getURL() !== as) { window.history[method]({ url, as, options }, null, as) } }