Unverified Commit 2b99db07 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Client component directive: use client (#41333)

Replace `'client'` with `'use client'` as client directive for client
components in RSC

x-ref: [slack
thread](https://vercel.slack.com/archives/C035J346QQL/p1665435520907559)
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 6c2a0f36
...@@ -94,7 +94,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -94,7 +94,7 @@ impl<C: Comments> ReactServerComponents<C> {
Some(expr_stmt) => { Some(expr_stmt) => {
match &*expr_stmt.expr { match &*expr_stmt.expr {
Expr::Lit(Lit::Str(Str { value, .. })) => { Expr::Lit(Lit::Str(Str { value, .. })) => {
if &**value == "client" { if &**value == "use client" {
is_client_entry = true; is_client_entry = true;
// Remove the directive. // Remove the directive.
......
...@@ -6,17 +6,17 @@ ...@@ -6,17 +6,17 @@
* This is a comment. * This is a comment.
*/ */
"client"; "use client";
// This is a comment. // This is a comment.
"foo"; "foo";
"client"; "use client";
import "fs" import "fs"
"client"; "use client";
"bar"; "bar";
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// This is a comment. // This is a comment.
"foo"; "foo";
import "fs"; import "fs";
"client"; "use client";
"bar"; "bar";
// This is a comment. // This is a comment.
1 + 1; 1 + 1;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* This is a comment. * This is a comment.
*/ */
"client"; "use client";
// This is a comment. // This is a comment.
......
...@@ -182,9 +182,9 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -182,9 +182,9 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
const result = ` const result = `
export ${treeCode} export ${treeCode}
export const AppRouter = require('next/dist/client/components/app-router.client.js').default export const AppRouter = require('next/dist/client/components/app-router.js').default
export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default export const LayoutRouter = require('next/dist/client/components/layout-router.js').default
export const RenderFromTemplateContext = require('next/dist/client/components/render-from-template-context.client.js').default export const RenderFromTemplateContext = require('next/dist/client/components/render-from-template-context.js').default
export const staticGenerationAsyncStorage = require('next/dist/client/components/static-generation-async-storage.js').staticGenerationAsyncStorage export const staticGenerationAsyncStorage = require('next/dist/client/components/static-generation-async-storage.js').staticGenerationAsyncStorage
export const requestAsyncStorage = require('next/dist/client/components/request-async-storage.js').requestAsyncStorage export const requestAsyncStorage = require('next/dist/client/components/request-async-storage.js').requestAsyncStorage
......
...@@ -2,8 +2,8 @@ import { appBootstrap } from './app-bootstrap' ...@@ -2,8 +2,8 @@ import { appBootstrap } from './app-bootstrap'
appBootstrap(() => { appBootstrap(() => {
// Include app-router and layout-router in the main chunk // Include app-router and layout-router in the main chunk
import('next/dist/client/components/app-router.client.js') import('next/dist/client/components/app-router.js')
import('next/dist/client/components/layout-router.client.js') import('next/dist/client/components/layout-router.js')
const { hydrate } = require('./app-index') const { hydrate } = require('./app-index')
hydrate() hydrate()
......
'client' 'use client'
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
import React, { useEffect, useMemo, useCallback } from 'react' import React, { useEffect, useMemo, useCallback } from 'react'
......
'client' 'use client'
import React, { import React, {
useContext, useContext,
...@@ -27,7 +27,7 @@ import { ...@@ -27,7 +27,7 @@ import {
TemplateContext, TemplateContext,
AppRouterContext, AppRouterContext,
} from '../../shared/lib/app-router-context' } from '../../shared/lib/app-router-context'
import { fetchServerResponse } from './app-router.client' import { fetchServerResponse } from './app-router'
import { createInfinitePromise } from './infinite-promise' import { createInfinitePromise } from './infinite-promise'
import { ErrorBoundary } from './error-boundary' import { ErrorBoundary } from './error-boundary'
import { matchSegment } from './match-segments' import { matchSegment } from './match-segments'
......
...@@ -8,7 +8,7 @@ import type { ...@@ -8,7 +8,7 @@ import type {
} from '../../server/app-render' } from '../../server/app-render'
// TODO-APP: change to React.use once it becomes stable // TODO-APP: change to React.use once it becomes stable
import { matchSegment } from './match-segments' import { matchSegment } from './match-segments'
import { fetchServerResponse } from './app-router.client' import { fetchServerResponse } from './app-router'
/** /**
* Create data fetching record for Promise. * Create data fetching record for Promise.
......
'client' 'use client'
import React, { useContext } from 'react' import React, { useContext } from 'react'
import { TemplateContext } from '../../shared/lib/app-router-context' import { TemplateContext } from '../../shared/lib/app-router-context'
......
'client' 'use client'
import React, { import React, {
useRef, useRef,
......
'client' 'use client'
import React, { import React, {
useRef, useRef,
......
'client' 'use client'
import React from 'react' import React from 'react'
import { UrlObject } from 'url' import { UrlObject } from 'url'
......
'client' 'use client'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import React, { useEffect, useContext, useRef } from 'react' import React, { useEffect, useContext, useRef } from 'react'
......
...@@ -808,9 +808,9 @@ export async function renderToHTMLOrFlight( ...@@ -808,9 +808,9 @@ export async function renderToHTMLOrFlight(
stripInternalQueries(query) stripInternalQueries(query)
const LayoutRouter = const LayoutRouter =
ComponentMod.LayoutRouter as typeof import('../client/components/layout-router.client').default ComponentMod.LayoutRouter as typeof import('../client/components/layout-router').default
const RenderFromTemplateContext = const RenderFromTemplateContext =
ComponentMod.RenderFromTemplateContext as typeof import('../client/components/render-from-template-context.client').default ComponentMod.RenderFromTemplateContext as typeof import('../client/components/render-from-template-context').default
/** /**
* Server Context is specifically only available in Server Components. * Server Context is specifically only available in Server Components.
...@@ -1337,7 +1337,7 @@ export async function renderToHTMLOrFlight( ...@@ -1337,7 +1337,7 @@ export async function renderToHTMLOrFlight(
// AppRouter is provided by next-app-loader // AppRouter is provided by next-app-loader
const AppRouter = const AppRouter =
ComponentMod.AppRouter as typeof import('../client/components/app-router.client').default ComponentMod.AppRouter as typeof import('../client/components/app-router').default
let serverComponentsInlinedTransformStream: TransformStream< let serverComponentsInlinedTransformStream: TransformStream<
Uint8Array, Uint8Array,
......
...@@ -12,7 +12,7 @@ export type CacheNode = { ...@@ -12,7 +12,7 @@ export type CacheNode = {
* In-flight request for this node. * In-flight request for this node.
*/ */
data: ReturnType< data: ReturnType<
typeof import('../../client/components/app-router.client').fetchServerResponse typeof import('../../client/components/app-router').fetchServerResponse
> | null > | null
/** /**
* React Component for this node. * React Component for this node.
......
'client' 'use client'
import React, { useContext } from 'react' import React, { useContext } from 'react'
import Effect from './side-effect' import Effect from './side-effect'
......
...@@ -113,10 +113,10 @@ module.exports = function (task) { ...@@ -113,10 +113,10 @@ module.exports = function (task) {
const output = yield transform(source, options) const output = yield transform(source, options)
const ext = path.extname(file.base) const ext = path.extname(file.base)
// Make sure the output content keeps the `"client"` directive. // Make sure the output content keeps the `"use client"` directive.
// TODO: Remove this once SWC fixes the issue. // TODO: Remove this once SWC fixes the issue.
if (/^['"]client['"]/.test(source)) { if (/^['"]use client['"]/.test(source)) {
output.code = '"client";\n' + output.code output.code = '"use client";\n' + output.code
} }
// Replace `.ts|.tsx` with `.js` in files with an extension // Replace `.ts|.tsx` with `.js` in files with an extension
......
'client' 'use client'
// TODO-APP: support typing for useSelectedLayoutSegment // TODO-APP: support typing for useSelectedLayoutSegment
// @ts-ignore // @ts-ignore
......
'client' 'use client'
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
......
'client' 'use client'
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
......
'client' 'use client'
// export function getServerSideProps() { { props: {} } } // export function getServerSideProps() { { props: {} } }
......
'client' 'use client'
// export function getStaticProps() { return { props: {} }} // export function getStaticProps() { return { props: {} }}
......
'client' 'use client'
import './client-layout.css' import './client-layout.css'
......
'client' 'use client'
import './client-page.css' import './client-page.css'
......
'client' 'use client'
import './style.css' import './style.css'
import styles from './style.module.css' import styles from './style.module.css'
......
'client' 'use client'
export default function Page() { export default function Page() {
return null return null
......
'client' 'use client'
import styles from './client-comp.module.css' import styles from './client-comp.module.css'
......
'client' 'use client'
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
......
'client' 'use client'
import { useState, lazy } from 'react' import { useState, lazy } from 'react'
const Lazy = lazy(() => import('../text-lazy-client.js')) const Lazy = lazy(() => import('../text-lazy-client.js'))
export function LazyClientComponent() { export function LazyClientComponent() {
let [state] = useState('client') let [state] = useState('use client')
return ( return (
<> <>
<Lazy /> <Lazy />
......
'client' 'use client'
import { useState } from 'react' import { useState } from 'react'
import styles from './dynamic.module.css' import styles from './dynamic.module.css'
......
'client' 'use client'
import styles from './lazy.module.css' import styles from './lazy.module.css'
......
'client' 'use client'
export default function ErrorBoundary({ error, reset }) { export default function ErrorBoundary({ error, reset }) {
return ( return (
......
'client' 'use client'
import { useState } from 'react' import { useState } from 'react'
......
'client' 'use client'
import { useState } from 'react' import { useState } from 'react'
......
'client' 'use client'
export default function Page() { export default function Page() {
throw new Error('Error during SSR') throw new Error('Error during SSR')
......
'client' 'use client'
import { cookies } from 'next/dist/client/components/hooks-server' import { cookies } from 'next/dist/client/components/hooks-server'
......
'client' 'use client'
import { headers } from 'next/dist/client/components/hooks-server' import { headers } from 'next/dist/client/components/hooks-server'
......
'client' 'use client'
// TODO-APP: enable once test is not skipped. // TODO-APP: enable once test is not skipped.
// import { useLayoutSegments } from 'next/dist/client/components/hooks-client' // import { useLayoutSegments } from 'next/dist/client/components/hooks-client'
......
'client' 'use client'
import { usePathname } from 'next/dist/client/components/hooks-client' import { usePathname } from 'next/dist/client/components/hooks-client'
......
'client' 'use client'
import { previewData } from 'next/dist/client/components/hooks-server' import { previewData } from 'next/dist/client/components/hooks-server'
......
'client' 'use client'
import { useRouter } from 'next/dist/client/components/hooks-client' import { useRouter } from 'next/dist/client/components/hooks-client'
......
'client' 'use client'
export default function Page() { export default function Page() {
return <h1 id="router-sub-page">hello from /hooks/use-router/sub-page</h1> return <h1 id="router-sub-page">hello from /hooks/use-router/sub-page</h1>
......
'client' 'use client'
import { useSearchParams } from 'next/dist/client/components/hooks-client' import { useSearchParams } from 'next/dist/client/components/hooks-client'
......
'client' 'use client'
// TODO-APP: enable once test is not skipped. // TODO-APP: enable once test is not skipped.
// import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client' // import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client'
......
'client' 'use client'
import { useRouter } from 'next/dist/client/components/hooks-client' import { useRouter } from 'next/dist/client/components/hooks-client'
import React from 'react' import React from 'react'
......
'client' 'use client'
import { TabNavItem } from './TabNavItem' import { TabNavItem } from './TabNavItem'
import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client' import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client'
......
'client' 'use client'
import { TabNavItem } from '../TabNavItem' import { TabNavItem } from '../TabNavItem'
import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client' import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client'
......
'client' 'use client'
import { notFound } from 'next/dist/client/components/not-found' import { notFound } from 'next/dist/client/components/not-found'
import React from 'react' import React from 'react'
......
'client' 'use client'
import { notFound } from 'next/dist/client/components/not-found' import { notFound } from 'next/dist/client/components/not-found'
export default function ClientComp() { export default function ClientComp() {
......
'client' 'use client'
import { useRouter, withRouter } from 'next/router' import { useRouter, withRouter } from 'next/router'
import IsNull from './is-null' import IsNull from './is-null'
......
'client' 'use client'
export default function Page({ params, searchParams }) { export default function Page({ params, searchParams }) {
return ( return (
......
'client' 'use client'
import { redirect } from 'next/dist/client/components/redirect' import { redirect } from 'next/dist/client/components/redirect'
import React from 'react' import React from 'react'
......
'client' 'use client'
import { redirect } from 'next/dist/client/components/redirect' import { redirect } from 'next/dist/client/components/redirect'
export default function ClientComp() { export default function ClientComp() {
......
'client' 'use client'
export default function Client() { export default function Client() {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
......
'client' 'use client'
export default function ShouldNotServeClientDotJs(props) { export default function ShouldNotServeClientDotJs(props) {
return ( return (
......
'client' 'use client'
import { useState } from 'react' import { useState } from 'react'
......
'client' 'use client'
import font6 from '../../fonts/font6' import font6 from '../../fonts/font6'
export default function Component() { export default function Component() {
......
'client' 'use client'
import font4 from '../../fonts/font4' import font4 from '../../fonts/font4'
export default function Root({ children }) { export default function Root({ children }) {
......
'client' 'use client'
import Comp from './Comp' import Comp from './Comp'
import font5 from '../../fonts/font5' import font5 from '../../fonts/font5'
......
'client' 'use client'
import styled from 'styled-components' import styled from 'styled-components'
......
'client' 'use client'
import css from 'styled-jsx/css' import css from 'styled-jsx/css'
......
'client' 'use client'
import React from 'react' import React from 'react'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx' import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
......
'client' 'use client'
export default function bar() { export default function bar() {
return 'bar.client' return 'bar.client'
......
'client' 'use client'
exports.Cjs = function Cjs() { exports.Cjs = function Cjs() {
return 'cjs-client' return 'cjs-client'
......
'client' 'use client'
export function Named() { export function Named() {
return 'named.client' return 'named.client'
......
'client' 'use client'
import { useState } from 'react' import { useState } from 'react'
......
'client' 'use client'
export * from './one' export * from './one'
'client' 'use client'
export default function foo() { export default function foo() {
return 'foo.client' return 'foo.client'
......
'client' 'use client'
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
......
'client' 'use client'
import styles from './style.module.css' import styles from './style.module.css'
......
'client' 'use client'
import Shared from './shared' import Shared from './shared'
......
'client' 'use client'
import getType, { named, value, array, obj } from 'non-isomorphic-text' import getType, { named, value, array, obj } from 'non-isomorphic-text'
......
'client' 'use client'
import v from 'conditional-exports' import v from 'conditional-exports'
import v1 from 'conditional-exports/subpath' import v1 from 'conditional-exports/subpath'
......
'client' 'use client'
export { default } from './detector' export { default } from './detector'
'client' 'use client'
import v from 'conditional-exports-optout' import v from 'conditional-exports-optout'
import v1 from 'conditional-exports-optout/subpath' import v1 from 'conditional-exports-optout/subpath'
......
'client' 'use client'
import { name } from 'random-module-instance' import { name } from 'random-module-instance'
......
'client' 'use client'
import { useState } from 'react' import { useState } from 'react'
......
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