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

Leverage existing component checking warning for streaming (#34526)

## Bug

Fixes: #31993

* Remove the simple component checking in middleware ssr
* Leverage existing components checking for Component / App / Document, if any of these component is not valid react type or is undefined nextjs will error in dev mode with redbox. Like above.

<img width="826" alt="image" src="https://user-images.githubusercontent.com/4800338/154668945-bcee24ee-17aa-4afd-acda-9f8b249891ac.png">


- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
parent 4ad1c5a2
...@@ -39,10 +39,6 @@ export default async function middlewareSSRLoader(this: any) { ...@@ -39,10 +39,6 @@ export default async function middlewareSSRLoader(this: any) {
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
const rscManifest = self.__RSC_MANIFEST const rscManifest = self.__RSC_MANIFEST
if (typeof pageMod.default !== 'function') {
throw new Error('Your page must export a \`default\` component')
}
// Set server context // Set server context
self.__server_context = { self.__server_context = {
page: ${JSON.stringify(page)}, page: ${JSON.stringify(page)},
......
...@@ -1122,7 +1122,7 @@ export default abstract class Server { ...@@ -1122,7 +1122,7 @@ export default abstract class Server {
const isSSG = !!components.getStaticProps const isSSG = !!components.getStaticProps
const hasServerProps = !!components.getServerSideProps const hasServerProps = !!components.getServerSideProps
const hasStaticPaths = !!components.getStaticPaths const hasStaticPaths = !!components.getStaticPaths
const hasGetInitialProps = !!(components.Component as any).getInitialProps const hasGetInitialProps = !!components.Component?.getInitialProps
// Toggle whether or not this is a Data request // Toggle whether or not this is a Data request
const isDataReq = !!query._nextDataReq && (isSSG || hasServerProps) const isDataReq = !!query._nextDataReq && (isSSG || hasServerProps)
......
import type {
AppType,
DocumentType,
NextComponentType,
} from '../shared/lib/utils'
import { import {
BUILD_MANIFEST, BUILD_MANIFEST,
REACT_LOADABLE_MANIFEST, REACT_LOADABLE_MANIFEST,
...@@ -5,7 +10,6 @@ import { ...@@ -5,7 +10,6 @@ import {
import { join } from 'path' import { join } from 'path'
import { requirePage } from './require' import { requirePage } from './require'
import { BuildManifest } from './get-page-files' import { BuildManifest } from './get-page-files'
import { AppType, DocumentType } from '../shared/lib/utils'
import { interopDefault } from '../lib/interop-default' import { interopDefault } from '../lib/interop-default'
import { import {
PageConfig, PageConfig,
...@@ -22,7 +26,7 @@ export type ManifestItem = { ...@@ -22,7 +26,7 @@ export type ManifestItem = {
export type ReactLoadableManifest = { [moduleId: string]: ManifestItem } export type ReactLoadableManifest = { [moduleId: string]: ManifestItem }
export type LoadComponentsReturnType = { export type LoadComponentsReturnType = {
Component: React.ComponentType Component: NextComponentType
pageConfig: PageConfig pageConfig: PageConfig
buildManifest: BuildManifest buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest reactLoadableManifest: ReactLoadableManifest
......
...@@ -545,7 +545,7 @@ export async function renderToHTML( ...@@ -545,7 +545,7 @@ export async function renderToHTML(
const defaultAppGetInitialProps = const defaultAppGetInitialProps =
App.getInitialProps === (App as any).origGetInitialProps App.getInitialProps === (App as any).origGetInitialProps
const hasPageGetInitialProps = !!(Component as any).getInitialProps const hasPageGetInitialProps = !!(Component as any)?.getInitialProps
const pageIsDynamic = isDynamicRoute(pathname) const pageIsDynamic = isDynamicRoute(pathname)
...@@ -561,7 +561,7 @@ export async function renderToHTML( ...@@ -561,7 +561,7 @@ export async function renderToHTML(
'getServerSideProps', 'getServerSideProps',
'getStaticPaths', 'getStaticPaths',
]) { ]) {
if ((Component as any)[methodName]) { if ((Component as any)?.[methodName]) {
throw new Error( throw new Error(
`page ${pathname} ${methodName} ${GSSP_COMPONENT_MEMBER_ERROR}` `page ${pathname} ${methodName} ${GSSP_COMPONENT_MEMBER_ERROR}`
) )
......
...@@ -11,11 +11,14 @@ import { ...@@ -11,11 +11,14 @@ import {
nextStart, nextStart,
renderViaHTTP, renderViaHTTP,
fetchViaHTTP, fetchViaHTTP,
hasRedbox,
getRedboxHeader,
} from 'next-test-utils' } from 'next-test-utils'
import blocking from './blocking' import blocking from './blocking'
import concurrent from './concurrent' import concurrent from './concurrent'
import basics from './basics' import basics from './basics'
import strictMode from './strict-mode' import strictMode from './strict-mode'
import webdriver from 'next-webdriver'
// overrides react and react-dom to v18 // overrides react and react-dom to v18
const nodeArgs = ['-r', join(__dirname, 'require-hook.js')] const nodeArgs = ['-r', join(__dirname, 'require-hook.js')]
...@@ -23,6 +26,7 @@ const appDir = join(__dirname, '../app') ...@@ -23,6 +26,7 @@ const appDir = join(__dirname, '../app')
const nextConfig = new File(join(appDir, 'next.config.js')) const nextConfig = new File(join(appDir, 'next.config.js'))
const dynamicHello = new File(join(appDir, 'components/dynamic-hello.js')) const dynamicHello = new File(join(appDir, 'components/dynamic-hello.js'))
const unwrappedPage = new File(join(appDir, 'pages/suspense/unwrapped.js')) const unwrappedPage = new File(join(appDir, 'pages/suspense/unwrapped.js'))
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
const USING_CREATE_ROOT = 'Using the createRoot API for React' const USING_CREATE_ROOT = 'Using the createRoot API for React'
...@@ -141,21 +145,22 @@ describe('Blocking mode', () => { ...@@ -141,21 +145,22 @@ describe('Blocking mode', () => {
}) })
function runTestsAgainstRuntime(runtime) { function runTestsAgainstRuntime(runtime) {
describe(`Concurrent mode in the ${runtime} runtime`, () => { runTests(
beforeAll(async () => { `Concurrent mode in the ${runtime} runtime`,
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`) (context, env) => {
dynamicHello.replace('suspense = false', `suspense = true`)
// `noSSR` mode will be ignored by suspense
dynamicHello.replace('let ssr', `let ssr = false`)
})
afterAll(async () => {
nextConfig.restore()
dynamicHello.restore()
})
runTests(`runtime is set to '${runtime}'`, (context) => {
concurrent(context, (p, q) => renderViaHTTP(context.appPort, p, q)) concurrent(context, (p, q) => renderViaHTTP(context.appPort, p, q))
if (env === 'dev') {
it('should recover after undefined exported as default', async () => {
const browser = await webdriver(context.appPort, '/invalid')
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(
`Error: The default export is not a React Component in page: "/invalid"`
)
})
}
it('should stream to users', async () => { it('should stream to users', async () => {
const res = await fetchViaHTTP(context.appPort, '/ssr') const res = await fetchViaHTTP(context.appPort, '/ssr')
expect(res.headers.get('etag')).toBeNull() expect(res.headers.get('etag')).toBeNull()
...@@ -189,17 +194,36 @@ function runTestsAgainstRuntime(runtime) { ...@@ -189,17 +194,36 @@ function runTestsAgainstRuntime(runtime) {
) )
expect(res.headers.get('etag')).toBeDefined() expect(res.headers.get('etag')).toBeDefined()
}) })
}) },
}) {
beforeAll: (env) => {
if (env === 'dev') {
invalidPage.write(`export const value = 1`)
}
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
dynamicHello.replace('suspense = false', `suspense = true`)
// `noSSR` mode will be ignored by suspense
dynamicHello.replace('let ssr', `let ssr = false`)
},
afterAll: (env) => {
if (env === 'dev') {
invalidPage.delete()
}
nextConfig.restore()
dynamicHello.restore()
},
}
)
} }
function runTest(mode, name, fn) { function runTest(env, name, fn, options) {
const context = { appDir } const context = { appDir }
describe(`${name} (${mode})`, () => { describe(`${name} (${env})`, () => {
beforeAll(async () => { beforeAll(async () => {
context.appPort = await findPort() context.appPort = await findPort()
context.stderr = '' context.stderr = ''
if (mode === 'dev') { options?.beforeAll(env)
if (env === 'dev') {
context.server = await launchApp(context.appDir, context.appPort, { context.server = await launchApp(context.appDir, context.appPort, {
nodeArgs, nodeArgs,
onStderr(msg) { onStderr(msg) {
...@@ -217,16 +241,17 @@ function runTest(mode, name, fn) { ...@@ -217,16 +241,17 @@ function runTest(mode, name, fn) {
} }
}) })
afterAll(async () => { afterAll(async () => {
options?.afterAll(env)
await killApp(context.server) await killApp(context.server)
}) })
fn(context) fn(context, env)
}) })
} }
runTestsAgainstRuntime('edge') runTestsAgainstRuntime('edge')
runTestsAgainstRuntime('nodejs') runTestsAgainstRuntime('nodejs')
function runTests(name, fn) { function runTests(name, fn, options) {
runTest('dev', name, fn) runTest('dev', name, fn, options)
runTest('prod', name, fn) runTest('prod', name, fn, options)
} }
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