diff --git a/client/state/routing/middleware.js b/client/state/routing/middleware.js index ec87e52ee90a29..2c1638b4fb6503 100644 --- a/client/state/routing/middleware.js +++ b/client/state/routing/middleware.js @@ -3,35 +3,48 @@ */ import debugFactory from 'debug'; import page from 'page'; -import store from 'store'; /** * Internal dependencies */ +import localforage from 'lib/localforage'; +import { isOutsideCalypso } from 'lib/url'; import { ROUTE_SET } from 'state/action-types'; const debug = debugFactory( 'calypso:restore-last-location' ); +const LAST_PATH = 'last_path'; -let hasInitialized = false; +export const restoreLastLocation = () => { + let hasInitialized = false; -export const restoreLastLocation = () => ( next ) => ( action ) => { - if ( action.type === ROUTE_SET && action.path ) { - const lastPath = store.get( 'last_path' ); - - if ( ! hasInitialized && lastPath && lastPath !== '/' && action.path === '/' ) { - debug( 'redir to', action.path ); - page( lastPath ); - } else { - debug( 'saving', action.path ); - store.set( 'last_path', action.path ); - } - - if ( ! hasInitialized ) { - hasInitialized = true; + return ( next ) => ( action ) => { + if ( action.type !== ROUTE_SET || ! action.path ) { + return next( action ); } - } - return next( action ); + localforage.getItem( LAST_PATH ).then( + ( lastPath ) => { + if ( ! hasInitialized && + lastPath && lastPath !== '/' && + action.path === '/' && + ! isOutsideCalypso( lastPath ) ) { + debug( 'redir to', lastPath ); + page( lastPath ); + } else if ( action.path !== lastPath && + ! isOutsideCalypso( action.path ) ) { + debug( 'saving', action.path ); + localforage.setItem( LAST_PATH, action.path ); + } + + if ( ! hasInitialized ) { + hasInitialized = true; + } + + return next( action ); + }, + () => next( action ) + ); + }; }; export default restoreLastLocation;