From 8b50932f556e260cab65cc3530f047b728e9efda Mon Sep 17 00:00:00 2001
From: Tim Neutkens
Date: Fri, 25 Jun 2021 16:51:53 +0200
Subject: [PATCH] Add check for ObjectExpression when iterating on tags
for font 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
---
.../font-stylesheet-gathering-plugin.ts | 32 ++++++++++++-------
.../pages/_document.js | 24 ++++++++++++++
.../spread-operator-regression/pages/index.js | 3 ++
.../font-optimization/test/index.test.js | 6 ++++
4 files changed, 53 insertions(+), 12 deletions(-)
create mode 100644 test/integration/font-optimization/fixtures/spread-operator-regression/pages/_document.js
create mode 100644 test/integration/font-optimization/fixtures/spread-operator-regression/pages/index.js
diff --git a/packages/next/build/webpack/plugins/font-stylesheet-gathering-plugin.ts b/packages/next/build/webpack/plugins/font-stylesheet-gathering-plugin.ts
index a3fff7d54fa99..1b7494826fb5c 100644
--- a/packages/next/build/webpack/plugins/font-stylesheet-gathering-plugin.ts
+++ b/packages/next/build/webpack/plugins/font-stylesheet-gathering-plugin.ts
@@ -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' ||
diff --git a/test/integration/font-optimization/fixtures/spread-operator-regression/pages/_document.js b/test/integration/font-optimization/fixtures/spread-operator-regression/pages/_document.js
new file mode 100644
index 0000000000000..a635c18efb021
--- /dev/null
+++ b/test/integration/font-optimization/fixtures/spread-operator-regression/pages/_document.js
@@ -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 (
+
+
+
+
+
+
+
+
+ )
+ }
+}
+
+export default MyDocument
diff --git a/test/integration/font-optimization/fixtures/spread-operator-regression/pages/index.js b/test/integration/font-optimization/fixtures/spread-operator-regression/pages/index.js
new file mode 100644
index 0000000000000..6ba1cb0bd3e6d
--- /dev/null
+++ b/test/integration/font-optimization/fixtures/spread-operator-regression/pages/index.js
@@ -0,0 +1,3 @@
+export default function Home() {
+ return Hello
+}
diff --git a/test/integration/font-optimization/test/index.test.js b/test/integration/font-optimization/test/index.test.js
index 59fa8ff433ae6..936c8d1a403b3 100644
--- a/test/integration/font-optimization/test/index.test.js
+++ b/test/integration/font-optimization/test/index.test.js
@@ -328,4 +328,10 @@ describe('Font Optimization', () => {
})
}
)
+
+ test('Spread operator regression on ', async () => {
+ const appDir = join(fixturesDir, 'spread-operator-regression')
+ const { code } = await nextBuild(appDir)
+ expect(code).toBe(0)
+ })
})