Skip to content

Commit 5d532af

Browse files
authored
Mark styled-jsx as client-only package (#41414)
For css-in-js libs like styled-jsx we don't recommend to use on server side since it will blow up the rsc payload during navigation. This upgration of styled-jsx includes `client-only` as dependency so that it will throw when it's been used on serevr layer x-ref: vercel/styled-jsx#816
1 parent 7bde099 commit 5d532af

File tree

8 files changed

+113
-61
lines changed

8 files changed

+113
-61
lines changed

packages/next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"@swc/helpers": "0.4.11",
7979
"caniuse-lite": "^1.0.30001406",
8080
"postcss": "8.4.14",
81-
"styled-jsx": "5.0.7",
81+
"styled-jsx": "5.1.0",
8282
"use-sync-external-store": "1.2.0"
8383
},
8484
"peerDependencies": {

pnpm-lock.yaml

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/index.test.ts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,62 +1240,6 @@ describe('app dir', () => {
12401240
await next.patchFile(filePath, origContent)
12411241
}
12421242
})
1243-
1244-
it('should throw an error when getServerSideProps is used', async () => {
1245-
const pageFile =
1246-
'app/client-with-errors/get-server-side-props/page.js'
1247-
const content = await next.readFile(pageFile)
1248-
const uncomment = content.replace(
1249-
'// export function getServerSideProps',
1250-
'export function getServerSideProps'
1251-
)
1252-
await next.patchFile(pageFile, uncomment)
1253-
const res = await fetchViaHTTP(
1254-
next.url,
1255-
'/client-with-errors/get-server-side-props'
1256-
)
1257-
await next.patchFile(pageFile, content)
1258-
1259-
await check(async () => {
1260-
const { status } = await fetchViaHTTP(
1261-
next.url,
1262-
'/client-with-errors/get-server-side-props'
1263-
)
1264-
return status
1265-
}, /200/)
1266-
1267-
expect(res.status).toBe(500)
1268-
expect(await res.text()).toContain(
1269-
'`getServerSideProps` is not allowed in Client Components'
1270-
)
1271-
})
1272-
1273-
it('should throw an error when getStaticProps is used', async () => {
1274-
const pageFile = 'app/client-with-errors/get-static-props/page.js'
1275-
const content = await next.readFile(pageFile)
1276-
const uncomment = content.replace(
1277-
'// export function getStaticProps',
1278-
'export function getStaticProps'
1279-
)
1280-
await next.patchFile(pageFile, uncomment)
1281-
const res = await fetchViaHTTP(
1282-
next.url,
1283-
'/client-with-errors/get-static-props'
1284-
)
1285-
await next.patchFile(pageFile, content)
1286-
await check(async () => {
1287-
const { status } = await fetchViaHTTP(
1288-
next.url,
1289-
'/client-with-errors/get-static-props'
1290-
)
1291-
return status
1292-
}, /200/)
1293-
1294-
expect(res.status).toBe(500)
1295-
expect(await res.text()).toContain(
1296-
'`getStaticProps` is not allowed in Client Components'
1297-
)
1298-
})
12991243
}
13001244
})
13011245

test/e2e/app-dir/rsc-errors.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import path from 'path'
2+
import { check, fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
3+
import { createNext, FileRef } from 'e2e-utils'
4+
import { NextInstance } from 'test/lib/next-modes/base'
5+
6+
describe('app dir - rsc basics', () => {
7+
let next: NextInstance
8+
9+
const { isNextDeploy, isNextDev } = global as any
10+
const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'
11+
if (isNextDeploy || isReact17) {
12+
it('should skip tests for next-deploy and react 17', () => {})
13+
return
14+
}
15+
if (!isNextDev) {
16+
it('should skip tests for next-start', () => {})
17+
return
18+
}
19+
20+
beforeAll(async () => {
21+
next = await createNext({
22+
files: new FileRef(path.join(__dirname, './rsc-errors')),
23+
dependencies: {
24+
react: 'experimental',
25+
'react-dom': 'experimental',
26+
},
27+
})
28+
})
29+
afterAll(() => next.destroy())
30+
31+
it('should throw an error when getServerSideProps is used', async () => {
32+
const pageFile = 'app/client-with-errors/get-server-side-props/page.js'
33+
const content = await next.readFile(pageFile)
34+
const uncomment = content.replace(
35+
'// export function getServerSideProps',
36+
'export function getServerSideProps'
37+
)
38+
await next.patchFile(pageFile, uncomment)
39+
const res = await fetchViaHTTP(
40+
next.url,
41+
'/client-with-errors/get-server-side-props'
42+
)
43+
await next.patchFile(pageFile, content)
44+
45+
await check(async () => {
46+
const { status } = await fetchViaHTTP(
47+
next.url,
48+
'/client-with-errors/get-server-side-props'
49+
)
50+
return status
51+
}, /200/)
52+
53+
expect(res.status).toBe(500)
54+
expect(await res.text()).toContain(
55+
'`getServerSideProps` is not allowed in Client Components'
56+
)
57+
})
58+
59+
it('should throw an error when getStaticProps is used', async () => {
60+
const pageFile = 'app/client-with-errors/get-static-props/page.js'
61+
const content = await next.readFile(pageFile)
62+
const uncomment = content.replace(
63+
'// export function getStaticProps',
64+
'export function getStaticProps'
65+
)
66+
await next.patchFile(pageFile, uncomment)
67+
const res = await fetchViaHTTP(
68+
next.url,
69+
'/client-with-errors/get-static-props'
70+
)
71+
await next.patchFile(pageFile, content)
72+
await check(async () => {
73+
const { status } = await fetchViaHTTP(
74+
next.url,
75+
'/client-with-errors/get-static-props'
76+
)
77+
return status
78+
}, /200/)
79+
80+
expect(res.status).toBe(500)
81+
expect(await res.text()).toContain(
82+
'`getStaticProps` is not allowed in Client Components'
83+
)
84+
})
85+
86+
it('should error for styled-jsx imports on server side', async () => {
87+
const html = await renderViaHTTP(next.url, '/server-with-errors/styled-jsx')
88+
expect(html).toContain(
89+
'This module cannot be imported from a Server Component module. It should only be used from a Client Component.'
90+
)
91+
})
92+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import JSXStyle from 'styled-jsx/style'
2+
3+
export default function Page() {
4+
return <JSXStyle>{`p{color:red}`}</JSXStyle>
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
experimental: { appDir: true },
3+
}

0 commit comments

Comments
 (0)