Skip to content

Commit b384cea

Browse files
committed
Add check for ObjectExpression when iterating on <link> tags for font optimization
Fixes #26547
1 parent 2f03bfa commit b384cea

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

packages/next/build/webpack/plugins/font-stylesheet-gathering-plugin.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,27 @@ export class FontStylesheetGatheringPlugin {
8989
}
9090

9191
// node.arguments[0] is the name of the tag and [1] are the props.
92-
const propsNode = node.arguments[1] as namedTypes.ObjectExpression
92+
const arg1 = node.arguments[1]
93+
94+
const propsNode =
95+
arg1.type === 'ObjectExpression'
96+
? (arg1 as namedTypes.ObjectExpression)
97+
: undefined
9398
const props: { [key: string]: string } = {}
94-
propsNode.properties.forEach((prop) => {
95-
if (prop.type !== 'Property') {
96-
return
97-
}
98-
if (
99-
prop.key.type === 'Identifier' &&
100-
prop.value.type === 'Literal'
101-
) {
102-
props[prop.key.name] = prop.value.value as string
103-
}
104-
})
99+
if (propsNode) {
100+
propsNode.properties.forEach((prop) => {
101+
if (prop.type !== 'Property') {
102+
return
103+
}
104+
if (
105+
prop.key.type === 'Identifier' &&
106+
prop.value.type === 'Literal'
107+
) {
108+
props[prop.key.name] = prop.value.value as string
109+
}
110+
})
111+
}
112+
105113
if (
106114
!props.rel ||
107115
props.rel !== 'stylesheet' ||
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Document, { Html, Head, Main, NextScript } from 'next/document'
2+
3+
class MyDocument extends Document {
4+
static async getInitialProps(ctx) {
5+
const initialProps = await Document.getInitialProps(ctx)
6+
return { ...initialProps }
7+
}
8+
9+
render() {
10+
const things = { className: 'test' }
11+
return (
12+
<Html>
13+
<Head />
14+
<link {...things} />
15+
<body>
16+
<Main />
17+
<NextScript />
18+
</body>
19+
</Html>
20+
)
21+
}
22+
}
23+
24+
export default MyDocument
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Home() {
2+
return <h1>Hello</h1>
3+
}

test/integration/font-optimization/test/index.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,10 @@ describe('Font Optimization', () => {
328328
})
329329
}
330330
)
331+
332+
test('Spread operator regression on <link>', async () => {
333+
const appDir = join(fixturesDir, 'spread-operator-regression')
334+
const { code } = await nextBuild(appDir)
335+
expect(code).toBe(0)
336+
})
331337
})

0 commit comments

Comments
 (0)