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

Fix Utility Types #11077

Merged
merged 3 commits into from
Apr 6, 2020
Merged
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
9 changes: 1 addition & 8 deletions packages/next/build/plugins/collect-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ async function collectPluginMeta(
}
}

type SeparatedPlugins = {
appMiddlewarePlugins: PluginMetaData[]
documentMiddlewarePlugins: PluginMetaData[]
}

// clean package name so it can be used as variable
export const getPluginId = (pkg: string): string => {
pkg = pkg.replace(/\W/g, '')
Expand Down Expand Up @@ -254,6 +249,4 @@ async function _collectPlugins(

// only execute it once between server/client configs
// since the plugins need to match
export const collectPlugins = execOnce(
_collectPlugins
) as typeof _collectPlugins
export const collectPlugins = execOnce(_collectPlugins)
18 changes: 10 additions & 8 deletions packages/next/next-server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,19 @@ export type NextApiHandler<T = any> = (
/**
* Utils
*/
export function execOnce(this: any, fn: (...args: any) => any) {
export function execOnce<T extends (...args: any[]) => ReturnType<T>>(
fn: T
): T {
let used = false
let result: any = null
let result: ReturnType<T>

return (...args: any) => {
return ((...args: any[]) => {
if (!used) {
used = true
result = fn.apply(this, args)
result = fn(...args)
}
return result
}
}) as T
}

export function getLocationOrigin() {
Expand All @@ -260,7 +262,7 @@ export function getURL() {
return href.substring(origin.length)
}

export function getDisplayName(Component: ComponentType<any>) {
export function getDisplayName<P>(Component: ComponentType<P>) {
return typeof Component === 'string'
? Component
: Component.displayName || Component.name || 'Unknown'
Expand Down Expand Up @@ -293,7 +295,7 @@ export async function loadGetInitialProps<
pageProps: await loadGetInitialProps(ctx.Component, ctx.ctx),
}
}
return {} as any
return {} as IP
}

const props = await App.getInitialProps(ctx)
Expand Down Expand Up @@ -353,7 +355,7 @@ export function formatWithValidation(
}
}

return format(url as any, options)
return format(url as URL, options)
}

export const SP = typeof performance !== 'undefined'
Expand Down