Skip to content

Commit

Permalink
feat: expose basePath in useRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
Netail committed Jan 9, 2025
1 parent fa422ce commit f9e562f
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/01-app/04-api-reference/04-functions/use-router.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default function Page() {
- `router.prefetch(href: string)`: [Prefetch](/docs/app/building-your-application/routing/linking-and-navigating#2-prefetching) the provided route for faster client-side transitions.
- `router.back()`: Navigate back to the previous route in the browser’s history stack.
- `router.forward()`: Navigate forwards to the next page in the browser’s history stack.
- `router.basePath`: The active [basePath](/docs/app/api-reference/config/next-config-js/basePath) (if enabled).

> **Good to know**:
>
Expand Down
2 changes: 1 addition & 1 deletion docs/02-pages/03-api-reference/03-functions/use-router.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The following is the definition of the `router` object returned by both [`useRou
- `query`: `Object` - The query string parsed to an object, including [dynamic route](/docs/pages/building-your-application/routing/dynamic-routes) parameters. It will be an empty object during prerendering if the page doesn't use [Server-side Rendering](/docs/pages/building-your-application/data-fetching/get-server-side-props). Defaults to `{}`
- `asPath`: `String` - The path as shown in the browser including the search params and respecting the `trailingSlash` configuration. `basePath` and `locale` are not included.
- `isFallback`: `boolean` - Whether the current page is in [fallback mode](/docs/pages/api-reference/functions/get-static-paths#fallback-true).
- `basePath`: `String` - The active [basePath](/docs/app/api-reference/config/next-config-js/basePath) (if enabled).
- `basePath`: `String` - The active [basePath](/docs/pages/api-reference/config/next-config-js/basePath) (if enabled).
- `locale`: `String` - The active locale (if enabled).
- `locales`: `String[]` - All supported locales (if enabled).
- `defaultLocale`: `String` - The current default locale (if enabled).
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ function Router({
})
}
},
basePath: process.env.__NEXT_ROUTER_BASEPATH || '',
}

return routerInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export interface AppRouterInstance {
* Prefetch the provided href.
*/
prefetch(href: string, options?: PrefetchOptions): void
/**
* The configured app's basePath.
*/
basePath: string
}

export const AppRouterContext = React.createContext<AppRouterInstance | null>(
Expand Down
6 changes: 6 additions & 0 deletions packages/next/src/shared/lib/router/adapters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('adaptForAppRouterInstance', () => {
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
basePath: '/base-path',
} as unknown as NextRouter

const adapter = adaptForAppRouterInstance(router)
Expand Down Expand Up @@ -62,4 +63,9 @@ describe('adaptForAppRouterInstance', () => {
adapter.prefetch('/foo')
expect(router.prefetch).toHaveBeenCalledWith('/foo')
})

it('should forward the basePath to `basePath`', () => {
const basePath = adapter.basePath
expect(basePath).toEqual('/base-path')
})
})
1 change: 1 addition & 0 deletions packages/next/src/shared/lib/router/adapters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function adaptForAppRouterInstance(
prefetch(href) {
void pagesRouter.prefetch(href)
},
basePath: pagesRouter.basePath,
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/e2e/app-dir/app-basepath/app/page.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import Link from 'next/link'
import { useRouter } from 'next/navigation'

export default function Page() {
const router = useRouter()

return (
<div>
<h1>Test Page</h1>
<p>basePath: {router.basePath}</p>
<Link href="/another">Go to page 2</Link>
</div>
)
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-basepath/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ describe('app dir - basepath', () => {
expect(html).toContain('<h1>Test Page</h1>')
})

it('useRouter should correctly return `basePath`', async () => {
const html = await next.render('/base')
expect(html).toContain('<p>basePath: /base</p>')
})

it('should support Link with basePath prefixed', async () => {
const browser = await next.browser('/base')
expect(
Expand Down

0 comments on commit f9e562f

Please sign in to comment.