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

refactor: move HtmlContext #34482

Merged
merged 5 commits into from Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 4 additions & 3 deletions packages/next/pages/_document.tsx
@@ -1,18 +1,19 @@
import React, { Component, ReactElement, ReactNode, useContext } from 'react'
import { OPTIMIZED_FONT_PROVIDERS } from '../shared/lib/constants'
import {
import type {
DocumentContext,
DocumentInitialProps,
DocumentProps,
HtmlContext,
HtmlProps,
} from '../shared/lib/utils'
import { BuildManifest, getPageFiles } from '../server/get-page-files'
import { cleanAmpPath } from '../server/utils'
import { htmlEscapeJsonString } from '../server/htmlescape'
import Script, { ScriptProps } from '../client/script'
import isError from '../lib/is-error'

import { HtmlContext } from '../shared/lib/html-context'
import type { HtmlProps } from '../shared/lib/html-context'

export { DocumentContext, DocumentInitialProps, DocumentProps }

export type OriginProps = {
Expand Down
6 changes: 4 additions & 2 deletions packages/next/server/render.tsx
Expand Up @@ -37,15 +37,17 @@ import {
DocumentInitialProps,
DocumentProps,
DocumentContext,
HtmlContext,
HtmlProps,
getDisplayName,
isResSent,
loadGetInitialProps,
NextComponentType,
RenderPage,
RenderPageResult,
} from '../shared/lib/utils'

import { HtmlContext } from '../shared/lib/html-context'
import type { HtmlProps } from '../shared/lib/html-context'

import type { NextApiRequestCookies, __ApiPreviewProps } from './api-utils'
import { denormalizePagePath } from './denormalize-page-path'
import type { FontManifest } from './font-utils'
Expand Down
42 changes: 42 additions & 0 deletions packages/next/shared/lib/html-context.ts
@@ -0,0 +1,42 @@
import type { BuildManifest } from '../../server/get-page-files'
import type { NEXT_DATA, MaybeDeferContentHook } from './utils'

import { createContext } from 'react'

export type HtmlProps = {
__NEXT_DATA__: NEXT_DATA
dangerousAsPath: string
docComponentsRendered: {
Html?: boolean
Main?: boolean
Head?: boolean
NextScript?: boolean
}
buildManifest: BuildManifest
ampPath: string
inAmpMode: boolean
hybridAmp: boolean
isDevelopment: boolean
dynamicImports: string[]
assetPrefix?: string
canonicalBase: string
headTags: any[]
unstable_runtimeJS?: false
unstable_JsPreload?: false
devOnlyCacheBusterQueryString: string
scriptLoader: { afterInteractive?: string[]; beforeInteractive?: any[] }
locale?: string
disableOptimizedLoading?: boolean
styles?: React.ReactElement[] | React.ReactFragment
head?: Array<JSX.Element | null>
useMaybeDeferContent: MaybeDeferContentHook
crossOrigin?: string
optimizeCss?: boolean
optimizeFonts?: boolean
runtime?: 'edge' | 'nodejs'
}

export const HtmlContext = createContext<HtmlProps>(null as any)
if (process.env.NODE_ENV !== 'production') {
HtmlContext.displayName = 'HtmlContext'
}
2 changes: 1 addition & 1 deletion packages/next/shared/lib/router/router.ts
Expand Up @@ -22,7 +22,6 @@ import { normalizeLocalePath } from '../i18n/normalize-locale-path'
import mitt from '../mitt'
import {
AppContextType,
formatWithValidation,
getLocationOrigin,
getURL,
loadGetInitialProps,
Expand All @@ -38,6 +37,7 @@ import resolveRewrites from './utils/resolve-rewrites'
import { getRouteMatcher } from './utils/route-matcher'
import { getRouteRegex } from './utils/route-regex'
import { getMiddlewareRegex } from './utils/get-middleware-regex'
import { formatWithValidation } from './utils/format-url'

declare global {
interface Window {
Expand Down
35 changes: 33 additions & 2 deletions packages/next/shared/lib/router/utils/format-url.ts
Expand Up @@ -20,8 +20,8 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

import { UrlObject } from 'url'
import { ParsedUrlQuery } from 'querystring'
import type { UrlObject } from 'url'
import type { ParsedUrlQuery } from 'querystring'
import * as querystring from './querystring'

const slashedProtocols = /https?|ftp|gopher|file/
Expand Down Expand Up @@ -71,3 +71,34 @@ export function formatUrl(urlObj: UrlObject) {

return `${protocol}${host}${pathname}${search}${hash}`
}

export const urlObjectKeys = [
'auth',
'hash',
'host',
'hostname',
'href',
'path',
'pathname',
'port',
'protocol',
'query',
'search',
'slashes',
]

export function formatWithValidation(url: UrlObject): string {
if (process.env.NODE_ENV === 'development') {
if (url !== null && typeof url === 'object') {
Object.keys(url).forEach((key) => {
if (urlObjectKeys.indexOf(key) === -1) {
console.warn(
`Unknown key passed via urlObject into url.format: ${key}`
)
}
})
}
}

return formatUrl(url)
}
74 changes: 1 addition & 73 deletions packages/next/shared/lib/utils.ts
@@ -1,14 +1,11 @@
import type { BuildManifest } from '../../server/get-page-files'
import type { HtmlProps } from './html-context'
import type { ComponentType } from 'react'
import type { DomainLocale } from '../../server/config'
import type { Env } from '@next/env'
import type { IncomingMessage, ServerResponse } from 'http'
import type { NextRouter } from './router/router'
import type { ParsedUrlQuery } from 'querystring'
import type { PreviewData } from 'next/types'
import type { UrlObject } from 'url'
import { createContext } from 'react'
import { formatUrl } from './router/utils/format-url'

export type NextComponentType<
C extends BaseContext = NextPageContext,
Expand Down Expand Up @@ -195,39 +192,6 @@ export type MaybeDeferContentHook = (
contentFn: () => JSX.Element
) => [boolean, JSX.Element]

export type HtmlProps = {
__NEXT_DATA__: NEXT_DATA
dangerousAsPath: string
docComponentsRendered: {
Html?: boolean
Main?: boolean
Head?: boolean
NextScript?: boolean
}
buildManifest: BuildManifest
ampPath: string
inAmpMode: boolean
hybridAmp: boolean
isDevelopment: boolean
dynamicImports: string[]
assetPrefix?: string
canonicalBase: string
headTags: any[]
unstable_runtimeJS?: false
unstable_JsPreload?: false
devOnlyCacheBusterQueryString: string
scriptLoader: { afterInteractive?: string[]; beforeInteractive?: any[] }
locale?: string
disableOptimizedLoading?: boolean
styles?: React.ReactElement[] | React.ReactFragment
head?: Array<JSX.Element | null>
useMaybeDeferContent: MaybeDeferContentHook
crossOrigin?: string
optimizeCss?: boolean
optimizeFonts?: boolean
runtime?: 'edge' | 'nodejs'
}

/**
* Next `API` route request
*/
Expand Down Expand Up @@ -410,37 +374,6 @@ export async function loadGetInitialProps<
return props
}

export const urlObjectKeys = [
'auth',
'hash',
'host',
'hostname',
'href',
'path',
'pathname',
'port',
'protocol',
'query',
'search',
'slashes',
]

export function formatWithValidation(url: UrlObject): string {
if (process.env.NODE_ENV === 'development') {
if (url !== null && typeof url === 'object') {
Object.keys(url).forEach((key) => {
if (urlObjectKeys.indexOf(key) === -1) {
console.warn(
`Unknown key passed via urlObject into url.format: ${key}`
)
}
})
}
}

return formatUrl(url)
}

export const SP = typeof performance !== 'undefined'
export const ST =
SP &&
Expand All @@ -449,11 +382,6 @@ export const ST =

export class DecodeError extends Error {}

export const HtmlContext = createContext<HtmlProps>(null as any)
if (process.env.NODE_ENV !== 'production') {
HtmlContext.displayName = 'HtmlContext'
}

export interface CacheFs {
readFile(f: string): Promise<string>
readFileSync(f: string): string
Expand Down