Skip to content

Commit

Permalink
fix(document): accepts nodearray for head's children (#35424)
Browse files Browse the repository at this point in the history
This PR attempts to amends #35390. 

There's new property 'children' in `OriginProps` : https://github.com/vercel/next.js/blob/canary/packages/next/pages/_document.tsx#L22 which overwrites default prop's children defined at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L504. Those 2 are incompatible, especially new one doesn't allow iterable children which results multiple children in head tag makes compilation fails.

PR matches type of new property same as default, then apply some workaround to bend over internal check logics. I'm not sure if it's worth to apply strict types as much or just bend it via `any` casting, PR tried to be strict as much.

## Bug
- closes #35390

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
kwonoj committed Mar 18, 2022
1 parent f2fee3d commit 9c2949a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/next/pages/_document.tsx
Expand Up @@ -19,7 +19,7 @@ export { DocumentContext, DocumentInitialProps, DocumentProps }
export type OriginProps = {
nonce?: string
crossOrigin?: string
children?: React.ReactElement
children?: React.ReactNode
}

type DocumentFiles = {
Expand Down Expand Up @@ -72,6 +72,10 @@ function getPolyfillScripts(context: HtmlProps, props: OriginProps) {
))
}

function hasComponentProps(child: any): child is React.ReactElement {
return !!child && !!child.props
}

function getPreNextWorkerScripts(context: HtmlProps, props: OriginProps) {
const { assetPrefix, scriptLoader, crossOrigin, nextScriptWorkers } = context

Expand All @@ -88,7 +92,8 @@ function getPreNextWorkerScripts(context: HtmlProps, props: OriginProps) {

// Check to see if the user has defined their own Partytown configuration
const userDefinedConfig = children.find(
(child: React.ReactElement) =>
(child) =>
hasComponentProps(child) &&
child?.props?.dangerouslySetInnerHTML?.__html.length &&
'data-partytown-config' in child.props
)
Expand Down

0 comments on commit 9c2949a

Please sign in to comment.