Skip to content

Commit

Permalink
Add check for ObjectExpression when iterating on <link> tags for font…
Browse files Browse the repository at this point in the history
… optimization (#26608)

Fixes #26547



## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

## 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.

## Documentation / Examples

- [ ] Make sure the linting passes
  • Loading branch information
timneutkens committed Jun 25, 2021
1 parent 2f03bfa commit 170dc0d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
Expand Up @@ -89,19 +89,27 @@ export class FontStylesheetGatheringPlugin {
}

// node.arguments[0] is the name of the tag and [1] are the props.
const propsNode = node.arguments[1] as namedTypes.ObjectExpression
const arg1 = node.arguments[1]

const propsNode =
arg1.type === 'ObjectExpression'
? (arg1 as namedTypes.ObjectExpression)
: undefined
const props: { [key: string]: string } = {}
propsNode.properties.forEach((prop) => {
if (prop.type !== 'Property') {
return
}
if (
prop.key.type === 'Identifier' &&
prop.value.type === 'Literal'
) {
props[prop.key.name] = prop.value.value as string
}
})
if (propsNode) {
propsNode.properties.forEach((prop) => {
if (prop.type !== 'Property') {
return
}
if (
prop.key.type === 'Identifier' &&
prop.value.type === 'Literal'
) {
props[prop.key.name] = prop.value.value as string
}
})
}

if (
!props.rel ||
props.rel !== 'stylesheet' ||
Expand Down
@@ -0,0 +1,24 @@
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() {
const things = { className: 'test' }
return (
<Html>
<Head />
<link {...things} />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}

export default MyDocument
@@ -0,0 +1,3 @@
export default function Home() {
return <h1>Hello</h1>
}
6 changes: 6 additions & 0 deletions test/integration/font-optimization/test/index.test.js
Expand Up @@ -328,4 +328,10 @@ describe('Font Optimization', () => {
})
}
)

test('Spread operator regression on <link>', async () => {
const appDir = join(fixturesDir, 'spread-operator-regression')
const { code } = await nextBuild(appDir)
expect(code).toBe(0)
})
})

0 comments on commit 170dc0d

Please sign in to comment.