Skip to content

Commit

Permalink
improving history management in middleware/main/mono
Browse files Browse the repository at this point in the history
  • Loading branch information
Lcfvs committed Jan 25, 2019
1 parent 6bdee1e commit 37fd3aa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions middleware/main/mono/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const window = global()
const history = create()
history.entries = create()

function cleanHref (href) {
return href.toString().split('#')[0]
}

function getTitle (element) {
return text(one('h1', element)).trim()
}
Expand All @@ -24,18 +28,19 @@ function updateTitle (element) {
function listen () {
const main = one('main')

register(main, window.location.href)
register(main, cleanHref(window.location.href))
window.addEventListener('popstate', onPopState)
history.branding = window.document.title.trim()
.replace(getTitle(main), '$1')
}

function onPopState (event) {
const main = history.entries[event.target.location.href]
const registered = history.entries[cleanHref(event.target.location.href)]
const current = one('main')

if (main) {
updateTitle(main)
replace(main, one('main'))
if (registered && registered !== current) {
updateTitle(registered)
replace(registered, current)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anticore",
"version": "2.23.1",
"version": "2.23.2",
"description": "A generic living DOM library to simplify your client code, like with its easiest middleware manager for AJAX requests.",
"main": "index.js",
"module": "index.js",
Expand Down

0 comments on commit 37fd3aa

Please sign in to comment.