Unverified Commit 943209c0 authored by Kiarash Zarinmehr's avatar Kiarash Zarinmehr Committed by GitHub

Improve code quality of _document (#12342)

parent 98233b91
...@@ -261,22 +261,20 @@ export class Head extends Component< ...@@ -261,22 +261,20 @@ export class Head extends Component<
}) })
: [] : []
return preloadFiles.length === 0 return !preloadFiles.length
? null ? null
: preloadFiles.map((file: string) => { : preloadFiles.map((file: string) => (
return ( <link
<link key={file}
key={file} nonce={this.props.nonce}
nonce={this.props.nonce} rel="preload"
rel="preload" href={`${assetPrefix}/_next/${encodeURI(
href={`${assetPrefix}/_next/${encodeURI( file
file )}${_devOnlyInvalidateCacheQueryString}`}
)}${_devOnlyInvalidateCacheQueryString}`} as="script"
as="script" crossOrigin={this.props.crossOrigin || process.crossOrigin}
crossOrigin={this.props.crossOrigin || process.crossOrigin} />
/> ))
)
})
} }
getFidPolyfill(): JSX.Element | null { getFidPolyfill(): JSX.Element | null {
...@@ -315,9 +313,8 @@ export class Head extends Component< ...@@ -315,9 +313,8 @@ export class Head extends Component<
// show a warning if Head contains <title> (only in development) // show a warning if Head contains <title> (only in development)
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
children = React.Children.map(children, (child: any) => { children = React.Children.map(children, (child: any) => {
const isReactHelmet = const isReactHelmet = child?.props?.['data-react-helmet']
child && child.props && child.props['data-react-helmet'] if (child?.type === 'title' && !isReactHelmet) {
if (child && child.type === 'title' && !isReactHelmet) {
console.warn( console.warn(
"Warning: <title> should not be used in _document.js's <Head>. https://err.sh/next.js/no-document-title" "Warning: <title> should not be used in _document.js's <Head>. https://err.sh/next.js/no-document-title"
) )
...@@ -391,14 +388,11 @@ export class Head extends Component< ...@@ -391,14 +388,11 @@ export class Head extends Component<
Array.isArray(styles.props.children) Array.isArray(styles.props.children)
) { ) {
const hasStyles = (el: React.ReactElement) => const hasStyles = (el: React.ReactElement) =>
el && el?.props?.dangerouslySetInnerHTML?.__html
el.props &&
el.props.dangerouslySetInnerHTML &&
el.props.dangerouslySetInnerHTML.__html
// @ts-ignore Property 'props' does not exist on type ReactElement // @ts-ignore Property 'props' does not exist on type ReactElement
styles.props.children.forEach((child: React.ReactElement) => { styles.props.children.forEach((child: React.ReactElement) => {
if (Array.isArray(child)) { if (Array.isArray(child)) {
child.map(el => hasStyles(el) && curStyles.push(el)) child.forEach(el => hasStyles(el) && curStyles.push(el))
} else if (hasStyles(child)) { } else if (hasStyles(child)) {
curStyles.push(child) curStyles.push(child)
} }
...@@ -844,13 +838,10 @@ export class NextScript extends Component<OriginProps> { ...@@ -844,13 +838,10 @@ export class NextScript extends Component<OriginProps> {
} }
function getAmpPath(ampPath: string, asPath: string) { function getAmpPath(ampPath: string, asPath: string) {
return ampPath ? ampPath : `${asPath}${asPath.includes('?') ? '&' : '?'}amp=1` return ampPath || `${asPath}${asPath.includes('?') ? '&' : '?'}amp=1`
} }
function getPageFile(page: string, buildId?: string) { function getPageFile(page: string, buildId?: string) {
if (page === '/') { const startingUrl = page === '/' ? '/index' : page
return buildId ? `/index.${buildId}.js` : '/index.js' return buildId ? `${startingUrl}.${buildId}.js` : `${startingUrl}.js`
}
return buildId ? `${page}.${buildId}.js` : `${page}.js`
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment