Skip to content

Commit

Permalink
Add router transition flag to App router
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyRoller committed Apr 17, 2024
1 parent ab344ea commit a3cbe12
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
17 changes: 10 additions & 7 deletions packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import type { ReactNode } from 'react'
import { type ReactNode, useTransition } from 'react'
import React, {
use,
useEffect,
Expand Down Expand Up @@ -321,6 +321,7 @@ function Router({
)
const [reducerState, dispatch, sync] =
useReducerWithReduxDevtools(initialState)
const [isRouterTransitioning, startRouterTransition] = useTransition()

useEffect(() => {
// Ensure initialParallelRoutes is cleaned up from memory once it's used.
Expand Down Expand Up @@ -353,6 +354,8 @@ function Router({
*/
const appRouter = useMemo<AppRouterInstance>(() => {
const routerInstance: AppRouterInstance = {
isRouterTransitioning,
startRouterTransition,
back: () => window.history.back(),
forward: () => window.history.forward(),
prefetch: (href, options) => {
Expand All @@ -369,7 +372,7 @@ function Router({
if (isExternalURL(url)) {
return
}
startTransition(() => {
startRouterTransition(() => {
dispatch({
type: ACTION_PREFETCH,
url,
Expand All @@ -378,17 +381,17 @@ function Router({
})
},
replace: (href, options = {}) => {
startTransition(() => {
startRouterTransition(() => {
navigate(href, 'replace', options.scroll ?? true)
})
},
push: (href, options = {}) => {
startTransition(() => {
startRouterTransition(() => {
navigate(href, 'push', options.scroll ?? true)
})
},
refresh: () => {
startTransition(() => {
startRouterTransition(() => {
dispatch({
type: ACTION_REFRESH,
origin: window.location.origin,
Expand All @@ -401,7 +404,7 @@ function Router({
'fastRefresh can only be used in development mode. Please use refresh instead.'
)
} else {
startTransition(() => {
startRouterTransition(() => {
dispatch({
type: ACTION_FAST_REFRESH,
origin: window.location.origin,
Expand All @@ -412,7 +415,7 @@ function Router({
}

return routerInstance
}, [dispatch, navigate])
}, [dispatch, isRouterTransitioning, navigate])

useEffect(() => {
// Exists for debugging purposes. Don't use in application code.
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ function linkClicked(
}
}

if (isAppRouter) {
React.startTransition(navigate)
if (isAppRouter && 'startRouterTransition' in router) {
router.startRouterTransition(navigate)
} else {
navigate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from '../../client/components/router-reducer/router-reducer-types'
import type { FetchServerResponseResult } from '../../client/components/router-reducer/fetch-server-response'
import type { FlightRouterState } from '../../server/app-render/types'
import React from 'react'
import React, { type TransitionStartFunction } from 'react'
import type { ErrorComponent } from '../../client/components/error-boundary'

export type ChildSegmentMap = Map<string, CacheNode>
Expand Down Expand Up @@ -126,6 +126,8 @@ export interface PrefetchOptions {
}

export interface AppRouterInstance {
isRouterTransitioning: boolean
startRouterTransition: TransitionStartFunction
/**
* Navigate to the previous history entry.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/shared/lib/router/adapters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export function adaptForAppRouterInstance(
pagesRouter: NextRouter
): AppRouterInstance {
return {
isRouterTransitioning: false,
startRouterTransition: () => () => {},
back() {
pagesRouter.back()
},
Expand Down

0 comments on commit a3cbe12

Please sign in to comment.