Skip to content

Commit

Permalink
Change Router's key to public field
Browse files Browse the repository at this point in the history
  • Loading branch information
akfm.sato committed Mar 31, 2022
1 parent b1e51be commit 8542cb5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/api-reference/next/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The following is the definition of the `router` object returned by both [`useRou
- `domainLocales`: `Array<{domain, defaultLocale, locales}>` - Any configured domain locales.
- `isReady`: `boolean` - Whether the router fields are updated client-side and ready for use. Should only be used inside of `useEffect` methods and not for conditionally rendering on the server. See related docs for use case with [automatically statically optimized pages](/docs/advanced-features/automatic-static-optimization.md)
- `isPreview`: `boolean` - Whether the application is currently in [preview mode](/docs/advanced-features/preview-mode.md).
- `key`: `string` - String uniquely representing the current history entry. Reloading will reset it.

The following methods are included inside `router`:

Expand Down
1 change: 1 addition & 0 deletions packages/next/client/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const urlPropertyFields = [
'locales',
'defaultLocale',
'isReady',
'key',
'isPreview',
'isLocaleDomain',
'domainLocales',
Expand Down
3 changes: 3 additions & 0 deletions packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
} from './node-web-streams-helper'
import { ImageConfigContext } from '../shared/lib/image-config-context'
import { FlushEffectsContext } from '../shared/lib/flush-effects'
import { createKey } from '../shared/lib/router/router'

let optimizeAmp: typeof import('./optimize-amp').default
let getFontDefinitionFromManifest: typeof import('./font-utils').getFontDefinitionFromManifest
Expand Down Expand Up @@ -121,6 +122,7 @@ class ServerRouter implements NextRouter {
domainLocales?: DomainLocale[]
isPreview: boolean
isLocaleDomain: boolean
key: string

constructor(
pathname: string,
Expand Down Expand Up @@ -149,6 +151,7 @@ class ServerRouter implements NextRouter {
this.domainLocales = domainLocales
this.isPreview = !!isPreview
this.isLocaleDomain = !!isLocaleDomain
this.key = createKey()
}

push(): any {
Expand Down
6 changes: 3 additions & 3 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ export type NextRouter = BaseRouter &
| 'isFallback'
| 'isReady'
| 'isPreview'
| 'key'
>

export type PrefetchOptions = {
Expand Down Expand Up @@ -597,7 +598,7 @@ interface NextDataCache {

type Key = string

function createKey() {
export function createKey() {
return Math.random().toString(36).slice(2, 10)
}

Expand Down Expand Up @@ -629,6 +630,7 @@ export default class Router implements BaseRouter {
domainLocales?: DomainLocale[]
isReady: boolean
isLocaleDomain: boolean
key: Key

private state: Readonly<{
route: string
Expand All @@ -640,8 +642,6 @@ export default class Router implements BaseRouter {
isPreview: boolean
}>

private key: Key

static events: MittEmitter<RouterEvent> = mitt()

constructor(
Expand Down

0 comments on commit 8542cb5

Please sign in to comment.