Skip to content

Commit

Permalink
Merge pull request #30068 from storybookjs/valentin/support-nextjs-15.2
Browse files Browse the repository at this point in the history
Next.js: Support v15.1.1
  • Loading branch information
valentinpalkovic authored Dec 16, 2024
2 parents dedad9e + fa46d28 commit 302c65c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ function getSelectedParams(currentTree: FlightRouterState, params: Params = {}):
continue;
}

// Ensure catchAll and optional catchall are turned into an array

// Ensure catchAll and optional catchall are turned into an array
const isCatchAll = isDynamicParameter && (segment[2] === 'c' || segment[2] === 'oc');

Expand Down Expand Up @@ -82,6 +80,16 @@ export const AppRouterProvider: React.FC<React.PropsWithChildren<AppRouterProvid
return getSelectedParams(tree);
}, [tree]);

const newLazyCacheNode = {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
};

// https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/app-router.tsx#L436
return (
<PathParamsContext.Provider value={pathParams}>
Expand All @@ -106,10 +114,18 @@ export const AppRouterProvider: React.FC<React.PropsWithChildren<AppRouterProvid
<AppRouterContext.Provider value={getRouter()}>
<LayoutRouterContext.Provider
value={{
// TODO Remove when dropping Next.js < v15.1.1
childNodes: new Map(),
loading: null,
tree,
// TODO END

// START Next.js v15.2 support
// @ts-expect-error Only available in Next.js >= v15.1.1
parentTree: tree,
parentCacheNode: newLazyCacheNode,
// END
url: pathname,
loading: null,
}}
>
{children}
Expand Down
18 changes: 18 additions & 0 deletions code/frameworks/nextjs/src/routing/app-router-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ export const AppRouterProvider: React.FC<React.PropsWithChildren<AppRouterProvid
return getSelectedParams(tree);
}, [tree]);

const newLazyCacheNode = {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
};

// https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/app-router.tsx#L436
return (
<PathParamsContext.Provider value={pathParams}>
Expand All @@ -104,8 +114,16 @@ export const AppRouterProvider: React.FC<React.PropsWithChildren<AppRouterProvid
<AppRouterContext.Provider value={getRouter()}>
<LayoutRouterContext.Provider
value={{
// TODO Remove when dropping Next.js < v15.1.1
childNodes: new Map(),
tree,
// TODO END

// START Next.js v15.2 support
// @ts-expect-error Only available in Next.js >= v15.1.1
parentTree: tree,
parentCacheNode: newLazyCacheNode,
// END
url: pathname,
loading: null,
}}
Expand Down

0 comments on commit 302c65c

Please sign in to comment.