Skip to content

Commit

Permalink
Merge branch 'canary' into mannyb/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mannybecerra committed Jan 11, 2022
2 parents 247c64c + 0de8447 commit 9f94dfe
Show file tree
Hide file tree
Showing 55 changed files with 558 additions and 394 deletions.
4 changes: 4 additions & 0 deletions docs/advanced-features/custom-error-page.md
Expand Up @@ -94,3 +94,7 @@ export default function Page({ errorCode, stars }) {
The `Error` component also takes `title` as a property if you want to pass in a text message along with a `statusCode`.

If you have a custom `Error` component be sure to import that one instead. `next/error` exports the default component used by Next.js.

### Caveats

- `Error` currently does not support Next.js [Data Fetching methods](/docs/basic-features/data-fetching.md) like [`getStaticProps`](/docs/basic-features/data-fetching.md#getstaticprops-static-generation) or [`getServerSideProps`](/docs/basic-features/data-fetching.md#getserversideprops-server-side-rendering).
2 changes: 1 addition & 1 deletion docs/advanced-features/src-directory.md
Expand Up @@ -11,7 +11,7 @@ The `src` directory is very common in many apps and Next.js supports it by defau
## Caveats

- `src/pages` will be ignored if `pages` is present in the root directory
- Config files like `next.config.js` and `tsconfig.json` should be inside the root directory, moving them to `src` won't work. Same goes for the [`public` directory](/docs/basic-features/static-file-serving.md)
- Config files like `next.config.js` and `tsconfig.json`, as well as environment variables, should be inside the root directory, moving them to `src` won't work. Same goes for the [`public` directory](/docs/basic-features/static-file-serving.md)

## Related

Expand Down
2 changes: 2 additions & 0 deletions docs/basic-features/environment-variables.md
Expand Up @@ -76,6 +76,8 @@ export async function getStaticProps() {
> CORRECT=pre\$A
> ```
> **Note**: If you are using a `/src` folder, please note that Next.js will load the .env files **only** from the parent folder and **not** from the `/src` folder.
## Exposing Environment Variables to the Browser

By default environment variables are only available in the Node.js environment, meaning they won't be exposed to the browser.
Expand Down
18 changes: 15 additions & 3 deletions errors/threw-undefined.md
@@ -1,9 +1,21 @@
# Threw undefined in render
# Threw `undefined`/`null`

#### Why This Error Occurred

Somewhere in your code you `throw` an `undefined` value. Since this isn't a valid error there isn't a stack trace. We show this error instead to let you know what to look for.
Somewhere in your code you `throw` an `undefined` or `null` value. Since this isn't a valid error there isn't a stack trace. We show this error instead to let you know what to look for.

```js
function getData() {
let error
throw error
}

function Page() {
const error = data?.error || null
throw error
}
```
#### Possible Ways to Fix It
Look in your pages and find where an error could be throwing `undefined`
Look in your pages and find where an error could be throwing `undefined` or `null` values and ensure `new Error()` is used instead.
2 changes: 1 addition & 1 deletion lerna.json
Expand Up @@ -16,5 +16,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "12.0.8-canary.19"
"version": "12.0.8-canary.21"
}
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"keywords": [
"react",
"next",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config-next/package.json
@@ -1,6 +1,6 @@
{
"name": "eslint-config-next",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"description": "ESLint configuration used by NextJS.",
"main": "index.js",
"license": "MIT",
Expand All @@ -9,7 +9,7 @@
"directory": "packages/eslint-config-next"
},
"dependencies": {
"@next/eslint-plugin-next": "12.0.8-canary.19",
"@next/eslint-plugin-next": "12.0.8-canary.21",
"@rushstack/eslint-patch": "^1.0.8",
"@typescript-eslint/parser": "^5.0.0",
"eslint-import-resolver-node": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-next/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/eslint-plugin-next",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"description": "ESLint plugin for NextJS.",
"main": "lib/index.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-bundle-analyzer/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/bundle-analyzer",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-codemod/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/codemod",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"license": "MIT",
"dependencies": {
"chalk": "4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-env/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/env",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-mdx/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/mdx",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-storybook/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-storybook",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-storybook"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-module/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-module",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-nomodule/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-nomodule",
"version": "12.0.8-canary.19",
"version": "12.0.8-canary.21",
"description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js",
"license": "MIT",
Expand Down
43 changes: 40 additions & 3 deletions packages/next-swc/crates/core/src/next_dynamic.rs
Expand Up @@ -4,10 +4,11 @@ use pathdiff::diff_paths;
use swc_atoms::js_word;
use swc_common::{FileName, DUMMY_SP};
use swc_ecmascript::ast::{
ArrayLit, ArrowExpr, BinExpr, BinaryOp, BlockStmtOrExpr, CallExpr, Expr, ExprOrSpread,
ExprOrSuper, Ident, ImportDecl, ImportSpecifier, KeyValueProp, Lit, MemberExpr, ObjectLit,
Prop, PropName, PropOrSpread, Str, StrKind,
ArrayLit, ArrowExpr, BinExpr, BinaryOp, BlockStmtOrExpr, Bool, CallExpr, Expr, ExprOrSpread,
ExprOrSuper, Ident, ImportDecl, ImportSpecifier, KeyValueProp, Lit, MemberExpr, Null,
ObjectLit, Prop, PropName, PropOrSpread, Str, StrKind,
};
use swc_ecmascript::utils::ExprFactory;
use swc_ecmascript::utils::{
ident::{Id, IdentLike},
HANDLER,
Expand Down Expand Up @@ -236,16 +237,52 @@ impl Fold for NextDynamicPatcher {
value: generated,
})))];

let mut has_ssr_false = false;

if expr.args.len() == 2 {
if let Expr::Object(ObjectLit {
props: options_props,
..
}) = &*expr.args[1].expr
{
for prop in options_props.iter() {
if let Some(KeyValueProp { key, value }) = match prop {
PropOrSpread::Prop(prop) => match &**prop {
Prop::KeyValue(key_value_prop) => Some(key_value_prop),
_ => None,
},
_ => None,
} {
if let Some(Ident {
sym,
span: _,
optional: _,
}) = match key {
PropName::Ident(ident) => Some(ident),
_ => None,
} {
if sym == "ssr" {
if let Some(Lit::Bool(Bool {
value: false,
span: _,
})) = match &**value {
Expr::Lit(lit) => Some(lit),
_ => None,
} {
has_ssr_false = true
}
}
}
}
}
props.extend(options_props.iter().cloned());
}
}

if has_ssr_false && self.is_server {
expr.args[0] = Lit::Null(Null { span: DUMMY_SP }).as_arg();
}

let second_arg = ExprOrSpread {
spread: None,
expr: Box::new(Expr::Object(ObjectLit {
Expand Down
25 changes: 25 additions & 0 deletions packages/next-swc/crates/core/src/next_ssg.rs
Expand Up @@ -141,6 +141,31 @@ impl Fold for Analyzer<'_> {
e
}

fn fold_jsx_element(&mut self, jsx: JSXElement) -> JSXElement {
fn get_leftmost_id_member_expr(e: &JSXMemberExpr) -> Id {
match &e.obj {
JSXObject::Ident(i) => {
i.to_id()
}
JSXObject::JSXMemberExpr(e) => {
get_leftmost_id_member_expr(e)
}
}
}

match &jsx.opening.name {
JSXElementName::Ident(i) => {
self.add_ref(i.to_id());
}
JSXElementName::JSXMemberExpr(e) => {
self.add_ref(get_leftmost_id_member_expr(e));
}
_ => {}
}

jsx.fold_children_with(self)
}

fn fold_fn_decl(&mut self, f: FnDecl) -> FnDecl {
let old_in_data = self.in_data_fn;

Expand Down
14 changes: 14 additions & 0 deletions packages/next-swc/crates/core/tests/fixture.rs
Expand Up @@ -34,6 +34,7 @@ fn amp_attributes_fixture(input: PathBuf) {
fn next_dynamic_fixture(input: PathBuf) {
let output_dev = input.parent().unwrap().join("output-dev.js");
let output_prod = input.parent().unwrap().join("output-prod.js");
let output_server = input.parent().unwrap().join("output-server.js");
test_fixture(
syntax(),
&|_tr| {
Expand All @@ -60,6 +61,19 @@ fn next_dynamic_fixture(input: PathBuf) {
&input,
&output_prod,
);
test_fixture(
syntax(),
&|_tr| {
next_dynamic(
false,
true,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
},
&input,
&output_server,
);
}

#[fixture("tests/fixture/ssg/**/input.js")]
Expand Down
@@ -0,0 +1,12 @@
import dynamic1 from 'next/dynamic'
import dynamic2 from 'next/dynamic'
const DynamicComponent1 = dynamic1(() => import('../components/hello1'), {
loadableGenerated: {
modules: ['some-file.js -> ' + '../components/hello1'],
},
})
const DynamicComponent2 = dynamic2(() => import('../components/hello2'), {
loadableGenerated: {
modules: ['some-file.js -> ' + '../components/hello2'],
},
})
@@ -0,0 +1,8 @@
import dynamic from 'next/dynamic'
import somethingElse from 'something-else'
const DynamicComponent = dynamic(() => import('../components/hello'), {
loadableGenerated: {
modules: ['some-file.js -> ' + '../components/hello'],
},
})
somethingElse.dynamic('should not be transformed')
@@ -0,0 +1,6 @@
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../components/hello'), {
loadableGenerated: {
modules: ['some-file.js -> ' + '../components/hello'],
},
})
Expand Up @@ -4,3 +4,8 @@ const DynamicComponentWithCustomLoading = dynamic(
() => import('../components/hello'),
{ loading: () => <p>...</p> }
)

const DynamicClientOnlyComponent = dynamic(
() => import('../components/hello'),
{ ssr: false }
)
Expand Up @@ -8,3 +8,12 @@ const DynamicComponentWithCustomLoading = dynamic(()=>import("../components/hell
},
loading: ()=><p >...</p>
});
const DynamicClientOnlyComponent = dynamic(()=>import("../components/hello")
, {
loadableGenerated: {
modules: [
"some-file.js -> " + "../components/hello"
]
},
ssr: false
});
Expand Up @@ -8,3 +8,12 @@ const DynamicComponentWithCustomLoading = dynamic(()=>import("../components/hell
},
loading: ()=><p >...</p>
});
const DynamicClientOnlyComponent = dynamic(()=>import("../components/hello")
, {
loadableGenerated: {
webpack: ()=>[
require.resolveWeak("../components/hello")
]
},
ssr: false
});
@@ -0,0 +1,16 @@
import dynamic from 'next/dynamic'
const DynamicComponentWithCustomLoading = dynamic(
() => import('../components/hello'),
{
loadableGenerated: {
modules: ['some-file.js -> ' + '../components/hello'],
},
loading: () => <p>...</p>,
}
)
const DynamicClientOnlyComponent = dynamic(null, {
loadableGenerated: {
modules: ['some-file.js -> ' + '../components/hello'],
},
ssr: false,
})
@@ -0,0 +1,11 @@
import dynamic from "next/dynamic";
const DynamicComponent = dynamic(null, {
loadableGenerated: {
modules: [
"some-file.js -> " + "./components/hello"
]
},
loading: ()=>null
,
ssr: false
});

0 comments on commit 9f94dfe

Please sign in to comment.