Unverified Commit a8d09d73 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix small memory leaks (#52257)

Fix a few KBs (depending on the tree state size) of leaked memory caused by closures. All the marked objects below are caused by `serverActionDispatcher` which are not cleaned up after SSR:

![CleanShot 2023-07-05 at 15 15 17@2x](https://github.com/vercel/next.js/assets/3676859/d6731830-146e-4433-9ab7-19ea878472cf)
parent 6885b98d
......@@ -34,6 +34,7 @@ import {
ACTION_SERVER_ACTION,
ACTION_SERVER_PATCH,
PrefetchKind,
ReducerActions,
RouterChangeByServerResponse,
RouterNavigate,
ServerActionDispatcher,
......@@ -146,6 +147,76 @@ const createEmptyCacheNode = () => ({
parallelRoutes: new Map(),
})
function useServerActionDispatcher(
changeByServerResponse: RouterChangeByServerResponse,
dispatch: React.Dispatch<ReducerActions>,
navigate: RouterNavigate
) {
const serverActionDispatcher: ServerActionDispatcher = useCallback(
(actionPayload) => {
startTransition(() => {
dispatch({
...actionPayload,
type: ACTION_SERVER_ACTION,
mutable: {},
navigate,
changeByServerResponse,
})
})
},
[changeByServerResponse, dispatch, navigate]
)
globalServerActionDispatcher = serverActionDispatcher
}
/**
* Server response that only patches the cache and tree.
*/
function useChangeByServerResponse(
dispatch: React.Dispatch<ReducerActions>
): RouterChangeByServerResponse {
return useCallback(
(
previousTree: FlightRouterState,
flightData: FlightData,
overrideCanonicalUrl: URL | undefined
) => {
startTransition(() => {
dispatch({
type: ACTION_SERVER_PATCH,
flightData,
previousTree,
overrideCanonicalUrl,
cache: createEmptyCacheNode(),
mutable: {},
})
})
},
[dispatch]
)
}
function useNavigate(dispatch: React.Dispatch<ReducerActions>): RouterNavigate {
return useCallback(
(href, navigateType, forceOptimisticNavigation, shouldScroll) => {
const url = new URL(addBasePath(href), location.href)
return dispatch({
type: ACTION_NAVIGATE,
url,
isExternalUrl: isExternalURL(url),
locationSearch: location.search,
forceOptimisticNavigation,
shouldScroll: shouldScroll ?? true,
navigateType,
cache: createEmptyCacheNode(),
mutable: {},
})
},
[dispatch]
)
}
/**
* The global router that wraps the application components.
*/
......@@ -207,64 +278,9 @@ function Router({
}
}, [canonicalUrl])
/**
* Server response that only patches the cache and tree.
*/
const changeByServerResponse: RouterChangeByServerResponse = useCallback(
(
previousTree: FlightRouterState,
flightData: FlightData,
overrideCanonicalUrl: URL | undefined
) => {
startTransition(() => {
dispatch({
type: ACTION_SERVER_PATCH,
flightData,
previousTree,
overrideCanonicalUrl,
cache: createEmptyCacheNode(),
mutable: {},
})
})
},
[dispatch]
)
const navigate: RouterNavigate = useCallback(
(href, navigateType, forceOptimisticNavigation, shouldScroll) => {
const url = new URL(addBasePath(href), location.href)
return dispatch({
type: ACTION_NAVIGATE,
url,
isExternalUrl: isExternalURL(url),
locationSearch: location.search,
forceOptimisticNavigation,
shouldScroll: shouldScroll ?? true,
navigateType,
cache: createEmptyCacheNode(),
mutable: {},
})
},
[dispatch]
)
const serverActionDispatcher: ServerActionDispatcher = useCallback(
(actionPayload) => {
startTransition(() => {
dispatch({
...actionPayload,
type: ACTION_SERVER_ACTION,
mutable: {},
navigate,
changeByServerResponse,
})
})
},
[changeByServerResponse, dispatch, navigate]
)
globalServerActionDispatcher = serverActionDispatcher
const changeByServerResponse = useChangeByServerResponse(dispatch)
const navigate = useNavigate(dispatch)
useServerActionDispatcher(changeByServerResponse, dispatch, navigate)
/**
* The app router that is exposed through `useRouter`. It's only concerned with dispatching actions to the reducer, does not hold state.
......
......@@ -29,12 +29,13 @@ class FakeAsyncLocalStorage<Store extends {}>
}
}
const maybeGlobalAsyncLocalStorage = (globalThis as any).AsyncLocalStorage
export function createAsyncLocalStorage<
Store extends {}
>(): AsyncLocalStorage<Store> {
if ((globalThis as any).AsyncLocalStorage) {
return new (globalThis as any).AsyncLocalStorage()
if (maybeGlobalAsyncLocalStorage) {
return new maybeGlobalAsyncLocalStorage()
}
return new FakeAsyncLocalStorage()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment