Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Add replacePath functionality #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
21 changes: 18 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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' ? {} : {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
});

Expand All @@ -76,6 +90,7 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
module.exports = {
UPDATE_PATH,
updatePath,
replacePath,
syncReduxAndRouter,
routeReducer: update,
};