Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: backwards compatible router-dep hook support
- Loading branch information
Showing
8 changed files
with
142 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useContext } from 'react' | ||
import { | ||
AppRouterContext, | ||
AppRouterInstance, | ||
} from '../../shared/lib/app-router-context' | ||
|
||
/** | ||
* useAppRouter will get the AppRouterInstance on the context if it's mounted. | ||
* If it is not mounted, it will throw an error. This method should only be used | ||
* when you expect only to have the app router mounted (not pages router). | ||
* | ||
* @returns the app router instance | ||
*/ | ||
export function useAppRouter(): AppRouterInstance { | ||
const router = useContext(AppRouterContext) | ||
if (!router) { | ||
throw new Error('invariant expected app router to be mounted') | ||
} | ||
|
||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { AppRouterInstance } from '../../shared/lib/app-router-context' | ||
import { NextRouter } from '../router' | ||
|
||
export const HYBRID_ROUTER_TYPE = Symbol('HYBRID_ROUTER_TYPE') | ||
|
||
type MaskedHybridRouter<T extends string, Router, OtherRouter> = { | ||
// Store the router type on the router via this private symbol. | ||
[HYBRID_ROUTER_TYPE]: T | ||
} & Router & | ||
// Add partial fields of the other router so it's type-compatible with it, but | ||
// it will show those extra fields as undefined. | ||
Partial<Omit<OtherRouter, keyof Router>> | ||
|
||
export type HybridRouter = | ||
| MaskedHybridRouter<'app', AppRouterInstance, NextRouter> | ||
| MaskedHybridRouter<'pages', NextRouter, AppRouterInstance> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters