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

Dedupe ComponentRes type #16148

Merged
merged 3 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/next/client/page-loader.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 15 additions & 19 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -129,8 +130,6 @@ function tryParseRelativeUrl(
}
}

type ComponentRes = { page: ComponentType; mod: any }

export type BaseRouter = {
route: string
pathname: string
Expand Down Expand Up @@ -697,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

Expand Down Expand Up @@ -853,7 +849,7 @@ export default class Router implements BaseRouter {
])
}

async fetchComponent(route: string): Promise<ComponentRes> {
async fetchComponent(route: string): Promise<GoodPageCache> {
let cancelled = false
const cancel = (this.clc = () => {
cancelled = true
Expand Down