Unverified Commit 5fd1d537 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

feat(next-swc): lint for `ssr: false` in server components (#70378)

Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 1d62f471
...@@ -21,7 +21,7 @@ use swc_core::{ ...@@ -21,7 +21,7 @@ use swc_core::{
}, },
}; };
use super::cjs_finder::contains_cjs; use super::{cjs_finder::contains_cjs, import_analyzer::ImportMap};
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
...@@ -76,6 +76,7 @@ enum RSCErrorKind { ...@@ -76,6 +76,7 @@ enum RSCErrorKind {
NextRscErrConflictMetadataExport(Span), NextRscErrConflictMetadataExport(Span),
NextRscErrInvalidApi((String, Span)), NextRscErrInvalidApi((String, Span)),
NextRscErrDeprecatedApi((String, String, Span)), NextRscErrDeprecatedApi((String, String, Span)),
NextSsrDynamicFalseNotAllowed(Span),
} }
impl<C: Comments> VisitMut for ReactServerComponents<C> { impl<C: Comments> VisitMut for ReactServerComponents<C> {
...@@ -301,6 +302,11 @@ fn report_error(app_dir: &Option<PathBuf>, filepath: &str, error_kind: RSCErrorK ...@@ -301,6 +302,11 @@ fn report_error(app_dir: &Option<PathBuf>, filepath: &str, error_kind: RSCErrorK
), ),
_ => (format!("\"{source}\" is deprecated."), span), _ => (format!("\"{source}\" is deprecated."), span),
}, },
RSCErrorKind::NextSsrDynamicFalseNotAllowed(span) => (
"`ssr: false` is not allowed with `next/dynamic` in Server Components. Please move it into a client component."
.to_string(),
span,
),
}; };
HANDLER.with(|handler| handler.struct_span_err(span, msg.as_str()).emit()) HANDLER.with(|handler| handler.struct_span_err(span, msg.as_str()).emit())
...@@ -502,6 +508,7 @@ struct ReactServerComponentValidator { ...@@ -502,6 +508,7 @@ struct ReactServerComponentValidator {
invalid_client_imports: Vec<JsWord>, invalid_client_imports: Vec<JsWord>,
invalid_client_lib_apis_mapping: HashMap<&'static str, Vec<&'static str>>, invalid_client_lib_apis_mapping: HashMap<&'static str, Vec<&'static str>>,
pub directive_import_collection: Option<(bool, bool, RcVec<ModuleImports>, RcVec<String>)>, pub directive_import_collection: Option<(bool, bool, RcVec<ModuleImports>, RcVec<String>)>,
imports: ImportMap,
} }
// A type to workaround a clippy warning. // A type to workaround a clippy warning.
...@@ -576,6 +583,7 @@ impl ReactServerComponentValidator { ...@@ -576,6 +583,7 @@ impl ReactServerComponentValidator {
invalid_client_imports: vec![JsWord::from("server-only"), JsWord::from("next/headers")], invalid_client_imports: vec![JsWord::from("server-only"), JsWord::from("next/headers")],
invalid_client_lib_apis_mapping: [("next/server", vec!["unstable_after"])].into(), invalid_client_lib_apis_mapping: [("next/server", vec!["unstable_after"])].into(),
imports: ImportMap::default(),
} }
} }
...@@ -584,6 +592,13 @@ impl ReactServerComponentValidator { ...@@ -584,6 +592,13 @@ impl ReactServerComponentValidator {
RE.is_match(filepath) RE.is_match(filepath)
} }
fn is_callee_next_dynamic(&self, callee: &Callee) -> bool {
match callee {
Callee::Expr(expr) => self.imports.is_import(expr, "next/dynamic", "default"),
_ => false,
}
}
// Asserts the server lib apis // Asserts the server lib apis
// e.g. // e.g.
// assert_invalid_server_lib_apis("react", import) // assert_invalid_server_lib_apis("react", import)
...@@ -817,6 +832,44 @@ impl ReactServerComponentValidator { ...@@ -817,6 +832,44 @@ impl ReactServerComponentValidator {
} }
} }
} }
/// ```
/// import dynamic from 'next/dynamic'
///
/// dynamic(() => import(...)) // ✅
/// dynamic(() => import(...), { ssr: true }) // ✅
/// dynamic(() => import(...), { ssr: false }) // ❌
/// ```
fn check_for_next_ssr_false(&self, node: &CallExpr) -> Option<()> {
if !self.is_callee_next_dynamic(&node.callee) {
return None;
}
let ssr_arg = node.args.get(1)?;
let obj = ssr_arg.expr.as_object()?;
for prop in obj.props.iter().filter_map(|v| v.as_prop()?.as_key_value()) {
let is_ssr = match &prop.key {
PropName::Ident(IdentName { sym, .. }) => sym == "ssr",
PropName::Str(s) => s.value == "ssr",
_ => false,
};
if is_ssr {
let value = prop.value.as_lit()?;
if let Lit::Bool(Bool { value: false, .. }) = value {
report_error(
&self.app_dir,
&self.filepath,
RSCErrorKind::NextSsrDynamicFalseNotAllowed(node.span),
);
}
}
}
None
}
} }
impl Visit for ReactServerComponentValidator { impl Visit for ReactServerComponentValidator {
...@@ -830,7 +883,17 @@ impl Visit for ReactServerComponentValidator { ...@@ -830,7 +883,17 @@ impl Visit for ReactServerComponentValidator {
} }
} }
fn visit_call_expr(&mut self, node: &CallExpr) {
node.visit_children_with(self);
if self.is_react_server_layer {
self.check_for_next_ssr_false(node);
}
}
fn visit_module(&mut self, module: &Module) { fn visit_module(&mut self, module: &Module) {
self.imports = ImportMap::analyze(module);
let (is_client_entry, is_action_file, imports, export_names) = let (is_client_entry, is_action_file, imports, export_names) =
collect_top_level_directives_and_imports(&self.app_dir, &self.filepath, module); collect_top_level_directives_and_imports(&self.app_dir, &self.filepath, module);
let imports = Rc::new(imports); let imports = Rc::new(imports);
......
import dynamic from 'next/dynamic'
export default function () {
return dynamic(() => import('client-only'), { ssr: false })
}
import dynamic from 'next/dynamic';
export default function() {
return dynamic(()=>import('client-only'), {
ssr: false
});
}
x `ssr: false` is not allowed with `next/dynamic` in Server Components. Please move it into a client component.
,-[input.js:4:1]
3 | export default function () {
4 | return dynamic(() => import('client-only'), { ssr: false })
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | }
`----
...@@ -72,8 +72,6 @@ const ComponentC = dynamic(() => import('../components/C'), { ssr: false }) ...@@ -72,8 +72,6 @@ const ComponentC = dynamic(() => import('../components/C'), { ssr: false })
If you dynamically import a Server Component, only the Client Components that are children of the Server Component will be lazy-loaded - not the Server Component itself. If you dynamically import a Server Component, only the Client Components that are children of the Server Component will be lazy-loaded - not the Server Component itself.
It will also help preload the static assets such as CSS when you're using it in Server Components. It will also help preload the static assets such as CSS when you're using it in Server Components.
> **Note:** `ssr: false` option is not supported in Server Components.
```jsx filename="app/page.js" ```jsx filename="app/page.js"
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
...@@ -89,6 +87,9 @@ export default function ServerComponentExample() { ...@@ -89,6 +87,9 @@ export default function ServerComponentExample() {
} }
``` ```
> **Note:** `ssr: false` option is not supported in Server Components. You will see an error if you try to use it in Server Components.
> `ssr: false` is not allowed with `next/dynamic` in Server Components. Please move it into a client component.
### Loading External Libraries ### Loading External Libraries
External libraries can be loaded on demand using the [`import()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import) function. This example uses the external library `fuse.js` for fuzzy search. The module is only loaded on the client after the user types in the search input. External libraries can be loaded on demand using the [`import()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import) function. This example uses the external library `fuse.js` for fuzzy search. The module is only loaded on the client after the user types in the search input.
......
'use client'
export default function Client() {
return 'client'
}
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import dynamic from 'next/dynamic'
const DynamicClient = dynamic(() => import('./client'), { ssr: false })
export default function Page() {
return <DynamicClient />
}
import { nextTestSetup } from 'e2e-utils'
import {
assertHasRedbox,
getRedboxDescription,
getRedboxSource,
} from 'next-test-utils'
describe('app-dir - server-component-next-dynamic-ssr-false', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should error when use dynamic ssr:false in server component', async () => {
const browser = await next.browser('/')
await assertHasRedbox(browser)
const redbox = {
description: await getRedboxDescription(browser),
source: await getRedboxSource(browser),
}
expect(redbox.description).toBe('Failed to compile')
if (process.env.TURBOPACK) {
expect(redbox.source).toMatchInlineSnapshot(`
"./app/page.js:3:23
Ecmascript file had an error
1 | import dynamic from 'next/dynamic'
2 |
> 3 | const DynamicClient = dynamic(() => import('./client'), { ssr: false })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 |
5 | export default function Page() {
6 | return <DynamicClient />
\`ssr: false\` is not allowed with \`next/dynamic\` in Server Components. Please move it into a client component."
`)
} else {
expect(redbox.source).toMatchInlineSnapshot(`
"./app/page.js
Error: x \`ssr: false\` is not allowed with \`next/dynamic\` in Server Components. Please move it into a client component.
,-[3:1]
1 | import dynamic from 'next/dynamic'
2 |
3 | const DynamicClient = dynamic(() => import('./client'), { ssr: false })
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 |
5 | export default function Page() {
6 | return <DynamicClient />
\`----"
`)
}
})
})
import Client from './ssr-false-client' import Client from './ssr-false-client'
import Server from './ssr-false-server' // import Server from './ssr-false-server'
export default function Comp() { export default function Comp() {
return ( return (
<> <>
<Client /> <Client />
<Server /> {/* <Server /> */}
</> </>
) )
} }
'use client'
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
const Client1 = dynamic(() => import('./client')) const Client1 = dynamic(() => import('./client'))
......
...@@ -3,12 +3,12 @@ import dynamic from 'next/dynamic' ...@@ -3,12 +3,12 @@ import dynamic from 'next/dynamic'
export const NextDynamicServerComponent = dynamic( export const NextDynamicServerComponent = dynamic(
() => import('../text-dynamic-server') () => import('../text-dynamic-server')
) )
export const NextDynamicNoSSRServerComponent = dynamic( // export const NextDynamicNoSSRServerComponent = dynamic(
() => import('../text-dynamic-no-ssr-server'), // () => import('../text-dynamic-no-ssr-server'),
{ // {
ssr: false, // ssr: false,
} // }
) // )
export const NextDynamicServerImportClientComponent = dynamic( export const NextDynamicServerImportClientComponent = dynamic(
() => import('../text-dynamic-server-import-client') () => import('../text-dynamic-server-import-client')
) )
...@@ -3,7 +3,7 @@ import { NextDynamicClientComponent } from './dynamic-imports/dynamic-client' ...@@ -3,7 +3,7 @@ import { NextDynamicClientComponent } from './dynamic-imports/dynamic-client'
import { import {
NextDynamicServerComponent, NextDynamicServerComponent,
NextDynamicServerImportClientComponent, NextDynamicServerImportClientComponent,
NextDynamicNoSSRServerComponent, // NextDynamicNoSSRServerComponent,
} from './dynamic-imports/dynamic-server' } from './dynamic-imports/dynamic-server'
export default function page() { export default function page() {
...@@ -13,7 +13,7 @@ export default function page() { ...@@ -13,7 +13,7 @@ export default function page() {
<NextDynamicServerComponent /> <NextDynamicServerComponent />
<NextDynamicClientComponent /> <NextDynamicClientComponent />
<NextDynamicServerImportClientComponent /> <NextDynamicServerImportClientComponent />
<NextDynamicNoSSRServerComponent /> {/* <NextDynamicNoSSRServerComponent /> */}
</div> </div>
) )
} }
import TextClient from './text-client' // import TextClient from './text-client'
export default function Dynamic() { // export default function Dynamic() {
return ( // return (
<> // <>
<p id="css-text-dynamic-no-ssr-server"> // <p id="css-text-dynamic-no-ssr-server">
next-dynamic dynamic no ssr on server // next-dynamic dynamic no ssr on server
</p> // </p>
<TextClient /> // <TextClient />
</> // </>
) // )
} // }
...@@ -33,19 +33,10 @@ describe('app dir - next/dynamic', () => { ...@@ -33,19 +33,10 @@ describe('app dir - next/dynamic', () => {
expect(serverContent).toContain('next-dynamic dynamic on client') expect(serverContent).toContain('next-dynamic dynamic on client')
expect(serverContent).toContain('next-dynamic server import client') expect(serverContent).toContain('next-dynamic server import client')
expect(serverContent).not.toContain('next-dynamic dynamic no ssr on client') expect(serverContent).not.toContain('next-dynamic dynamic no ssr on client')
expect(serverContent).not.toContain('next-dynamic dynamic no ssr on server')
// client component under server component with ssr: false will not be rendered either in flight or SSR
expect($.html()).not.toContain('client component under sever no ssr')
}) })
it('should handle next/dynamic in hydration correctly', async () => { it('should handle next/dynamic in hydration correctly', async () => {
const selector = 'body div'
const browser = await next.browser('/dynamic') const browser = await next.browser('/dynamic')
const clientContent = await browser.elementByCss(selector).text()
expect(clientContent).toContain('next-dynamic dynamic no ssr on server')
expect(clientContent).toContain('client component under sever no ssr')
await browser.waitForElementByCss('#css-text-dynamic-no-ssr-client') await browser.waitForElementByCss('#css-text-dynamic-no-ssr-client')
expect( expect(
...@@ -91,17 +82,11 @@ describe('app dir - next/dynamic', () => { ...@@ -91,17 +82,11 @@ describe('app dir - next/dynamic', () => {
it('should not render client component imported through ssr: false in client components in edge runtime', async () => { it('should not render client component imported through ssr: false in client components in edge runtime', async () => {
// noSSR should not show up in html // noSSR should not show up in html
const $ = await next.render$('/dynamic-mixed-ssr-false/client-edge') const $ = await next.render$('/dynamic-mixed-ssr-false/client-edge')
expect($('#server-false-server-module')).not.toContain(
'ssr-false-server-module-text'
)
expect($('#server-false-client-module')).not.toContain( expect($('#server-false-client-module')).not.toContain(
'ssr-false-client-module-text' 'ssr-false-client-module-text'
) )
// noSSR should not show up in browser // noSSR should not show up in browser
const browser = await next.browser('/dynamic-mixed-ssr-false/client-edge') const browser = await next.browser('/dynamic-mixed-ssr-false/client-edge')
expect(
await browser.elementByCss('#ssr-false-server-module').text()
).toBe('ssr-false-server-module-text')
expect( expect(
await browser.elementByCss('#ssr-false-client-module').text() await browser.elementByCss('#ssr-false-client-module').text()
).toBe('ssr-false-client-module-text') ).toBe('ssr-false-client-module-text')
...@@ -119,17 +104,11 @@ describe('app dir - next/dynamic', () => { ...@@ -119,17 +104,11 @@ describe('app dir - next/dynamic', () => {
it('should not render client component imported through ssr: false in client components', async () => { it('should not render client component imported through ssr: false in client components', async () => {
// noSSR should not show up in html // noSSR should not show up in html
const $ = await next.render$('/dynamic-mixed-ssr-false/client') const $ = await next.render$('/dynamic-mixed-ssr-false/client')
expect($('#client-false-server-module')).not.toContain(
'ssr-false-server-module-text'
)
expect($('#client-false-client-module')).not.toContain( expect($('#client-false-client-module')).not.toContain(
'ssr-false-client-module-text' 'ssr-false-client-module-text'
) )
// noSSR should not show up in browser // noSSR should not show up in browser
const browser = await next.browser('/dynamic-mixed-ssr-false/client') const browser = await next.browser('/dynamic-mixed-ssr-false/client')
expect(
await browser.elementByCss('#ssr-false-server-module').text()
).toBe('ssr-false-server-module-text')
expect( expect(
await browser.elementByCss('#ssr-false-client-module').text() await browser.elementByCss('#ssr-false-client-module').text()
).toBe('ssr-false-client-module-text') ).toBe('ssr-false-client-module-text')
...@@ -139,7 +118,6 @@ describe('app dir - next/dynamic', () => { ...@@ -139,7 +118,6 @@ describe('app dir - next/dynamic', () => {
const pageServerChunk = await next.readFile( const pageServerChunk = await next.readFile(
'.next/server/app/dynamic-mixed-ssr-false/client/page.js' '.next/server/app/dynamic-mixed-ssr-false/client/page.js'
) )
expect(pageServerChunk).not.toContain('ssr-false-server-module-text')
expect(pageServerChunk).not.toContain('ssr-false-client-module-text') expect(pageServerChunk).not.toContain('ssr-false-client-module-text')
} }
}) })
......
'use client'
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
const Dynamic = dynamic(() => import('./dynamic'), { const Dynamic = dynamic(() => import('./dynamic'), {
......
'use client'
import dynamic from 'next/dynamic'
export const DynamicStaticImg = dynamic(
() => import('../../components/static-img'),
{
ssr: false,
}
)
import dynamic from 'next/dynamic' import { DynamicStaticImg } from './async-image'
const DynamicStaticImg = dynamic(() => import('../../components/static-img'), {
ssr: false,
})
export default () => { export default () => {
return ( return (
......
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