From 73f7f5f164e98bb3034babd5679b0ebca3cd51c0 Mon Sep 17 00:00:00 2001 From: Matt Kunze Date: Tue, 17 Nov 2015 21:55:11 -0700 Subject: [PATCH] Add replacePath functionality Pretty much the same as updatePath except it replaces the current path in history rather than appending a new entry Fixes jlongster/redux-simple-router#32 --- README.md | 5 +++++ src/index.js | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 61e9049..520ae79 100644 --- a/README.md +++ b/README.md @@ -150,3 +150,8 @@ to this and all future URL changes. Pass `false` to make it start reacting again. This is useful if replaying snapshots while using the `forceRefresh` option of the browser history which forces full reloads. It's a rare edge case. + +### `replacePath(path)` + +Similar to `updatePath` except it replaces the current path in the browser +history instead of adding a new entry. diff --git a/src/index.js b/src/index.js index 8cfb52e..b4f0bcb 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ const UPDATE_PATH = "@@router/UPDATE_PATH"; const SELECT_STATE = state => state.routing; -// Action creator +// Action creators function updatePath(path, noRouterUpdate) { return { @@ -14,6 +14,14 @@ function updatePath(path, noRouterUpdate) { } } +function replacePath(path) { + return { + type: UPDATE_PATH, + path: path, + replaceState: true + } +} + // Reducer const initialState = typeof window === 'undefined' ? {} : { @@ -24,7 +32,8 @@ function update(state=initialState, action) { if(action.type === UPDATE_PATH) { return Object.assign({}, state, { path: action.path, - noRouterUpdate: action.noRouterUpdate + noRouterUpdate: action.noRouterUpdate, + replaceState: action.replaceState }); } return state; @@ -63,7 +72,12 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) { // edge cases. if(routing.path !== locationToString(window.location) && !routing.noRouterUpdate) { - history.pushState(null, routing.path); + if(routing.replaceState) { + history.replaceState(null, routing.path); + } + else { + history.pushState(null, routing.path); + } } }); @@ -76,6 +90,7 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) { module.exports = { UPDATE_PATH, updatePath, + replacePath, syncReduxAndRouter, routeReducer: update, };