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

refactor: Use swc AST to determine use client and server directives (#54358)

Follow up for #54202, use swc AST to determine the directives. Also add server actions directives for extendibility here in case we might need it in the future

Closes NEXT-1538
parent ad556aee
...@@ -51,13 +51,17 @@ export interface PageStaticInfo { ...@@ -51,13 +51,17 @@ export interface PageStaticInfo {
const CLIENT_MODULE_LABEL = const CLIENT_MODULE_LABEL =
/\/\* __next_internal_client_entry_do_not_use__ ([^ ]*) (cjs|auto) \*\// /\/\* __next_internal_client_entry_do_not_use__ ([^ ]*) (cjs|auto) \*\//
const ACTION_MODULE_LABEL = const ACTION_MODULE_LABEL =
/\/\* __next_internal_action_entry_do_not_use__ ([^ ]+) \*\// /\/\* __next_internal_action_entry_do_not_use__ ([^ ]+) \*\//
const CLIENT_DIRECTIVE = 'use client'
const SERVER_ACTION_DIRECTIVE = 'use server'
export type RSCModuleType = 'server' | 'client' export type RSCModuleType = 'server' | 'client'
export function getRSCModuleInformation( export function getRSCModuleInformation(
source: string, source: string,
isServerLayer = true isServerLayer: boolean
): RSCMeta { ): RSCMeta {
const actions = source.match(ACTION_MODULE_LABEL)?.[1]?.split(',') const actions = source.match(ACTION_MODULE_LABEL)?.[1]?.split(',')
const clientInfoMatch = source.match(CLIENT_MODULE_LABEL) const clientInfoMatch = source.match(CLIENT_MODULE_LABEL)
...@@ -75,14 +79,13 @@ export function getRSCModuleInformation( ...@@ -75,14 +79,13 @@ export function getRSCModuleInformation(
const clientEntryType = clientInfoMatch?.[2] as 'cjs' | 'auto' const clientEntryType = clientInfoMatch?.[2] as 'cjs' | 'auto'
const type = clientRefs ? RSC_MODULE_TYPES.client : RSC_MODULE_TYPES.server const type = clientRefs ? RSC_MODULE_TYPES.client : RSC_MODULE_TYPES.server
const hasUseClientDirective = /^\s*['"]use client['"]/.test(source)
return { return {
type, type,
actions, actions,
clientRefs, clientRefs,
clientEntryType, clientEntryType,
isClientRef, isClientRef,
hasUseClientDirective,
} }
} }
...@@ -124,6 +127,7 @@ function checkExports( ...@@ -124,6 +127,7 @@ function checkExports(
generateSitemaps?: boolean generateSitemaps?: boolean
generateStaticParams: boolean generateStaticParams: boolean
extraProperties?: Set<string> extraProperties?: Set<string>
directives?: Set<string>
} { } {
const exportsSet = new Set<string>([ const exportsSet = new Set<string>([
'getStaticProps', 'getStaticProps',
...@@ -142,8 +146,27 @@ function checkExports( ...@@ -142,8 +146,27 @@ function checkExports(
let generateSitemaps: boolean = false let generateSitemaps: boolean = false
let generateStaticParams = false let generateStaticParams = false
let extraProperties = new Set<string>() let extraProperties = new Set<string>()
let directives = new Set<string>()
let hasLeadingNonDirectiveNode = false
for (const node of swcAST.body) { for (const node of swcAST.body) {
// There should be no non-string literals nodes before directives
if (
node.type === 'ExpressionStatement' &&
node.expression.type === 'StringLiteral'
) {
if (!hasLeadingNonDirectiveNode) {
const directive = node.expression.value
if (CLIENT_DIRECTIVE === directive) {
directives.add('client')
}
if (SERVER_ACTION_DIRECTIVE === directive) {
directives.add('server')
}
}
} else {
hasLeadingNonDirectiveNode = true
}
if ( if (
node.type === 'ExportDeclaration' && node.type === 'ExportDeclaration' &&
node.declaration?.type === 'VariableDeclaration' node.declaration?.type === 'VariableDeclaration'
...@@ -240,6 +263,7 @@ function checkExports( ...@@ -240,6 +263,7 @@ function checkExports(
generateSitemaps, generateSitemaps,
generateStaticParams, generateStaticParams,
extraProperties, extraProperties,
directives,
} }
} catch (err) {} } catch (err) {}
} }
...@@ -253,6 +277,7 @@ function checkExports( ...@@ -253,6 +277,7 @@ function checkExports(
generateSitemaps: false, generateSitemaps: false,
generateStaticParams: false, generateStaticParams: false,
extraProperties: undefined, extraProperties: undefined,
directives: undefined,
} }
} }
...@@ -467,8 +492,9 @@ export async function getPageStaticInfo(params: { ...@@ -467,8 +492,9 @@ export async function getPageStaticInfo(params: {
preferredRegion, preferredRegion,
generateStaticParams, generateStaticParams,
extraProperties, extraProperties,
directives,
} = checkExports(swcAST, pageFilePath) } = checkExports(swcAST, pageFilePath)
const rscInfo = getRSCModuleInformation(fileContent) const rscInfo = getRSCModuleInformation(fileContent, true)
const rsc = rscInfo.type const rsc = rscInfo.type
// default / failsafe value for config // default / failsafe value for config
...@@ -575,7 +601,7 @@ export async function getPageStaticInfo(params: { ...@@ -575,7 +601,7 @@ export async function getPageStaticInfo(params: {
if ( if (
pageType === 'app' && pageType === 'app' &&
rscInfo.hasUseClientDirective && directives?.has('client') &&
generateStaticParams generateStaticParams
) { ) {
throw new Error( throw new Error(
......
...@@ -32,7 +32,6 @@ export interface RSCMeta { ...@@ -32,7 +32,6 @@ export interface RSCMeta {
clientRefs?: string[] clientRefs?: string[]
clientEntryType?: 'cjs' | 'auto' clientEntryType?: 'cjs' | 'auto'
isClientRef?: boolean isClientRef?: boolean
hasUseClientDirective?: boolean
requests?: string[] // client requests in flight client entry requests?: string[] // client requests in flight client entry
} }
......
...@@ -22,7 +22,7 @@ export default function transformSource( ...@@ -22,7 +22,7 @@ export default function transformSource(
// Assign the RSC meta information to buildInfo. // Assign the RSC meta information to buildInfo.
// Exclude next internal files which are not marked as client files // Exclude next internal files which are not marked as client files
const buildInfo = getModuleBuildInfo(this._module) const buildInfo = getModuleBuildInfo(this._module)
buildInfo.rsc = getRSCModuleInformation(source) buildInfo.rsc = getRSCModuleInformation(source, true)
// A client boundary. // A client boundary.
if (buildInfo.rsc?.type === RSC_MODULE_TYPES.client) { if (buildInfo.rsc?.type === RSC_MODULE_TYPES.client) {
......
export default function Layout({ children }) {
return children
}
// make children routes dynamic
export const dynamic = 'force-dynamic'
...@@ -41,3 +41,5 @@ export default function page() { ...@@ -41,3 +41,5 @@ export default function page() {
</div> </div>
) )
} }
export const dynamic = 'force-dynamic'
import React from 'react' import React from 'react'
import RootStyleRegistry from './root-style-registry' import RootStyleRegistry from './root-style-registry'
export const revalidate = 0
export default function AppLayout({ children }) { export default function AppLayout({ children }) {
return ( return (
<html> <html>
......
...@@ -23,3 +23,5 @@ export default function () { ...@@ -23,3 +23,5 @@ export default function () {
</> </>
) )
} }
export const dynamic = 'force-dynamic'
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