Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): Data property added to next-router #36083

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/client/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const urlPropertyFields = [
'isPreview',
'isLocaleDomain',
'domainLocales',
'data',
]
const routerEvents = [
'routeChangeStart',
Expand Down
9 changes: 9 additions & 0 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface TransitionOptions {
shallow?: boolean
locale?: string | false
scroll?: boolean
data?: object
}

interface NextHistoryState {
Expand Down Expand Up @@ -453,6 +454,7 @@ export type BaseRouter = {
defaultLocale?: string
domainLocales?: DomainLocale[]
isLocaleDomain: boolean
data: object
}

export type NextRouter = BaseRouter &
Expand All @@ -468,6 +470,7 @@ export type NextRouter = BaseRouter &
| 'isFallback'
| 'isReady'
| 'isPreview'
| 'data'
>

export type PrefetchOptions = {
Expand Down Expand Up @@ -615,6 +618,8 @@ export default class Router implements BaseRouter {
// In-flight middleware preflight requests
sde: { [asPath: string]: object } = {}

data: object = {}

sub: Subscription
clc: ComponentLoadCancel
pageLoader: PageLoader
Expand Down Expand Up @@ -872,6 +877,7 @@ export default class Router implements BaseRouter {
* Go back in history
*/
back() {
this.data = {}
window.history.back()
}

Expand All @@ -882,6 +888,9 @@ export default class Router implements BaseRouter {
* @param options object you can define `shallow` and other options
*/
push(url: Url, as?: Url, options: TransitionOptions = {}) {
if (options.data) {
this.data = options.data
}
if (process.env.__NEXT_SCROLL_RESTORATION) {
// TODO: remove in the future when we update history before route change
// is complete, as the popstate event should handle this capture.
Expand Down