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
07c3464a
Unverified
Commit
07c3464a
authored
Jul 25, 2022
by
Tim Neutkens
Committed by
GitHub
Jul 25, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor router reducer (#38983)
parent
c3fd9e4e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
392 additions
and
326 deletions
+392
-326
next-app-loader.ts
packages/next/build/webpack/loaders/next-app-loader.ts
+0
-1
app-router.client.tsx
packages/next/client/components/app-router.client.tsx
+102
-55
hooks-client-context.ts
packages/next/client/components/hooks-client-context.ts
+4
-2
hooks-client.ts
packages/next/client/components/hooks-client.ts
+3
-3
hot-reloader.client.tsx
packages/next/client/components/hot-reloader.client.tsx
+2
-2
layout-router.client.tsx
packages/next/client/components/layout-router.client.tsx
+2
-2
reducer.ts
packages/next/client/components/reducer.ts
+247
-239
app-render.tsx
packages/next/server/app-render.tsx
+8
-20
app-router-context.ts
packages/next/shared/lib/app-router-context.ts
+24
-2
No files found.
packages/next/build/webpack/loaders/next-app-loader.ts
View file @
07c3464a
...
@@ -130,7 +130,6 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
...
@@ -130,7 +130,6 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
?
`require('next/dist/client/components/hot-reloader.client.js').default`
?
`require('next/dist/client/components/hot-reloader.client.js').default`
:
'null'
:
'null'
}
}
export const hooksClientContext = require('next/dist/client/components/hooks-client-context.js')
export const __next_app_webpack_require__ = __webpack_require__
export const __next_app_webpack_require__ = __webpack_require__
`
`
...
...
packages/next/client/components/app-router.client.tsx
View file @
07c3464a
This diff is collapsed.
Click to expand it.
packages/next/client/components/hooks-client-context.ts
View file @
07c3464a
import
{
createContext
}
from
'react'
import
{
createContext
}
from
'react'
import
{
NextParsedUrlQuery
}
from
'../../server/request-meta'
import
{
NextParsedUrlQuery
}
from
'../../server/request-meta'
export
const
QueryContext
=
createContext
<
NextParsedUrlQuery
>
(
null
as
any
)
export
const
SearchParamsContext
=
createContext
<
NextParsedUrlQuery
>
(
null
as
any
)
export
const
PathnameContext
=
createContext
<
string
>
(
null
as
any
)
export
const
PathnameContext
=
createContext
<
string
>
(
null
as
any
)
export
const
ParamsContext
=
createContext
(
null
as
any
)
export
const
ParamsContext
=
createContext
(
null
as
any
)
export
const
LayoutSegmentsContext
=
createContext
(
null
as
any
)
export
const
LayoutSegmentsContext
=
createContext
(
null
as
any
)
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
QueryContext
.
displayName
=
'Query
Context'
SearchParamsContext
.
displayName
=
'SearchParams
Context'
PathnameContext
.
displayName
=
'PathnameContext'
PathnameContext
.
displayName
=
'PathnameContext'
ParamsContext
.
displayName
=
'ParamsContext'
ParamsContext
.
displayName
=
'ParamsContext'
LayoutSegmentsContext
.
displayName
=
'LayoutSegmentsContext'
LayoutSegmentsContext
.
displayName
=
'LayoutSegmentsContext'
...
...
packages/next/client/components/hooks-client.ts
View file @
07c3464a
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
import
{
useContext
}
from
'react'
import
{
useContext
}
from
'react'
import
{
import
{
Query
Context
,
SearchParams
Context
,
// ParamsContext,
// ParamsContext,
PathnameContext
,
PathnameContext
,
// LayoutSegmentsContext,
// LayoutSegmentsContext,
...
@@ -13,11 +13,11 @@ import {
...
@@ -13,11 +13,11 @@ import {
}
from
'../../shared/lib/app-router-context'
}
from
'../../shared/lib/app-router-context'
export
function
useSearchParams
()
{
export
function
useSearchParams
()
{
return
useContext
(
Query
Context
)
return
useContext
(
SearchParams
Context
)
}
}
export
function
useSearchParam
(
key
:
string
):
string
|
string
[]
{
export
function
useSearchParam
(
key
:
string
):
string
|
string
[]
{
const
params
=
useContext
(
Query
Context
)
const
params
=
useContext
(
SearchParams
Context
)
return
params
[
key
]
return
params
[
key
]
}
}
...
...
packages/next/client/components/hot-reloader.client.tsx
View file @
07c3464a
...
@@ -6,7 +6,7 @@ import {
...
@@ -6,7 +6,7 @@ import {
// @ts-expect-error TODO-APP: startTransition exists
// @ts-expect-error TODO-APP: startTransition exists
startTransition
,
startTransition
,
}
from
'react'
}
from
'react'
import
{
FullAppTree
Context
}
from
'../../shared/lib/app-router-context'
import
{
GlobalLayoutRouter
Context
}
from
'../../shared/lib/app-router-context'
import
{
import
{
register
,
register
,
unregister
,
unregister
,
...
@@ -397,7 +397,7 @@ function processMessage(
...
@@ -397,7 +397,7 @@ function processMessage(
}
}
export
default
function
HotReload
({
assetPrefix
}:
{
assetPrefix
:
string
})
{
export
default
function
HotReload
({
assetPrefix
}:
{
assetPrefix
:
string
})
{
const
{
tree
}
=
useContext
(
FullAppTree
Context
)
const
{
tree
}
=
useContext
(
GlobalLayoutRouter
Context
)
const
router
=
useRouter
()
const
router
=
useRouter
()
const
webSocketRef
=
useRef
<
WebSocket
>
()
const
webSocketRef
=
useRef
<
WebSocket
>
()
...
...
packages/next/client/components/layout-router.client.tsx
View file @
07c3464a
...
@@ -8,7 +8,7 @@ import type {
...
@@ -8,7 +8,7 @@ import type {
}
from
'../../server/app-render'
}
from
'../../server/app-render'
import
{
import
{
AppTreeContext
,
AppTreeContext
,
FullAppTree
Context
,
GlobalLayoutRouter
Context
,
}
from
'../../shared/lib/app-router-context'
}
from
'../../shared/lib/app-router-context'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
matchSegment
}
from
'./match-segments'
import
{
matchSegment
}
from
'./match-segments'
...
@@ -72,7 +72,7 @@ export function InnerLayoutRouter({
...
@@ -72,7 +72,7 @@ export function InnerLayoutRouter({
changeByServerResponse
,
changeByServerResponse
,
tree
:
fullTree
,
tree
:
fullTree
,
focusRef
,
focusRef
,
}
=
useContext
(
FullAppTree
Context
)
}
=
useContext
(
GlobalLayoutRouter
Context
)
const
focusAndScrollRef
=
useRef
<
HTMLDivElement
>
(
null
)
const
focusAndScrollRef
=
useRef
<
HTMLDivElement
>
(
null
)
useEffect
(()
=>
{
useEffect
(()
=>
{
...
...
packages/next/client/components/reducer.ts
View file @
07c3464a
This diff is collapsed.
Click to expand it.
packages/next/server/app-render.tsx
View file @
07c3464a
...
@@ -830,29 +830,17 @@ export async function renderToHTML(
...
@@ -830,29 +830,17 @@ export async function renderToHTML(
const
AppRouter
=
const
AppRouter
=
ComponentMod
.
AppRouter
as
typeof
import
(
'../client/components/app-router.client'
).
default
ComponentMod
.
AppRouter
as
typeof
import
(
'../client/components/app-router.client'
).
default
const
{
QueryContext
,
PathnameContext
,
// ParamsContext,
// LayoutSegmentsContext,
}
=
ComponentMod
.
hooksClientContext
as
typeof
import
(
'../client/components/hooks-client-context'
)
const
WrappedComponentTreeWithRouter
=
()
=>
{
const
WrappedComponentTreeWithRouter
=
()
=>
{
return
(
return
(
<
QueryContext
.
Provider
value=
{
query
}
>
<
AppRouter
<
PathnameContext
.
Provider
value=
{
pathname
}
>
hotReloader=
{
HotReloader
&&
<
HotReloader
assetPrefix=
""
/>
}
{
/* <ParamsContext.Provider value={pathParams}> */
}
initialCanonicalUrl=
{
initialCanonicalUrl
}
<
AppRouter
initialTree=
{
initialTree
}
hotReloader=
{
HotReloader
&&
<
HotReloader
assetPrefix=
""
/>
}
initialStylesheets=
{
initialStylesheets
}
initialCanonicalUrl=
{
initialCanonicalUrl
}
>
initialTree=
{
initialTree
}
<
ComponentTree
/>
initialStylesheets=
{
initialStylesheets
}
</
AppRouter
>
>
<
ComponentTree
/>
</
AppRouter
>
{
/* </ParamsContext.Provider> */
}
</
PathnameContext
.
Provider
>
</
QueryContext
.
Provider
>
)
)
}
}
...
...
packages/next/shared/lib/app-router-context.ts
View file @
07c3464a
...
@@ -14,11 +14,33 @@ export type CacheNode = {
...
@@ -14,11 +14,33 @@ export type CacheNode = {
}
}
export
type
AppRouterInstance
=
{
export
type
AppRouterInstance
=
{
/**
* Reload the current page. Fetches new data from the server.
*/
reload
():
void
reload
():
void
/**
* Hard navigate to the provided href. Fetches new data from the server.
* Pushes a new history entry.
*/
push
(
href
:
string
):
void
push
(
href
:
string
):
void
/**
* Soft navigate to the provided href. Does not fetch data from the server if it was already fetched.
* Pushes a new history entry.
*/
softPush
(
href
:
string
):
void
softPush
(
href
:
string
):
void
/**
* Hard navigate to the provided href. Does not fetch data from the server if it was already fetched.
* Replaces the current history entry.
*/
replace
(
href
:
string
):
void
replace
(
href
:
string
):
void
/**
* Soft navigate to the provided href. Does not fetch data from the server if it was already fetched.
* Replaces the current history entry.
*/
softReplace
(
href
:
string
):
void
softReplace
(
href
:
string
):
void
/**
* Soft prefetch the provided href. Does not fetch data from the server if it was already fetched.
*/
prefetch
(
href
:
string
):
Promise
<
void
>
prefetch
(
href
:
string
):
Promise
<
void
>
}
}
...
@@ -31,7 +53,7 @@ export const AppTreeContext = React.createContext<{
...
@@ -31,7 +53,7 @@ export const AppTreeContext = React.createContext<{
url
:
string
url
:
string
stylesheets
?:
string
[]
stylesheets
?:
string
[]
}
>
(
null
as
any
)
}
>
(
null
as
any
)
export
const
FullAppTree
Context
=
React
.
createContext
<
{
export
const
GlobalLayoutRouter
Context
=
React
.
createContext
<
{
tree
:
FlightRouterState
tree
:
FlightRouterState
changeByServerResponse
:
(
changeByServerResponse
:
(
previousTree
:
FlightRouterState
,
previousTree
:
FlightRouterState
,
...
@@ -43,5 +65,5 @@ export const FullAppTreeContext = React.createContext<{
...
@@ -43,5 +65,5 @@ export const FullAppTreeContext = React.createContext<{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
AppRouterContext
.
displayName
=
'AppRouterContext'
AppRouterContext
.
displayName
=
'AppRouterContext'
AppTreeContext
.
displayName
=
'AppTreeContext'
AppTreeContext
.
displayName
=
'AppTreeContext'
FullAppTreeContext
.
displayName
=
'FullAppTree
Context'
GlobalLayoutRouterContext
.
displayName
=
'GlobalLayoutRouter
Context'
}
}
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