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

Fixes issue with makeStylesheetInert #32027

Merged
merged 7 commits into from
Jan 3, 2022
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
27 changes: 18 additions & 9 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,21 +452,30 @@ export class Head extends Component<
makeStylesheetInert(node: ReactNode): ReactNode[] {
return React.Children.map(node, (c: any) => {
if (
c.type === 'link' &&
c.props['href'] &&
c?.type === 'link' &&
c?.props?.href &&
OPTIMIZED_FONT_PROVIDERS.some(({ url }) =>
c.props['href'].startsWith(url)
c?.props?.href?.startsWith(url)
)
) {
const newProps = { ...(c.props || {}) }
newProps['data-href'] = newProps['href']
newProps['href'] = undefined
const newProps = {
...(c.props || {}),
'data-href': c.props.href,
href: undefined,
}

return React.cloneElement(c, newProps)
} else if (c?.props?.children) {
const newProps = {
...(c.props || {}),
children: this.makeStylesheetInert(c.props.children),
}

return React.cloneElement(c, newProps)
} else if (c.props && c.props['children']) {
c.props['children'] = this.makeStylesheetInert(c.props['children'])
}

return c
})
}).filter(Boolean)
}

render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}

render() {
return (
<Html>
<Head>
<>
{false && <script data-href="test"></script>}
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link
rel="preconnect"
href="https://fonts.gstatic.com"
crossOrigin
/>
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap"
rel="stylesheet"
/>
</>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}

export default MyDocument
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Home() {
return (
<h1>
Falsey values contained in an element contained in Head should not result
in an error!
</h1>
)
}
6 changes: 6 additions & 0 deletions test/integration/font-optimization/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,10 @@ describe('Font Optimization', () => {
const { code } = await nextBuild(appDir)
expect(code).toBe(0)
})

test('makeStylesheetInert regression', async () => {
const appDir = join(fixturesDir, 'make-stylesheet-inert-regression')
const { code } = await nextBuild(appDir)
expect(code).toBe(0)
})
})