@symbo.ls/router 2.11.29 → 2.11.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +40 -23
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -10,6 +10,7 @@ export const getActiveRoute = (level = 0, route = window.location.pathname) => {
10
10
  if (activeRoute) return `/${activeRoute}`
11
11
  }
12
12
 
13
+ export let lastPathname
13
14
  export let lastLevel = 0
14
15
 
15
16
  const defaultOptions = {
@@ -51,36 +52,52 @@ export const router = (
51
52
 
52
53
  const isModal = (findModal(element, element.state) || {}).FoundModal
53
54
  const isInitial = options.initialRender
55
+ const hashChanged = hash && hash !== window.location.hash.slice(1)
56
+ const pathChanged = pathname !== lastPathname
57
+ lastPathname = pathname
54
58
 
55
- if (content) {
56
- if (options.pushState) window.history.pushState(state, null, route + (hash ? `#${hash}` : ''))
59
+ if (!content) return
60
+ if (options.pushState) {
61
+ window.history.pushState(state, null, route + (hash ? `#${hash}` : ''))
62
+ }
57
63
 
58
- if (isInitial || (!isInitial && !isModal)) element.set({ tag: options.useFragment && 'fragment', extend: content })
59
- if (options.updateState) element.state.update({ route, hash })
64
+ if (pathChanged || !hashChanged) {
65
+ const hasSet = isInitial || (!isInitial && !isModal)
60
66
 
61
- const rootNode = element.node
62
- const scrollNode = options.scrollDocument ? document.documentElement : rootNode
63
- if (options.scrollToTop) {
64
- scrollNode.scrollTo({
65
- ...(options.scrollToOptions || {}), top: 0, left: 0
66
- })
67
+ if (options.updateState) {
68
+ element.state.update({ route, hash }, { preventContentUpdate: true })
67
69
  }
68
- if (options.scrollToNode) {
69
- content.content.node.scrollTo({
70
- ...(options.scrollToOptions || {}), top: 0, left: 0
70
+
71
+ if (hasSet) {
72
+ element.set({
73
+ tag: options.useFragment && 'fragment',
74
+ extend: content
71
75
  })
72
76
  }
77
+ }
73
78
 
74
- if (hash) {
75
- const activeNode = document.getElementById(hash)
76
- if (activeNode) {
77
- const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - options.scrollToOffset || 0
78
- scrollNode.scrollTo({
79
- ...(options.scrollToOptions || {}), top, left: 0
80
- })
81
- }
82
- }
79
+ const rootNode = element.node
80
+ const scrollNode = options.scrollDocument ? document.documentElement : rootNode
81
+ if (options.scrollToTop) {
82
+ scrollNode.scrollTo({
83
+ ...(options.scrollToOptions || {}), top: 0, left: 0
84
+ })
85
+ }
86
+ if (options.scrollToNode) {
87
+ content.content.node.scrollTo({
88
+ ...(options.scrollToOptions || {}), top: 0, left: 0
89
+ })
90
+ }
83
91
 
84
- if (route !== '/*') setCookie('route', route)
92
+ if (hash) {
93
+ const activeNode = document.getElementById(hash)
94
+ if (activeNode) {
95
+ const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - options.scrollToOffset || 0
96
+ scrollNode.scrollTo({
97
+ ...(options.scrollToOptions || {}), top, left: 0
98
+ })
99
+ }
85
100
  }
101
+
102
+ if (route !== '/*') setCookie('route', route)
86
103
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/router",
3
- "version": "2.11.29",
3
+ "version": "2.11.34",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -11,6 +11,6 @@
11
11
  "@domql/utils": "latest",
12
12
  "@symbo.ls/auth-redirect": "latest"
13
13
  },
14
- "gitHead": "8487231f7e47b8d7789658c3ac6e284f7bceca5e",
14
+ "gitHead": "a9875f5261165cbef5c9e6344f319636cb89c3f2",
15
15
  "source": "index.js"
16
16
  }