-
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.
When you're trying to migrate an application from `pages/` to `app/`, you'll need to access data like search parameters and the pathname in a way that lets you migrate safely. This adds support for dynamic typing of some of those exported functions from `next/navigation`, namely `useSearchParams` and `usePathname`. Currently, `searchParams` can’t be known when prerendering if the page doesn’t use [Server-side Rendering](https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props) in the `pages/` directory. `pathname` can’t be known during prerendering if the page is a fallback page or has been automatically statically optimized when accessed from `pages/`. To make migraitons easier, this adds a new feature to `next dev` that will automatically add the correct types for `next/navigation`. It does this by checking if you have both a `app/` and `pages/` directory. If it detects you have a `app/` directory, it will also enable the suggested Typescript feature, [`structNullChecks`](https://www.typescriptlang.org/tsconfig#strictNullChecks) which will warn developers when trying to access a value that may be `null`. --------- Co-authored-by: JJ Kasper <[email protected]>
- Loading branch information
Showing
11 changed files
with
187 additions
and
37 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,24 @@ | ||
import type { ReadonlyURLSearchParams } from 'next/navigation' | ||
|
||
declare module 'next/navigation' { | ||
/** | ||
* Get a read-only URLSearchParams object. For example searchParams.get('foo') would return 'bar' when ?foo=bar | ||
* Learn more about URLSearchParams here: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams | ||
* | ||
* If used from `pages/`, the hook may return `null` when the router is not | ||
* ready. | ||
*/ | ||
export function useSearchParams(): ReadonlyURLSearchParams | null | ||
|
||
/** | ||
* Get the current pathname. For example, if the URL is | ||
* https://example.com/foo?bar=baz, the pathname would be /foo. | ||
* | ||
* If the hook is accessed from `pages/`, the pathname may be `null` when the | ||
* router is not ready. | ||
*/ | ||
export function usePathname(): string | null | ||
|
||
// Re-export the types for next/navigation. | ||
export * from 'next/dist/client/components/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,18 @@ | ||
import type { ReadonlyURLSearchParams } from 'next/navigation' | ||
|
||
declare module 'next/navigation' { | ||
/** | ||
* Get a read-only URLSearchParams object. For example searchParams.get('foo') would return 'bar' when ?foo=bar | ||
* Learn more about URLSearchParams here: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams | ||
*/ | ||
export function useSearchParams(): ReadonlyURLSearchParams | ||
|
||
/** | ||
* Get the current pathname. For example, if the URL is | ||
* https://example.com/foo?bar=baz, the pathname would be /foo. | ||
*/ | ||
export function usePathname(): string | ||
|
||
// Re-export the types for next/navigation. | ||
export * from 'next/dist/client/components/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
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
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