-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added new
next/compat/navigation
hooks
- Loading branch information
Showing
6 changed files
with
116 additions
and
56 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,53 @@ | ||
import { useContext, useMemo } from 'react' | ||
import { | ||
PathnameContext, | ||
SearchParamsContext, | ||
} from '../../shared/lib/hooks-client-context' | ||
import { ReadonlyURLSearchParams } from '../components/readonly-url-search-params' | ||
|
||
/** | ||
* useRouter here is already fully backwards compatible in both `pages/` and in | ||
* `app/`. | ||
*/ | ||
export { useRouter } from '../components/navigation' | ||
|
||
/** | ||
* usePathname from `next/compat/navigation`, much like the hook from | ||
* `next/navigation` returns the pathname with the dynamic params substituted | ||
* in. Unlike the hook in `next/navigation`, this will return `null` when | ||
* the pathname is not available. | ||
* | ||
* This can only happen when the hook is used from a pages directory and the | ||
* page being rendered has been automatically statically optimized or the page | ||
* being rendered is the fallback page. | ||
* | ||
* @returns the pathname if available | ||
*/ | ||
export function usePathname(): string | null { | ||
return useContext(PathnameContext) | ||
} | ||
|
||
/** | ||
* useSearchParams from `next/compat/navigation`, much like the hook from | ||
* `next/navigation` returns the URLSearchParams object for the search | ||
* parameters. Unlike the hook in `next/navigation`, this will return `null` | ||
* when the search params are not available. | ||
* | ||
* It will be `null` during prerendering if the page doesn't use Server-side | ||
* Rendering. See https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props | ||
* | ||
* @returns the search params if available | ||
*/ | ||
export function useSearchParams(): URLSearchParams | null { | ||
const searchParams = useContext(SearchParamsContext) | ||
|
||
return useMemo(() => { | ||
if (!searchParams) { | ||
// When the router is not ready in pages, we won't have the search params | ||
// available. | ||
return null | ||
} | ||
|
||
return new ReadonlyURLSearchParams(searchParams) | ||
}, [searchParams]) | ||
} |
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
50 changes: 50 additions & 0 deletions
50
packages/next/client/components/readonly-url-search-params.ts
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,50 @@ | ||
const INTERNAL_URLSEARCHPARAMS_INSTANCE = Symbol( | ||
'internal for urlsearchparams readonly' | ||
) | ||
|
||
function readonlyURLSearchParamsError() { | ||
return new Error('ReadonlyURLSearchParams cannot be modified') | ||
} | ||
|
||
export class ReadonlyURLSearchParams { | ||
[INTERNAL_URLSEARCHPARAMS_INSTANCE]: URLSearchParams | ||
|
||
entries: URLSearchParams['entries'] | ||
forEach: URLSearchParams['forEach'] | ||
get: URLSearchParams['get'] | ||
getAll: URLSearchParams['getAll'] | ||
has: URLSearchParams['has'] | ||
keys: URLSearchParams['keys'] | ||
values: URLSearchParams['values'] | ||
toString: URLSearchParams['toString'] | ||
|
||
constructor(urlSearchParams: URLSearchParams) { | ||
// Since `new Headers` uses `this.append()` to fill the headers object ReadonlyHeaders can't extend from Headers directly as it would throw. | ||
this[INTERNAL_URLSEARCHPARAMS_INSTANCE] = urlSearchParams | ||
|
||
this.entries = urlSearchParams.entries.bind(urlSearchParams) | ||
this.forEach = urlSearchParams.forEach.bind(urlSearchParams) | ||
this.get = urlSearchParams.get.bind(urlSearchParams) | ||
this.getAll = urlSearchParams.getAll.bind(urlSearchParams) | ||
this.has = urlSearchParams.has.bind(urlSearchParams) | ||
this.keys = urlSearchParams.keys.bind(urlSearchParams) | ||
this.values = urlSearchParams.values.bind(urlSearchParams) | ||
this.toString = urlSearchParams.toString.bind(urlSearchParams) | ||
} | ||
[Symbol.iterator]() { | ||
return this[INTERNAL_URLSEARCHPARAMS_INSTANCE][Symbol.iterator]() | ||
} | ||
|
||
append() { | ||
throw readonlyURLSearchParamsError() | ||
} | ||
delete() { | ||
throw readonlyURLSearchParamsError() | ||
} | ||
set() { | ||
throw readonlyURLSearchParamsError() | ||
} | ||
sort() { | ||
throw readonlyURLSearchParamsError() | ||
} | ||
} |
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 @@ | ||
export * from '../dist/client/compat/navigation' |
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 @@ | ||
module.exports = require('../dist/client/compat/navigation') |
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