Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid direct React client API imports in the server graph #40686

Merged
merged 3 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component, ReactElement, ReactNode, useContext } from 'react'
import React, { ReactElement, ReactNode, useContext } from 'react'
import {
OPTIMIZED_FONT_PROVIDERS,
NEXT_BUILTIN_DOCUMENT,
Expand Down Expand Up @@ -353,7 +353,13 @@ function getAmpPath(ampPath: string, asPath: string): string {
return ampPath || `${asPath}${asPath.includes('?') ? '&' : '?'}amp=1`
}

export class Head extends Component<HeadProps> {
// Use `React.Component` to avoid errors from the RSC checks because
// it can't be imported directly in Server Components:
//
// import { Component } from 'react'
//
// More info: https://github.com/vercel/next.js/pull/40686
export class Head extends React.Component<HeadProps> {
static contextType = HtmlContext

context!: React.ContextType<typeof HtmlContext>
Expand Down Expand Up @@ -899,7 +905,7 @@ function handleDocumentScriptLoaderItems(
__NEXT_DATA__.scriptLoader = scriptLoaderItems
}

export class NextScript extends Component<OriginProps> {
export class NextScript extends React.Component<OriginProps> {
static contextType = HtmlContext

context!: React.ContextType<typeof HtmlContext>
Expand Down Expand Up @@ -1104,7 +1110,9 @@ export function Main() {
* `Document` component handles the initial `document` markup and renders only on the server side.
* Commonly used for implementing server side rendering for `css-in-js` libraries.
*/
export default class Document<P = {}> extends Component<DocumentProps & P> {
export default class Document<P = {}> extends React.Component<
DocumentProps & P
> {
/**
* `getInitialProps` hook returns the context object with the addition of `renderPage`.
* `renderPage` callback executes `React` rendering logic synchronously to support server-rendering wrappers
Expand Down
10 changes: 8 additions & 2 deletions packages/next/shared/lib/flush-effects.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import React, { createContext, useContext } from 'react'
import React, { useContext } from 'react'

export type FlushEffectsHook = (callbacks: () => React.ReactNode) => void

export const FlushEffectsContext = createContext<FlushEffectsHook | null>(
// Use `React.createContext` to avoid errors from the RSC checks because
// it can't be imported directly in Server Components:
//
// import { createContext } from 'react'
//
// More info: https://github.com/vercel/next.js/pull/40686
export const FlushEffectsContext = React.createContext<FlushEffectsHook | null>(
huozhi marked this conversation as resolved.
Show resolved Hide resolved
null as any
)

Expand Down