Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
b9fb5767
Commit
b9fb5767
authored
Oct 09, 2018
by
Tim Neutkens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make router use window for singleton
parent
b8e8228e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
58 deletions
+56
-58
index.js
packages/next-server/lib/router/index.js
+56
-58
No files found.
packages/next-server/lib/router/index.js
View file @
b9fb5767
/* global window */
/* global window */
import
_Router
from
'./router'
import
_Router
from
'./router'
import
{
execOnce
}
from
'../utils'
// Create public properties and methods of the router in the SingletonRouter
const
urlPropertyFields
=
[
'pathname'
,
'route'
,
'query'
,
'asPath'
]
const
propertyFields
=
[
'components'
]
const
routerEvents
=
[
'routeChangeStart'
,
'beforeHistoryChange'
,
'routeChangeComplete'
,
'routeChangeError'
,
'hashChangeStart'
,
'hashChangeComplete'
]
const
coreMethodFields
=
[
'push'
,
'replace'
,
'reload'
,
'back'
,
'prefetch'
,
'beforePopState'
]
const
SingletonRouter
=
{
const
SingletonRouter
=
{
router
:
null
,
// holds the actual router instance
router
:
null
,
// holds the actual router instance
...
@@ -13,77 +18,70 @@ const SingletonRouter = {
...
@@ -13,77 +18,70 @@ const SingletonRouter = {
}
}
}
}
// Create public properties and methods of the router in the SingletonRouter
if
(
typeof
window
!==
'undefined'
)
{
const
urlPropertyFields
=
[
'pathname'
,
'route'
,
'query'
,
'asPath'
]
window
.
__NEXT_ROUTER__
=
window
.
__NEXT_ROUTER__
||
SingletonRouter
const
propertyFields
=
[
'components'
]
}
const
routerEvents
=
[
'routeChangeStart'
,
'beforeHistoryChange'
,
'routeChangeComplete'
,
'routeChangeError'
,
'hashChangeStart'
,
'hashChangeComplete'
]
const
coreMethodFields
=
[
'push'
,
'replace'
,
'reload'
,
'back'
,
'prefetch'
,
'beforePopState'
]
// Events is a static property on the router, the router doesn't have to be initialized to use it
function
throwIfNoRouter
()
{
Object
.
defineProperty
(
SingletonRouter
,
'events'
,
{
if
(
!
SingletonRouter
.
router
)
{
get
()
{
const
message
=
'No router instance found.
\
n'
+
return
_Router
.
events
'You should only use "next/router" inside the client side of your app.
\
n'
throw
new
Error
(
message
)
}
}
}
)
}
propertyFields
.
concat
(
urlPropertyFields
).
forEach
((
field
)
=>
{
let
initialized
=
false
// Here we need to use Object.defineProperty because, we need to return
if
(
!
initialized
)
{
// the property assigned to the actual router
initialized
=
true
// The value might get changed as we change routes and this is the
//
proper way to access
it
//
Events is a static property on the router, the router doesn't have to be initialized to use
it
Object
.
defineProperty
(
SingletonRouter
,
field
,
{
Object
.
defineProperty
(
SingletonRouter
,
'events'
,
{
get
()
{
get
()
{
throwIfNoRouter
()
return
_Router
.
events
return
SingletonRouter
.
router
[
field
]
}
}
})
})
})
coreMethodFields
.
forEach
((
field
)
=>
{
propertyFields
.
concat
(
urlPropertyFields
).
forEach
((
field
)
=>
{
SingletonRouter
[
field
]
=
(...
args
)
=>
{
// Here we need to use Object.defineProperty because, we need to return
throwIfNoRouter
()
// the property assigned to the actual router
return
SingletonRouter
.
router
[
field
](...
args
)
// The value might get changed as we change routes and this is the
}
// proper way to access it
})
Object
.
defineProperty
(
SingletonRouter
,
field
,
{
get
()
{
routerEvents
.
forEach
((
event
)
=>
{
throwIfNoRouter
()
SingletonRouter
.
ready
(()
=>
{
return
SingletonRouter
.
router
[
field
]
_Router
.
events
.
on
(
event
,
(...
args
)
=>
{
const
eventField
=
`on
${
event
.
charAt
(
0
).
toUpperCase
()}${
event
.
substring
(
1
)}
`
if
(
SingletonRouter
[
eventField
])
{
try
{
SingletonRouter
[
eventField
](...
args
)
}
catch
(
err
)
{
console
.
error
(
`Error when running the Router event:
${
eventField
}
`
)
console
.
error
(
`
${
err
.
message
}
\n
${
err
.
stack
}
`
)
}
}
}
})
})
})
})
})
const
warnAboutRouterOnAppUpdated
=
execOnce
(()
=>
{
coreMethodFields
.
forEach
((
field
)
=>
{
console
.
warn
(
`Router.onAppUpdated is removed - visit https://err.sh/zeit/next.js/no-on-app-updated-hook for more information.`
)
SingletonRouter
[
field
]
=
(...
args
)
=>
{
})
throwIfNoRouter
()
return
SingletonRouter
.
router
[
field
](...
args
)
Object
.
defineProperty
(
SingletonRouter
,
'onAppUpdated'
,
{
}
get
()
{
return
null
},
})
set
()
{
warnAboutRouterOnAppUpdated
()
return
null
}
})
function
throwIfNoRouter
()
{
routerEvents
.
forEach
((
event
)
=>
{
if
(
!
SingletonRouter
.
router
)
{
SingletonRouter
.
ready
(()
=>
{
const
message
=
'No router instance found.
\
n'
+
_Router
.
events
.
on
(
event
,
(...
args
)
=>
{
'You should only use "next/router" inside the client side of your app.
\
n'
const
eventField
=
`on
${
event
.
charAt
(
0
).
toUpperCase
()}${
event
.
substring
(
1
)}
`
throw
new
Error
(
message
)
if
(
SingletonRouter
[
eventField
])
{
}
try
{
SingletonRouter
[
eventField
](...
args
)
}
catch
(
err
)
{
console
.
error
(
`Error when running the Router event:
${
eventField
}
`
)
console
.
error
(
`
${
err
.
message
}
\n
${
err
.
stack
}
`
)
}
}
})
})
})
}
}
const
routerInstance
=
typeof
window
!==
'undefined'
?
window
.
__NEXT_ROUTER__
:
SingletonRouter
// Export the SingletonRouter and this is the public API.
// Export the SingletonRouter and this is the public API.
export
default
SingletonRouter
export
default
routerInstance
// Reexport the withRoute HOC
// Reexport the withRoute HOC
export
{
default
as
withRouter
}
from
'./with-router'
export
{
default
as
withRouter
}
from
'./with-router'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment