Skip to content

Commit

Permalink
Merge branch 'canary' into feat/app-static-generation
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/next/build/index.ts
#	packages/next/server/app-render.tsx
#	test/e2e/app-dir/app-rendering/app/getserversideprops-only/layout.js
#	test/e2e/app-dir/app-rendering/app/getserversideprops-only/nested/page.js
#	test/e2e/app-dir/app-rendering/app/getserversideprops-only/slow/layout.js
#	test/e2e/app-dir/app-rendering/app/getserversideprops-only/slow/page.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-getserversideprops-combined/nested/layout.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-getserversideprops-combined/nested/page.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-getserversideprops-combined/slow/layout.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-getserversideprops-combined/slow/page.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-isr-multiple/layout.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-isr-multiple/nested/page.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-only/layout.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-only/nested/page.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-only/slow/layout.js
#	test/e2e/app-dir/app-rendering/app/getstaticprops-only/slow/page.js
#	test/e2e/app-dir/app/pages/api/preview.js
  • Loading branch information
ijjk committed Sep 19, 2022
2 parents 974be76 + db3b844 commit 368351c
Show file tree
Hide file tree
Showing 240 changed files with 689 additions and 774 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@
"react-17": "npm:[email protected]",
"react-dom": "18.2.0",
"react-dom-17": "npm:[email protected]",
"react-dom-exp": "npm:[email protected]e6a062bd2-20220913",
"react-exp": "npm:[email protected]e6a062bd2-20220913",
"react-dom-exp": "npm:[email protected]8951c5fc9-20220915",
"react-exp": "npm:[email protected]8951c5fc9-20220915",
"react-ssr-prepass": "1.0.8",
"react-virtualized": "9.22.3",
"relay-compiler": "13.0.2",
Expand All @@ -207,7 +207,7 @@
},
"resolutions": {
"browserslist": "4.20.2",
"caniuse-lite": "1.0.30001332",
"caniuse-lite": "1.0.30001406",
"@babel/core": "7.18.0",
"@babel/parser": "7.18.0",
"@babel/types": "7.18.0",
Expand Down
123 changes: 102 additions & 21 deletions packages/next-swc/crates/core/src/react_server_components.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use regex::Regex;
use serde::Deserialize;

use swc_core::{
Expand Down Expand Up @@ -63,7 +64,7 @@ impl<C: Comments> VisitMut for ReactServerComponents<C> {
return;
}
} else {
self.assert_client_graph(&imports);
self.assert_client_graph(&imports, module);
}
module.visit_mut_children_with(self)
}
Expand Down Expand Up @@ -226,11 +227,7 @@ impl<C: Comments> ReactServerComponents<C> {
handler
.struct_span_err(
import.source.1,
format!(
"Disallowed import of `{}` in the Server Components compilation.",
source
)
.as_str(),
format!("NEXT_RSC_ERR_SERVER_IMPORT: {}", source).as_str(),
)
.emit()
})
Expand All @@ -242,12 +239,7 @@ impl<C: Comments> ReactServerComponents<C> {
handler
.struct_span_err(
specifier.1,
format!(
"Disallowed React API `{}` in the Server Components \
compilation.",
&specifier.0
)
.as_str(),
format!("NEXT_RSC_ERR_REACT_API: {}", &specifier.0).as_str(),
)
.emit()
})
Expand All @@ -261,12 +253,7 @@ impl<C: Comments> ReactServerComponents<C> {
handler
.struct_span_err(
specifier.1,
format!(
"Disallowed ReactDOM API `{}` in the Server Components \
compilation.",
&specifier.0
)
.as_str(),
format!("NEXT_RSC_ERR_REACT_API: {}", &specifier.0).as_str(),
)
.emit()
})
Expand All @@ -276,17 +263,111 @@ impl<C: Comments> ReactServerComponents<C> {
}
}

fn assert_client_graph(&self, imports: &Vec<ModuleImports>) {
fn assert_client_graph(&self, imports: &Vec<ModuleImports>, module: &Module) {
for import in imports {
let source = import.source.0.clone();
if self.invalid_client_imports.contains(&source) {
HANDLER.with(|handler| {
handler
.struct_span_err(
import.source.1,
format!("NEXT_RSC_ERR_CLIENT_IMPORT: {}", source).as_str(),
)
.emit()
})
}
}

// Assert `getServerSideProps` and `getStaticProps` exports.
let is_layout_or_page = Regex::new(r"/(page|layout)\.(ts|js)x?$")
.unwrap()
.is_match(&self.filepath);
if is_layout_or_page {
let mut span = DUMMY_SP;
let mut has_get_server_side_props = false;
let mut has_get_static_props = false;

'matcher: for export in &module.body {
match export {
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(export)) => {
for specifier in &export.specifiers {
if let ExportSpecifier::Named(named) = specifier {
match &named.orig {
ModuleExportName::Ident(i) => {
if i.sym == *"getServerSideProps" {
has_get_server_side_props = true;
span = named.span;
break 'matcher;
}
if i.sym == *"getStaticProps" {
has_get_static_props = true;
span = named.span;
break 'matcher;
}
}
ModuleExportName::Str(s) => {
if s.value == *"getServerSideProps" {
has_get_server_side_props = true;
span = named.span;
break 'matcher;
}
if s.value == *"getStaticProps" {
has_get_static_props = true;
span = named.span;
break 'matcher;
}
}
}
}
}
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(export)) => match &export.decl {
Decl::Fn(f) => {
if f.ident.sym == *"getServerSideProps" {
has_get_server_side_props = true;
span = f.ident.span;
break 'matcher;
}
if f.ident.sym == *"getStaticProps" {
has_get_static_props = true;
span = f.ident.span;
break 'matcher;
}
}
Decl::Var(v) => {
for decl in &v.decls {
if let Pat::Ident(i) = &decl.name {
if i.sym == *"getServerSideProps" {
has_get_server_side_props = true;
span = i.span;
break 'matcher;
}
if i.sym == *"getStaticProps" {
has_get_static_props = true;
span = i.span;
break 'matcher;
}
}
}
}
_ => {}
},
_ => {}
}
}

if has_get_server_side_props || has_get_static_props {
HANDLER.with(|handler| {
handler
.struct_span_err(
span,
format!(
"Disallowed import of `{}` in the Client Components compilation.",
source
"`{}` is not allowed in Client Components.",
if has_get_server_side_props {
"getServerSideProps"
} else {
"getStaticProps"
}
)
.as_str(),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/core/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn react_server_components_client_graph_errors(input: PathBuf) {
syntax(),
&|tr| {
server_components(
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
FileName::Real(PathBuf::from("/some-project/src/page.js")),
next_swc::react_server_components::Config::WithOptions(
next_swc::react_server_components::Options { is_server: false },
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function getServerSideProps (){
}

export default function () {
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getServerSideProps() {}
export default function() {
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

x `getServerSideProps` is not allowed in Client Components.
,-[input.js:1:1]
1 | export function getServerSideProps (){
: ^^^^^^^^^^^^^^^^^^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function getStaticProps (){
}

export default function () {
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getStaticProps() {}
export default function() {
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

x `getStaticProps` is not allowed in Client Components.
,-[input.js:1:1]
1 | export function getStaticProps (){
: ^^^^^^^^^^^^^^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

x Disallowed import of `server-only` in the Client Components compilation.
x NEXT_RSC_ERR_CLIENT_IMPORT: server-only
,-[input.js:9:1]
9 | import "server-only"
: ^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

x Disallowed import of `client-only` in the Server Components compilation.
x NEXT_RSC_ERR_SERVER_IMPORT: client-only
,-[input.js:9:1]
9 | import "client-only"
: ^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@

x Disallowed React API `useState` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useState
,-[input.js:1:1]
1 | import { useState } from 'react'
: ^^^^^^^^
`----

x Disallowed React API `createContext` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: createContext
,-[input.js:3:1]
3 | import { createContext } from 'react'
: ^^^^^^^^^^^^^
`----

x Disallowed React API `useEffect` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useEffect
,-[input.js:5:1]
5 | import { useEffect, useImperativeHandle } from 'react'
: ^^^^^^^^^
`----

x Disallowed React API `useImperativeHandle` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useImperativeHandle
,-[input.js:5:1]
5 | import { useEffect, useImperativeHandle } from 'react'
: ^^^^^^^^^^^^^^^^^^^
`----

x Disallowed React API `Component` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: Component
,-[input.js:8:5]
8 | Component,
: ^^^^^^^^^
`----

x Disallowed React API `createFactory` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: createFactory
,-[input.js:9:5]
9 | createFactory,
: ^^^^^^^^^^^^^
`----

x Disallowed React API `PureComponent` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: PureComponent
,-[input.js:10:5]
10 | PureComponent,
: ^^^^^^^^^^^^^
`----

x Disallowed React API `useDeferredValue` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useDeferredValue
,-[input.js:11:3]
11 | useDeferredValue,
: ^^^^^^^^^^^^^^^^
`----

x Disallowed React API `useInsertionEffect` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useInsertionEffect
,-[input.js:12:5]
12 | useInsertionEffect,
: ^^^^^^^^^^^^^^^^^^
`----

x Disallowed React API `useLayoutEffect` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useLayoutEffect
,-[input.js:13:5]
13 | useLayoutEffect,
: ^^^^^^^^^^^^^^^
`----

x Disallowed React API `useReducer` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useReducer
,-[input.js:14:5]
14 | useReducer,
: ^^^^^^^^^^
`----

x Disallowed React API `useRef` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useRef
,-[input.js:15:5]
15 | useRef,
: ^^^^^^
`----

x Disallowed React API `useSyncExternalStore` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: useSyncExternalStore
,-[input.js:16:5]
16 | useSyncExternalStore
: ^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

x Disallowed ReactDOM API `findDOMNode` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: findDOMNode
,-[input.js:2:5]
2 | findDOMNode,
: ^^^^^^^^^^^
`----

x Disallowed ReactDOM API `flushSync` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: flushSync
,-[input.js:3:3]
3 | flushSync,
: ^^^^^^^^^
`----

x Disallowed ReactDOM API `unstable_batchedUpdates` in the Server Components compilation.
x NEXT_RSC_ERR_REACT_API: unstable_batchedUpdates
,-[input.js:4:3]
4 | unstable_batchedUpdates,
: ^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

x Disallowed import of `react-dom/server` in the Server Components compilation.
x NEXT_RSC_ERR_SERVER_IMPORT: react-dom/server
,-[input.js:9:1]
9 | import "react-dom/server"
: ^^^^^^^^^^^^^^^^^^^^^^^^^
`----

x Disallowed import of `react-dom/client` in the Server Components compilation.
x NEXT_RSC_ERR_SERVER_IMPORT: react-dom/client
,-[input.js:11:1]
11 | import "react-dom/client"
: ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading

0 comments on commit 368351c

Please sign in to comment.