Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

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 (vercel#26608)

Fixes vercel#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 authored Jun 25, 2021
1 parent bb62d9a commit 8b50932
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 8b50932

Please sign in to comment.