Improve code quality of _document (#12342)

This commit is contained in:
Kiarash Zarinmehr 2020-05-02 09:22:56 +04:30 committed by GitHub
parent 98233b914e
commit 943209c055
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -261,22 +261,20 @@ export class Head extends Component<
})
: []
return preloadFiles.length === 0
return !preloadFiles.length
? null
: preloadFiles.map((file: string) => {
return (
<link
key={file}
nonce={this.props.nonce}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(
file
)}${_devOnlyInvalidateCacheQueryString}`}
as="script"
crossOrigin={this.props.crossOrigin || process.crossOrigin}
/>
)
})
: preloadFiles.map((file: string) => (
<link
key={file}
nonce={this.props.nonce}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(
file
)}${_devOnlyInvalidateCacheQueryString}`}
as="script"
crossOrigin={this.props.crossOrigin || process.crossOrigin}
/>
))
}
getFidPolyfill(): JSX.Element | null {
@ -315,9 +313,8 @@ export class Head extends Component<
// show a warning if Head contains <title> (only in development)
if (process.env.NODE_ENV !== 'production') {
children = React.Children.map(children, (child: any) => {
const isReactHelmet =
child && child.props && child.props['data-react-helmet']
if (child && child.type === 'title' && !isReactHelmet) {
const isReactHelmet = child?.props?.['data-react-helmet']
if (child?.type === 'title' && !isReactHelmet) {
console.warn(
"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<
Array.isArray(styles.props.children)
) {
const hasStyles = (el: React.ReactElement) =>
el &&
el.props &&
el.props.dangerouslySetInnerHTML &&
el.props.dangerouslySetInnerHTML.__html
el?.props?.dangerouslySetInnerHTML?.__html
// @ts-ignore Property 'props' does not exist on type ReactElement
styles.props.children.forEach((child: React.ReactElement) => {
if (Array.isArray(child)) {
child.map(el => hasStyles(el) && curStyles.push(el))
child.forEach(el => hasStyles(el) && curStyles.push(el))
} else if (hasStyles(child)) {
curStyles.push(child)
}
@ -844,13 +838,10 @@ export class NextScript extends Component<OriginProps> {
}
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) {
if (page === '/') {
return buildId ? `/index.${buildId}.js` : '/index.js'
}
return buildId ? `${page}.${buildId}.js` : `${page}.js`
const startingUrl = page === '/' ? '/index' : page
return buildId ? `${startingUrl}.${buildId}.js` : `${startingUrl}.js`
}