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
Show 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,20 +18,30 @@ const SingletonRouter = {
...
@@ -13,20 +18,30 @@ 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'
]
function
throwIfNoRouter
()
{
if
(
!
SingletonRouter
.
router
)
{
const
message
=
'No router instance found.
\
n'
+
'You should only use "next/router" inside the client side of your app.
\
n'
throw
new
Error
(
message
)
}
}
let
initialized
=
false
if
(
!
initialized
)
{
initialized
=
true
// Events is a static property on the router, the router doesn't have to be initialized to use it
// Events is a static property on the router, the router doesn't have to be initialized to use it
Object
.
defineProperty
(
SingletonRouter
,
'events'
,
{
Object
.
defineProperty
(
SingletonRouter
,
'events'
,
{
get
()
{
get
()
{
return
_Router
.
events
return
_Router
.
events
}
}
})
})
propertyFields
.
concat
(
urlPropertyFields
).
forEach
((
field
)
=>
{
propertyFields
.
concat
(
urlPropertyFields
).
forEach
((
field
)
=>
{
// Here we need to use Object.defineProperty because, we need to return
// Here we need to use Object.defineProperty because, we need to return
// the property assigned to the actual router
// the property assigned to the actual router
// The value might get changed as we change routes and this is the
// The value might get changed as we change routes and this is the
...
@@ -37,16 +52,16 @@ propertyFields.concat(urlPropertyFields).forEach((field) => {
...
@@ -37,16 +52,16 @@ propertyFields.concat(urlPropertyFields).forEach((field) => {
return
SingletonRouter
.
router
[
field
]
return
SingletonRouter
.
router
[
field
]
}
}
})
})
})
})
coreMethodFields
.
forEach
((
field
)
=>
{
coreMethodFields
.
forEach
((
field
)
=>
{
SingletonRouter
[
field
]
=
(...
args
)
=>
{
SingletonRouter
[
field
]
=
(...
args
)
=>
{
throwIfNoRouter
()
throwIfNoRouter
()
return
SingletonRouter
.
router
[
field
](...
args
)
return
SingletonRouter
.
router
[
field
](...
args
)
}
}
})
})
routerEvents
.
forEach
((
event
)
=>
{
routerEvents
.
forEach
((
event
)
=>
{
SingletonRouter
.
ready
(()
=>
{
SingletonRouter
.
ready
(()
=>
{
_Router
.
events
.
on
(
event
,
(...
args
)
=>
{
_Router
.
events
.
on
(
event
,
(...
args
)
=>
{
const
eventField
=
`on
${
event
.
charAt
(
0
).
toUpperCase
()}${
event
.
substring
(
1
)}
`
const
eventField
=
`on
${
event
.
charAt
(
0
).
toUpperCase
()}${
event
.
substring
(
1
)}
`
...
@@ -60,30 +75,13 @@ routerEvents.forEach((event) => {
...
@@ -60,30 +75,13 @@ routerEvents.forEach((event) => {
}
}
})
})
})
})
})
})
const
warnAboutRouterOnAppUpdated
=
execOnce
(()
=>
{
console
.
warn
(
`Router.onAppUpdated is removed - visit https://err.sh/zeit/next.js/no-on-app-updated-hook for more information.`
)
})
Object
.
defineProperty
(
SingletonRouter
,
'onAppUpdated'
,
{
get
()
{
return
null
},
set
()
{
warnAboutRouterOnAppUpdated
()
return
null
}
})
function
throwIfNoRouter
()
{
if
(
!
SingletonRouter
.
router
)
{
const
message
=
'No router instance found.
\
n'
+
'You should only use "next/router" inside the client side of your app.
\
n'
throw
new
Error
(
message
)
}
}
}
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