Unverified Commit fb100ee7 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Check react 18 flags and add more tests (#30575)

* error when react 18 experimental flags aren't setup properly
* add more tests for rsc ssr
parent f6b15b59
...@@ -347,20 +347,35 @@ export default async function getBaseWebpackConfig( ...@@ -347,20 +347,35 @@ export default async function getBaseWebpackConfig(
cwd: dir, cwd: dir,
name: 'react-dom', name: 'react-dom',
}) })
const isReactExperimental = Boolean(
reactDomVersion && /0\.0\.0-experimental/.test(reactDomVersion)
)
const hasReact18: boolean = const hasReact18: boolean =
Boolean(reactDomVersion) && Boolean(reactDomVersion) &&
(semver.gte(reactDomVersion!, '18.0.0') || (semver.gte(reactDomVersion!, '18.0.0') ||
semver.coerce(reactDomVersion)?.version === '18.0.0') semver.coerce(reactDomVersion)?.version === '18.0.0')
const hasReactPrerelease = const hasReactPrerelease =
Boolean(reactDomVersion) && semver.prerelease(reactDomVersion!) != null (Boolean(reactDomVersion) && semver.prerelease(reactDomVersion!) != null) ||
const hasReactRoot: boolean = config.experimental.reactRoot || hasReact18 isReactExperimental
const hasReactRoot: boolean =
config.experimental.reactRoot || hasReact18 || isReactExperimental
if (config.experimental.reactRoot && !(hasReact18 || isReactExperimental)) {
// It's fine to only mention React 18 here as we don't recommend people to try experimental.
Log.warn('You have to use React 18 to use `experimental.reactRoot`.')
}
if (config.experimental.concurrentFeatures && !hasReactRoot) {
throw new Error(
'`experimental.concurrentFeatures` requires `experimental.reactRoot` to be enabled along with React 18.'
)
}
if ( if (
config.experimental.serverComponents && config.experimental.serverComponents &&
!config.experimental.concurrentFeatures !config.experimental.concurrentFeatures
) { ) {
throw new Error( throw new Error(
`Flag \`experimental.concurrentFeatures\` is required to be enabled along with \`experimental.serverComponents\`.` '`experimental.concurrentFeatures` is required to be enabled along with `experimental.serverComponents`.'
) )
} }
const hasConcurrentFeatures = const hasConcurrentFeatures =
......
node_modules
\ No newline at end of file
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { File, nextBuild } from 'next-test-utils'
const appDir = __dirname
const nextConfig = new File(join(appDir, 'next.config.js'))
function writeNextConfig(config) {
const content = `module.exports = { experimental: ${JSON.stringify(config)} }`
nextConfig.write(content)
}
describe('Invalid react 18 webpack config', () => {
it('should enable `experimental.reactRoot` when `experimental.concurrentFeatures` enables', async () => {
writeNextConfig({
concurrentFeatures: true,
})
const { stderr } = await nextBuild(appDir, [], { stderr: true })
nextConfig.restore()
expect(stderr).toContain(
'`experimental.concurrentFeatures` requires `experimental.reactRoot` to be enabled along with React 18.'
)
})
it('should enable `experimental.concurrentFeatures` for server components', async () => {
writeNextConfig({
reactRoot: true,
concurrentFeatures: false,
serverComponents: true,
})
const { stderr } = await nextBuild(appDir, [], { stderr: true })
nextConfig.restore()
expect(stderr).toContain(
'`experimental.concurrentFeatures` is required to be enabled along with `experimental.serverComponents`.'
)
})
it('should warn user when not using react 18 and `experimental.reactRoot` is enabled', async () => {
const reactDomPackagePah = join(appDir, 'node_modules/react-dom')
await fs.mkdirp(reactDomPackagePah)
await fs.writeFile(
join(reactDomPackagePah, 'package.json'),
JSON.stringify({ name: 'react-dom', version: '17.0.0' })
)
writeNextConfig({ reactRoot: true })
const { stderr } = await nextBuild(appDir, [], { stderr: true })
await fs.remove(reactDomPackagePah)
nextConfig.restore()
expect(stderr).toContain(
'You have to use React 18 to use `experimental.reactRoot`.'
)
})
})
module.exports = { experimental: { reactRoot: true } }
export default function Index() {
return 'index'
}
/* eslint-env jest */ /* eslint-env jest */
import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
...@@ -78,6 +79,22 @@ describe('RSC prod', () => { ...@@ -78,6 +79,22 @@ describe('RSC prod', () => {
}) })
}) })
it('should have clientInfo in middleware manifest', async () => {
const middlewareManifestPath = join(
distDir,
'server',
'middleware-manifest.json'
)
const content = JSON.parse(
await fs.readFile(middlewareManifestPath, 'utf8')
)
expect(content.clientInfo).toEqual([
['/', true],
['/next-api/image', true],
['/next-api/link', true],
['/routes/[dynamic]', true],
])
})
runTests(context) runTests(context)
}) })
...@@ -97,9 +114,37 @@ describe('RSC dev', () => { ...@@ -97,9 +114,37 @@ describe('RSC dev', () => {
async function runTests(context) { async function runTests(context) {
it('should render the correct html', async () => { it('should render the correct html', async () => {
const homeHTML = await renderViaHTTP(context.appPort, '/') const homeHTML = await renderViaHTTP(context.appPort, '/')
const linkHTML = await renderViaHTTP(context.appPort, '/next-api/link')
// dynamic routes
const dynamicRouteHTML1 = await renderViaHTTP(
context.appPort,
'/routes/dynamic1'
)
const dynamicRouteHTML2 = await renderViaHTTP(
context.appPort,
'/routes/dynamic2'
)
expect(homeHTML).toContain('thisistheindexpage.server') expect(homeHTML).toContain('thisistheindexpage.server')
expect(homeHTML).toContain('foo.client') expect(homeHTML).toContain('foo.client')
expect(linkHTML).toContain('go home')
expect(dynamicRouteHTML1).toContain('[pid]')
expect(dynamicRouteHTML2).toContain('[pid]')
})
it('should suspense next/link on server side', async () => {
const linkHTML = await renderViaHTTP(context.appPort, '/next-api/link')
const $ = cheerio.load(linkHTML)
const linkText = $('div[hidden] > a[href="/"]').text()
expect(linkText).toContain('go home')
})
it('should suspense next/image on server side', async () => {
const imageHTML = await renderViaHTTP(context.appPort, '/next-api/image')
const $ = cheerio.load(imageHTML)
const imageTag = $('div[hidden] > span > span > img')
expect(imageTag.attr('src')).toContain('data:image')
}) })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment