diff --git a/packages/next/client/router.ts b/packages/next/client/router.ts index 22a8ada7b4220..bfb7f69e5f890 100644 --- a/packages/next/client/router.ts +++ b/packages/next/client/router.ts @@ -48,6 +48,7 @@ const urlPropertyFields = [ 'isPreview', 'isLocaleDomain', 'domainLocales', + 'data', ] const routerEvents = [ 'routeChangeStart', diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index 5e858c1f60b32..3002c8e4849e6 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -54,6 +54,7 @@ interface TransitionOptions { shallow?: boolean locale?: string | false scroll?: boolean + data?: object } interface NextHistoryState { @@ -453,6 +454,7 @@ export type BaseRouter = { defaultLocale?: string domainLocales?: DomainLocale[] isLocaleDomain: boolean + data: object } export type NextRouter = BaseRouter & @@ -468,6 +470,7 @@ export type NextRouter = BaseRouter & | 'isFallback' | 'isReady' | 'isPreview' + | 'data' > export type PrefetchOptions = { @@ -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 @@ -872,6 +877,7 @@ export default class Router implements BaseRouter { * Go back in history */ back() { + this.data = {} window.history.back() } @@ -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.