Skip to content

Commit

Permalink
Fix issue with history when using browser's back button.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgegorka committed Dec 8, 2020
1 parent e03f594 commit 9b85751
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 78 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Svelte Router changelog

## 5.8.2

Fix issue with history when using browser's back button.

## 5.8.1

Improve handling of href links.
Expand Down
115 changes: 54 additions & 61 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-router-spa",
"version": "5.8.1",
"version": "5.8.2",
"description": "A full featured router component for Svelte and your Single Page Applications.",
"main": "src/index.js",
"svelte": "src/index.js",
Expand All @@ -15,10 +15,10 @@
"license": "MIT",
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^7.14.0",
"eslint": "^7.15.0",
"mocha": "^8.2.1",
"mocha-jsdom": "^2.0.0",
"svelte": "^3.30.0"
"svelte": "^3.31.0"
},
"dependencies": {
"url-params-parser": "^1.0.3"
Expand Down
11 changes: 7 additions & 4 deletions src/router/current.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ function RouterCurrent(trackPage) {
const trackPageview = trackPage || false
let activeRoute = ''

function setActive(newRoute) {
function setActive(newRoute, updateBrowserHistory) {
activeRoute = newRoute.path
pushActiveRoute(newRoute)
pushActiveRoute(newRoute, updateBrowserHistory)
}

function active() {
Expand Down Expand Up @@ -40,11 +40,14 @@ function RouterCurrent(trackPage) {
}
}

function pushActiveRoute(newRoute) {
function pushActiveRoute(newRoute, updateBrowserHistory) {
if (typeof window !== 'undefined') {
const pathAndSearch = pathWithQueryParams(newRoute)

window.history.pushState({ page: pathAndSearch }, '', pathAndSearch)
if (updateBrowserHistory) {
window.history.pushState({ page: pathAndSearch }, '', pathAndSearch)
}
// Moving back in history does not update browser history but does update tracking.
if (trackPageview) {
gaTracking(pathAndSearch)
}
Expand Down
19 changes: 10 additions & 9 deletions src/spa_router.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ function SpaRouter(routes, currentUrl, options = {}) {
* Redirect current route to another
* @param destinationUrl
**/
function navigateNow(destinationUrl) {
function navigateNow(destinationUrl, updateBrowserHistory) {
if (typeof window !== 'undefined') {
if (destinationUrl === NotFoundPage) {
routerCurrent.setActive({ path: NotFoundPage })
routerCurrent.setActive({ path: NotFoundPage }, updateBrowserHistory)
} else {
navigateTo(destinationUrl)
}
Expand All @@ -53,13 +53,13 @@ function SpaRouter(routes, currentUrl, options = {}) {
return destinationUrl
}

function setActiveRoute() {
function setActiveRoute(updateBrowserHistory = true) {
const currentRoute = findActiveRoute()
if (currentRoute.redirectTo) {
return navigateNow(currentRoute.redirectTo)
return navigateNow(currentRoute.redirectTo, updateBrowserHistory)
}

routerCurrent.setActive(currentRoute)
routerCurrent.setActive(currentRoute, updateBrowserHistory)
activeRoute.set(currentRoute)

return currentRoute
Expand All @@ -86,15 +86,16 @@ function localisedRoute(pathName, language) {
* Updates the current active route and updates the browser pathname
* @param pathName String
* @param language String
* @param updateBrowserHistory Boolean
**/
function navigateTo(pathName, language = null) {
function navigateTo(pathName, language = null, updateBrowserHistory = true) {
pathName = removeSlash(pathName, 'lead')

if (language) {
routerOptions.langConvertTo = language
}

return SpaRouter(userDefinedRoutes, 'http://fake.com/' + pathName, routerOptions).setActiveRoute()
return SpaRouter(userDefinedRoutes, 'http://fake.com/' + pathName, routerOptions).setActiveRoute(updateBrowserHistory)
}

/**
Expand Down Expand Up @@ -131,8 +132,8 @@ if (typeof window !== 'undefined') {

window.onpopstate = function (_event) {
let navigatePathname = window.location.pathname + window.location.search + window.location.hash

navigateTo(navigatePathname)
console.log('here we are')
navigateTo(navigatePathname, null, false)
}
}

Expand Down
24 changes: 23 additions & 1 deletion test/spa_router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ function thisIsTrue() {
}

describe('Router', function () {
describe('When there are no routes defined and update browser history is false', function () {
beforeEach(function () {
testRouter = SpaRouter([], pathName).setActiveRoute(false)
})

it('should set the component', function () {
expect(testRouter.component).to.be.undefined
})

it('should set the route name to 404', function () {
expect(testRouter.name).to.be.undefined
})

it('should set the route path to 404', function () {
expect(testRouter.path).to.be.undefined
})

it('should not update history', function () {
expect(global.window.history.state).to.be.null
})
})

describe('When there are no routes defined', function () {
beforeEach(function () {
testRouter = SpaRouter([], pathName).setActiveRoute()
Expand Down Expand Up @@ -1595,7 +1617,7 @@ describe('onlyIf', function () {
]

pathName = 'http://web.app/admin'
SpaRouter(routes, pathName).setActiveRoute()
SpaRouter(routes, pathName).setActiveRoute(true)
})

it('should render admin', function () {
Expand Down

0 comments on commit 9b85751

Please sign in to comment.