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
647c93e4
Unverified
Commit
647c93e4
authored
Jun 01, 2022
by
Tim Neutkens
Committed by
GitHub
Jun 01, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle link with back/forward navigation (#37332)
parent
5384171b
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
183 additions
and
69 deletions
+183
-69
entries.ts
packages/next/build/entries.ts
+11
-1
webpack-config.ts
packages/next/build/webpack-config.ts
+1
-0
next-app-loader.ts
packages/next/build/webpack/loaders/next-app-loader.ts
+1
-0
app-index.tsx
packages/next/client/app-index.tsx
+7
-0
app-router.client.tsx
packages/next/client/components/app-router.client.tsx
+32
-45
layout-router.client.tsx
packages/next/client/components/layout-router.client.tsx
+30
-0
userouter.ts
packages/next/client/components/userouter.ts
+38
-0
link.tsx
packages/next/client/link.tsx
+37
-8
app-render.tsx
packages/next/server/app-render.tsx
+22
-15
hot-reloader.ts
packages/next/server/dev/hot-reloader.ts
+4
-0
No files found.
packages/next/build/entries.ts
View file @
647c93e4
...
@@ -432,11 +432,13 @@ export function finalizeEntrypoint({
...
@@ -432,11 +432,13 @@ export function finalizeEntrypoint({
compilerType
,
compilerType
,
value
,
value
,
isServerComponent
,
isServerComponent
,
appDir
,
}:
{
}:
{
compilerType
?:
'client'
|
'server'
|
'edge-server'
compilerType
?:
'client'
|
'server'
|
'edge-server'
name
:
string
name
:
string
value
:
ObjectValue
<
webpack5
.
EntryObject
>
value
:
ObjectValue
<
webpack5
.
EntryObject
>
isServerComponent
?:
boolean
isServerComponent
?:
boolean
appDir
?:
boolean
}):
ObjectValue
<
webpack5
.
EntryObject
>
{
}):
ObjectValue
<
webpack5
.
EntryObject
>
{
const
entry
=
const
entry
=
typeof
value
!==
'object'
||
Array
.
isArray
(
value
)
typeof
value
!==
'object'
||
Array
.
isArray
(
value
)
...
@@ -471,11 +473,19 @@ export function finalizeEntrypoint({
...
@@ -471,11 +473,19 @@ export function finalizeEntrypoint({
name
!==
CLIENT_STATIC_FILES_RUNTIME_AMP
&&
name
!==
CLIENT_STATIC_FILES_RUNTIME_AMP
&&
name
!==
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH
name
!==
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH
)
{
)
{
// TODO: this is a temporary fix. @shuding is going to change the handling of server components
if
(
appDir
&&
entry
.
import
.
includes
(
'flight'
))
{
return
{
dependOn
:
CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT
,
...
entry
,
}
}
return
{
return
{
dependOn
:
dependOn
:
name
.
startsWith
(
'pages/'
)
&&
name
!==
'pages/_app'
name
.
startsWith
(
'pages/'
)
&&
name
!==
'pages/_app'
?
'pages/_app'
?
'pages/_app'
:
'main'
,
:
CLIENT_STATIC_FILES_RUNTIME_MAIN
,
...
entry
,
...
entry
,
}
}
}
}
...
...
packages/next/build/webpack-config.ts
View file @
647c93e4
...
@@ -2289,6 +2289,7 @@ export default async function getBaseWebpackConfig(
...
@@ -2289,6 +2289,7 @@ export default async function getBaseWebpackConfig(
value
:
entry
[
name
],
value
:
entry
[
name
],
compilerType
,
compilerType
,
name
,
name
,
appDir
:
config
.
experimental
.
appDir
,
})
})
}
}
...
...
packages/next/build/webpack/loaders/next-app-loader.ts
View file @
647c93e4
...
@@ -118,6 +118,7 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
...
@@ -118,6 +118,7 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
};
};
export const AppRouter = require('next/dist/client/components/app-router.client.js').default
export const AppRouter = require('next/dist/client/components/app-router.client.js').default
export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default
export const __next_app_webpack_require__ = __webpack_require__
export const __next_app_webpack_require__ = __webpack_require__
`
`
...
...
packages/next/client/app-index.tsx
View file @
647c93e4
...
@@ -10,6 +10,13 @@ import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-we
...
@@ -10,6 +10,13 @@ import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-we
export
const
version
=
process
.
env
.
__NEXT_VERSION
export
const
version
=
process
.
env
.
__NEXT_VERSION
// History replace has to happen on bootup to ensure `state` is always populated in popstate event
window
.
history
.
replaceState
(
{
url
:
window
.
location
.
toString
()
},
''
,
window
.
location
.
toString
()
)
const
appElement
:
HTMLElement
|
Document
|
null
=
document
const
appElement
:
HTMLElement
|
Document
|
null
=
document
let
reactRoot
:
any
=
null
let
reactRoot
:
any
=
null
...
...
packages/next/client/components/app-router.client.tsx
View file @
647c93e4
import
React
from
'react'
import
React
from
'react'
import
{
createFromFetch
}
from
'next/dist/compiled/react-server-dom-webpack'
import
{
createFromFetch
}
from
'next/dist/compiled/react-server-dom-webpack'
import
{
AppRouterContext
}
from
'../../shared/lib/app-router-context'
import
{
AppRouterContext
}
from
'../../shared/lib/app-router-context'
import
useRouter
from
'./userouter.js'
function
createResponseCache
()
{
function
createResponseCache
()
{
return
new
Map
<
string
,
any
>
()
return
new
Map
<
string
,
any
>
()
}
}
const
rscCache
=
createResponseCache
()
const
rscCache
=
createResponseCache
()
const
getCacheKey
=
()
=>
{
function
fetchFlight
(
href
:
string
,
layoutPath
?:
string
)
{
const
{
pathname
,
search
}
=
location
const
flightUrl
=
new
URL
(
href
,
location
.
origin
.
toString
())
return
pathname
+
search
const
searchParams
=
flightUrl
.
searchParams
}
function
fetchFlight
(
href
:
string
)
{
const
url
=
new
URL
(
href
,
location
.
origin
)
const
searchParams
=
url
.
searchParams
searchParams
.
append
(
'__flight__'
,
'1'
)
searchParams
.
append
(
'__flight__'
,
'1'
)
if
(
layoutPath
)
{
searchParams
.
append
(
'__flight_router_path__'
,
layoutPath
)
}
return
fetch
(
u
rl
.
toString
())
return
fetch
(
flightU
rl
.
toString
())
}
}
function
fetchServerResponse
(
cacheKey
:
string
)
{
export
function
fetchServerResponse
(
href
:
string
,
layoutPath
?:
string
)
{
const
cacheKey
=
href
+
layoutPath
let
response
=
rscCache
.
get
(
cacheKey
)
let
response
=
rscCache
.
get
(
cacheKey
)
if
(
response
)
return
response
if
(
response
)
return
response
response
=
createFromFetch
(
fetchFlight
(
getCacheKey
()
))
response
=
createFromFetch
(
fetchFlight
(
href
,
layoutPath
))
rscCache
.
set
(
cacheKey
,
response
)
rscCache
.
set
(
cacheKey
,
response
)
return
response
return
response
}
}
// TODO: move to client component when handling is implemented
export
default
function
AppRouter
({
initialUrl
,
layoutPath
,
children
}:
any
)
{
export
default
function
AppRouter
({
initialUrl
,
children
}:
any
)
{
const
[
appRouter
,
previousUrlRef
,
current
]
=
useRouter
(
initialUrl
)
const
initialState
=
{
url
:
initialUrl
,
const
onPopState
=
React
.
useCallback
(
}
({
state
}:
PopStateEvent
)
=>
{
const
previousUrlRef
=
React
.
useRef
(
initialState
)
if
(
!
state
)
{
const
[
current
,
setCurrent
]
=
React
.
useState
(
initialState
)
return
const
appRouter
=
React
.
useMemo
(()
=>
{
}
return
{
// @ts-ignore useTransition exists
prefetch
:
()
=>
{},
// TODO: Ideally the back button should not use startTransition as it should apply the updates synchronously
replace
:
(
url
:
string
)
=>
{
React
.
startTransition
(()
=>
appRouter
.
replace
(
state
.
url
))
previousUrlRef
.
current
=
current
},
setCurrent
({
...
current
,
url
})
[
appRouter
]
// TODO: update url eagerly or not?
)
window
.
history
.
replaceState
(
current
,
''
,
url
)
React
.
useEffect
(()
=>
{
},
window
.
addEventListener
(
'popstate'
,
onPopState
)
push
:
(
url
:
string
)
=>
{
return
()
=>
{
previousUrlRef
.
current
=
current
window
.
removeEventListener
(
'popstate'
,
onPopState
)
setCurrent
({
...
current
,
url
})
// TODO: update url eagerly or not?
window
.
history
.
pushState
(
current
,
''
,
url
)
},
url
:
current
.
url
,
}
}
},
[
current
])
})
if
(
typeof
window
!==
'undefined'
)
{
// @ts-ignore TODO: for testing
window
.
appRouter
=
appRouter
console
.
log
({
appRouter
,
previous
:
previousUrlRef
.
current
,
current
,
})
}
let
root
let
root
// TODO: Check the RSC cache first for the page you want to navigate to
if
(
current
.
url
!==
previousUrlRef
.
current
?.
url
)
{
if
(
current
.
url
!==
previousUrlRef
.
current
?.
url
)
{
// eslint-disable-next-line
// eslint-disable-next-line
const
data
=
fetchServerResponse
(
current
.
url
)
const
data
=
fetchServerResponse
(
current
.
url
,
layoutPath
)
root
=
data
.
readRoot
()
root
=
data
.
readRoot
()
}
}
return
(
return
(
...
...
packages/next/client/components/layout-router.client.tsx
0 → 100644
View file @
647c93e4
import
React
from
'react'
import
{
AppRouterContext
}
from
'../../shared/lib/app-router-context'
import
{
fetchServerResponse
}
from
'./app-router.client.js'
import
useRouter
from
'./userouter.js'
// TODO:
// What is the next segment for this router?
// What is the parallel router (for lookup in history state)?
// If you have children or another prop name for parallel router does not exist because it has not seen before
// Use the props as the initial value to fill in the history state, this would cause a replaceState to update the tree
// Should probably be batched (wrapper around history)
export
default
function
LayoutRouter
({
initialUrl
,
layoutPath
,
children
,
}:
any
)
{
const
[
appRouter
,
previousUrlRef
,
current
]
=
useRouter
(
initialUrl
)
let
root
if
(
current
.
url
!==
previousUrlRef
.
current
?.
url
)
{
// eslint-disable-next-line
const
data
=
fetchServerResponse
(
current
.
url
,
layoutPath
)
root
=
data
.
readRoot
()
// TODO: handle case where middleware rewrites to another page
}
return
(
<
AppRouterContext
.
Provider
value=
{
appRouter
}
>
{
root
?
root
:
children
}
</
AppRouterContext
.
Provider
>
)
}
packages/next/client/components/userouter.ts
0 → 100644
View file @
647c93e4
import
React
from
'react'
// TODO: Handle case where current layout path does not handle navigation
export
default
function
useRouter
(
initialUrl
:
string
):
any
{
const
initialState
=
{
url
:
initialUrl
,
}
const
previousUrlRef
=
React
.
useRef
(
initialState
)
const
[
current
,
setCurrent
]
=
React
.
useState
(
initialState
)
const
change
=
React
.
useCallback
(
(
method
:
'replaceState'
|
'pushState'
,
url
:
string
)
=>
{
// @ts-ignore startTransition exists
React
.
startTransition
(()
=>
{
previousUrlRef
.
current
=
current
const
state
=
{
...
current
,
url
}
setCurrent
(
state
)
// TODO: update url eagerly or not?
window
.
history
[
method
](
state
,
''
,
url
)
})
},
[
current
]
)
const
appRouter
=
React
.
useMemo
(()
=>
{
return
{
// TODO: implement prefetching of loading / flight
prefetch
:
()
=>
Promise
.
resolve
({}),
replace
:
(
url
:
string
)
=>
{
return
change
(
'replaceState'
,
url
)
},
push
:
(
url
:
string
)
=>
{
return
change
(
'pushState'
,
url
)
},
url
:
current
.
url
,
}
},
[
current
,
change
])
return
[
appRouter
,
previousUrlRef
,
current
,
change
]
}
packages/next/client/link.tsx
View file @
647c93e4
...
@@ -13,6 +13,9 @@ import { useIntersection } from './use-intersection'
...
@@ -13,6 +13,9 @@ import { useIntersection } from './use-intersection'
import
{
getDomainLocale
}
from
'./get-domain-locale'
import
{
getDomainLocale
}
from
'./get-domain-locale'
import
{
addBasePath
}
from
'./add-base-path'
import
{
addBasePath
}
from
'./add-base-path'
// @ts-ignore useTransition exist
const
hasUseTransition
=
typeof
React
.
useTransition
!==
'undefined'
type
Url
=
string
|
UrlObject
type
Url
=
string
|
UrlObject
type
RequiredKeys
<
T
>
=
{
type
RequiredKeys
<
T
>
=
{
[
K
in
keyof
T
]
-
?:
{}
extends
Pick
<
T
,
K
>
?
never
:
K
[
K
in
keyof
T
]
-
?:
{}
extends
Pick
<
T
,
K
>
?
never
:
K
...
@@ -98,7 +101,8 @@ function linkClicked(
...
@@ -98,7 +101,8 @@ function linkClicked(
replace
?:
boolean
,
replace
?:
boolean
,
shallow
?:
boolean
,
shallow
?:
boolean
,
scroll
?:
boolean
,
scroll
?:
boolean
,
locale
?:
string
|
false
locale
?:
string
|
false
,
startTransition
?:
(
cb
:
any
)
=>
void
):
void
{
):
void
{
const
{
nodeName
}
=
e
.
currentTarget
const
{
nodeName
}
=
e
.
currentTarget
...
@@ -112,12 +116,20 @@ function linkClicked(
...
@@ -112,12 +116,20 @@ function linkClicked(
e
.
preventDefault
()
e
.
preventDefault
()
// replace state instead of push if prop is present
const
navigate
=
()
=>
{
router
[
replace
?
'replace'
:
'push'
](
href
,
as
,
{
// replace state instead of push if prop is present
shallow
,
router
[
replace
?
'replace'
:
'push'
](
href
,
as
,
{
locale
,
shallow
,
scroll
,
locale
,
})
scroll
,
})
}
if
(
startTransition
)
{
startTransition
(
navigate
)
}
else
{
navigate
()
}
}
}
type
LinkPropsReal
=
React
.
PropsWithChildren
<
type
LinkPropsReal
=
React
.
PropsWithChildren
<
...
@@ -268,6 +280,13 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
...
@@ -268,6 +280,13 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
}
}
const
p
=
prefetchProp
!==
false
const
p
=
prefetchProp
!==
false
const
[,
/* isPending */
startTransition
]
=
hasUseTransition
?
// Rules of hooks is disabled here because the useTransition will always exist with React 18.
// There is no difference between renders in this case, only between using React 18 vs 17.
// @ts-ignore useTransition exists
// eslint-disable-next-line react-hooks/rules-of-hooks
React
.
useTransition
()
:
[]
let
router
=
React
.
useContext
(
RouterContext
)
let
router
=
React
.
useContext
(
RouterContext
)
const
appRouter
=
React
.
useContext
(
AppRouterContext
)
const
appRouter
=
React
.
useContext
(
AppRouterContext
)
...
@@ -387,7 +406,17 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
...
@@ -387,7 +406,17 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
child
.
props
.
onClick
(
e
)
child
.
props
.
onClick
(
e
)
}
}
if
(
!
e
.
defaultPrevented
)
{
if
(
!
e
.
defaultPrevented
)
{
linkClicked
(
e
,
router
,
href
,
as
,
replace
,
shallow
,
scroll
,
locale
)
linkClicked
(
e
,
router
,
href
,
as
,
replace
,
shallow
,
scroll
,
locale
,
appRouter
?
startTransition
:
undefined
)
}
}
},
},
onMouseEnter
:
(
e
:
React
.
MouseEvent
)
=>
{
onMouseEnter
:
(
e
:
React
.
MouseEvent
)
=>
{
...
...
packages/next/server/app-render.tsx
View file @
647c93e4
...
@@ -229,7 +229,7 @@ export async function renderToHTML(
...
@@ -229,7 +229,7 @@ export async function renderToHTML(
if
(
flightRouterPath
)
{
if
(
flightRouterPath
)
{
// TODO: check the actual path
// TODO: check the actual path
const
pathLength
=
path
.
length
const
pathLength
=
path
.
length
return
pathLength
>
=
flightRouterPath
.
length
return
pathLength
>
flightRouterPath
.
length
}
}
return
true
return
true
})
})
...
@@ -237,6 +237,7 @@ export async function renderToHTML(
...
@@ -237,6 +237,7 @@ export async function renderToHTML(
.
map
((
path
)
=>
{
.
map
((
path
)
=>
{
const
mod
=
ComponentMod
.
components
[
path
]()
const
mod
=
ComponentMod
.
components
[
path
]()
mod
.
Component
=
mod
.
default
||
mod
mod
.
Component
=
mod
.
default
||
mod
mod
.
path
=
path
return
mod
return
mod
})
})
...
@@ -305,6 +306,7 @@ export async function renderToHTML(
...
@@ -305,6 +306,7 @@ export async function renderToHTML(
preloadDataFetchingRecord
(
dataCache
,
dataCacheKey
,
fetcher
)
preloadDataFetchingRecord
(
dataCache
,
dataCacheKey
,
fetcher
)
}
}
const
LayoutRouter
=
ComponentMod
.
LayoutRouter
// eslint-disable-next-line no-loop-func
// eslint-disable-next-line no-loop-func
const
lastComponent
=
WrappedComponent
const
lastComponent
=
WrappedComponent
WrappedComponent
=
(
props
:
any
)
=>
{
WrappedComponent
=
(
props
:
any
)
=>
{
...
@@ -326,22 +328,23 @@ export async function renderToHTML(
...
@@ -326,22 +328,23 @@ export async function renderToHTML(
}
}
}
}
// if this is the root layout pass children as children prop
const
children
=
React
.
createElement
(
if
(
!
isSubtreeRender
&&
i
===
0
)
{
lastComponent
||
React
.
Fragment
,
return
React
.
createElement
(
layout
.
Component
,
{
{},
...
props
,
null
children
:
React
.
createElement
(
)
lastComponent
||
React
.
Fragment
,
// Pages don't need to be wrapped in a router
{},
null
),
})
}
return
React
.
createElement
(
return
React
.
createElement
(
layout
.
Component
,
layout
.
Component
,
props
,
props
,
React
.
createElement
(
lastComponent
||
React
.
Fragment
,
{},
null
)
layout
.
path
.
endsWith
(
'/page'
)
?
(
children
)
:
(
// TODO: only provide the part of the url that is relevant to the layout (see layout-router.client.tsx)
<
LayoutRouter
initialUrl=
{
pathname
}
layoutPath=
{
layout
.
path
}
>
{
children
}
</
LayoutRouter
>
)
)
)
}
}
// TODO: loading state
// TODO: loading state
...
@@ -357,8 +360,12 @@ export async function renderToHTML(
...
@@ -357,8 +360,12 @@ export async function renderToHTML(
const
AppRouter
=
ComponentMod
.
AppRouter
const
AppRouter
=
ComponentMod
.
AppRouter
const
WrappedComponentWithRouter
=
()
=>
{
const
WrappedComponentWithRouter
=
()
=>
{
if
(
flightRouterPath
)
{
return
<
WrappedComponent
/>
}
return
(
return
(
<
AppRouter
initialUrl=
{
req
.
url
}
>
// TODO: verify pathname passed is correct
<
AppRouter
initialUrl=
{
pathname
}
>
<
WrappedComponent
/>
<
WrappedComponent
/>
</
AppRouter
>
</
AppRouter
>
)
)
...
...
packages/next/server/dev/hot-reloader.ts
View file @
647c93e4
...
@@ -590,6 +590,7 @@ export default class HotReloader {
...
@@ -590,6 +590,7 @@ export default class HotReloader {
pages
:
this
.
pagesMapping
,
pages
:
this
.
pagesMapping
,
isServerComponent
,
isServerComponent
,
}),
}),
appDir
:
this
.
config
.
experimental
.
appDir
,
})
})
},
},
onClient
:
()
=>
{
onClient
:
()
=>
{
...
@@ -607,6 +608,7 @@ export default class HotReloader {
...
@@ -607,6 +608,7 @@ export default class HotReloader {
),
),
absolutePagePath
:
clientLoader
,
absolutePagePath
:
clientLoader
,
})}
!`
+
clientLoader
,
})}
!`
+
clientLoader
,
appDir
:
this
.
config
.
experimental
.
appDir
,
})
})
}
else
{
}
else
{
entries
[
pageKey
].
status
=
BUILDING
entries
[
pageKey
].
status
=
BUILDING
...
@@ -617,6 +619,7 @@ export default class HotReloader {
...
@@ -617,6 +619,7 @@ export default class HotReloader {
absolutePagePath
,
absolutePagePath
,
page
,
page
,
}),
}),
appDir
:
this
.
config
.
experimental
.
appDir
,
})
})
}
}
},
},
...
@@ -644,6 +647,7 @@ export default class HotReloader {
...
@@ -644,6 +647,7 @@ export default class HotReloader {
pageExtensions
:
this
.
config
.
pageExtensions
,
pageExtensions
:
this
.
config
.
pageExtensions
,
})
})
:
request
,
:
request
,
appDir
:
this
.
config
.
experimental
.
appDir
,
})
})
},
},
})
})
...
...
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