From b12681301068a11e3069e03852a383c3a886efd9 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 13 Aug 2020 00:24:38 -0400 Subject: [PATCH 1/2] Dedupe ComponentRes type --- packages/next/client/page-loader.ts | 3 ++- .../next/next-server/lib/router/router.ts | 19 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/next/client/page-loader.ts b/packages/next/client/page-loader.ts index e48c6585b6e28..2277d6fb4e0d2 100644 --- a/packages/next/client/page-loader.ts +++ b/packages/next/client/page-loader.ts @@ -1,3 +1,4 @@ +import type { ComponentType } from 'react' import type { ClientSsgManifest } from '../build' import type { ClientBuildManifest } from '../build/webpack/plugins/build-manifest-plugin' import mitt from '../next-server/lib/mitt' @@ -68,7 +69,7 @@ function appendLink( }) } -export type GoodPageCache = { page: any; mod: any } +export type GoodPageCache = { page: ComponentType; mod: any } export type PageCacheEntry = { error: any } | GoodPageCache export default class PageLoader { diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index ae75e2ed9a0d2..c76e393732833 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -3,25 +3,26 @@ import { ParsedUrlQuery } from 'querystring' import { ComponentType } from 'react' import { UrlObject } from 'url' +import { + normalizePathTrailingSlash, + removePathTrailingSlash, +} from '../../../client/normalize-trailing-slash' +import type { GoodPageCache } from '../../../client/page-loader' import mitt, { MittEmitter } from '../mitt' import { AppContextType, formatWithValidation, + getLocationOrigin, getURL, loadGetInitialProps, NextPageContext, ST, - getLocationOrigin, } from '../utils' import { isDynamicRoute } from './utils/is-dynamic' +import { parseRelativeUrl } from './utils/parse-relative-url' +import { searchParamsToUrlQuery } from './utils/querystring' import { getRouteMatcher } from './utils/route-matcher' import { getRouteRegex } from './utils/route-regex' -import { searchParamsToUrlQuery } from './utils/querystring' -import { parseRelativeUrl } from './utils/parse-relative-url' -import { - removePathTrailingSlash, - normalizePathTrailingSlash, -} from '../../../client/normalize-trailing-slash' interface TransitionOptions { shallow?: boolean @@ -129,8 +130,6 @@ function tryParseRelativeUrl( } } -type ComponentRes = { page: ComponentType; mod: any } - export type BaseRouter = { route: string pathname: string @@ -853,7 +852,7 @@ export default class Router implements BaseRouter { ]) } - async fetchComponent(route: string): Promise { + async fetchComponent(route: string): Promise { let cancelled = false const cancel = (this.clc = () => { cancelled = true From 21e7715fb5ceb795e9261c5c02557bdb734930e6 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 13 Aug 2020 00:31:00 -0400 Subject: [PATCH 2/2] Fix type --- packages/next/next-server/lib/router/router.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index c76e393732833..e8f4cc1f56d4b 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -696,16 +696,13 @@ export default class Router implements BaseRouter { return cachedRouteInfo } - const routeInfo = cachedRouteInfo + const routeInfo: RouteInfo = cachedRouteInfo ? cachedRouteInfo - : await this.fetchComponent(route).then( - (res) => - ({ - Component: res.page, - __N_SSG: res.mod.__N_SSG, - __N_SSP: res.mod.__N_SSP, - } as RouteInfo) - ) + : await this.fetchComponent(route).then((res) => ({ + Component: res.page, + __N_SSG: res.mod.__N_SSG, + __N_SSP: res.mod.__N_SSP, + })) const { Component, __N_SSG, __N_SSP } = routeInfo