Skip to content

Commit

Permalink
Merge branch 'canary' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Sep 13, 2022
2 parents aef8f0a + 3f2b4bc commit ff150dd
Show file tree
Hide file tree
Showing 263 changed files with 4,748 additions and 2,225 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build_test_deploy.yml
Expand Up @@ -51,7 +51,7 @@ jobs:
run: sudo ethtool -K eth0 tx off rx off

- name: Check non-docs only change
run: echo ::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')
run: echo "::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')"
id: docs-change

- run: echo ${{steps.docs-change.outputs.DOCS_CHANGE}}
Expand Down Expand Up @@ -124,7 +124,7 @@ jobs:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}

- run: echo ::set-output name=SWC_CHANGE::$(node scripts/run-for-change.js --type next-swc --exec echo 'yup')
- run: echo "::set-output name=SWC_CHANGE::$(node scripts/run-for-change.js --type next-swc --exec echo 'yup')"
id: swc-change

- run: echo ${{ steps.swc-change.outputs.SWC_CHANGE }}
Expand Down Expand Up @@ -1102,7 +1102,7 @@ jobs:
with:
fetch-depth: 25

- run: echo ::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')
- run: echo "::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')"
id: docs-change

- name: Setup node
Expand Down Expand Up @@ -1191,7 +1191,7 @@ jobs:
with:
fetch-depth: 25

- run: echo ::set-output name=SWC_CHANGE::$(node scripts/run-for-change.js --type next-swc --exec echo 'yup')
- run: echo "::set-output name=SWC_CHANGE::$(node scripts/run-for-change.js --type next-swc --exec echo 'yup')"
id: swc-change

- run: echo ${{ steps.swc-change.outputs.SWC_CHANGE }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pull_request_stats.yml
Expand Up @@ -24,7 +24,7 @@ jobs:
fetch-depth: 25

- name: Check non-docs only change
run: echo ::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')
run: echo "::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')"
id: docs-change

- name: Setup node
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
fetch-depth: 25

- name: Check non-docs only change
run: echo ::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')
run: echo "::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')"
id: docs-change

- uses: actions/download-artifact@v3
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/validate_issue.yml
Expand Up @@ -15,4 +15,3 @@ jobs:
run: node ./.github/actions/issue-validator/index.mjs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEBUG: 1
5 changes: 3 additions & 2 deletions docs/advanced-features/compiler.md
Expand Up @@ -9,6 +9,7 @@ description: Learn about the Next.js Compiler, written in Rust, which transforms

| Version | Changes |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `v12.3.0` | SWC Minifier [stable](https://nextjs.org/blog/next-12-3#swc-minifier-stable). |
| `v12.2.0` | [SWC Plugins](#swc-plugins-Experimental) experimental support added. |
| `v12.1.0` | Added support for Styled Components, Jest, Relay, Remove React Properties, Legacy Decorators, Remove Console, and jsxImportSource. |
| `v12.0.0` | Next.js Compiler [introduced](https://nextjs.org/blog/next-12). |
Expand Down Expand Up @@ -238,8 +239,6 @@ module.exports = {

Only `importMap` in `@emotion/babel-plugin` is not supported for now.

## Experimental Features

### Minification

You can opt-in to using the Next.js compiler for minification. This is 7x faster than Terser.
Expand All @@ -254,6 +253,8 @@ module.exports = {

If you have feedback about `swcMinify`, please share it on the [feedback discussion](https://github.com/vercel/next.js/discussions/30237).

## Experimental Features

### Minifier debug options

While the minifier is experimental, we are making the following options available for debugging purposes. They will not be available once the minifier is made stable.
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced-features/dynamic-import.md
Expand Up @@ -42,7 +42,7 @@ If you are not using React 18, you can use the `loading` attribute in place of t

```jsx
const DynamicHeader = dynamic(() => import('../components/header'), {
loading: () => <header />,
loading: () => <div>Loading...</div>,
})
```

Expand Down
8 changes: 4 additions & 4 deletions docs/advanced-features/middleware.md
Expand Up @@ -31,7 +31,7 @@ To begin using Middleware, follow the steps below:
npm install next@latest
```

2. Create a `middleware.ts` (or `.js`) file at the same level as your `pages` directory
2. Create a `middleware.ts` (or `.js`) file at the root or in the `src` directory (same level as your `pages`)
3. Export a middleware function from the `middleware.ts` file:

```typescript
Expand Down Expand Up @@ -93,9 +93,9 @@ export const config = {
Configured matchers:

1. MUST start with `/`
1. can include named parameters: `/about/:path` matches `/about/a` and `/about/b` but not `/about/a/c`
1. can have modifiers on named parameters (starting with `:`): `/about/:path*` matches `/about/a/b/c` because `*` is _zero or more_. `?` is _zero or one_ and `+` _one or more_
1. can use regular expression enclosed in parenthesis: `/about/(.*)` is the same as `/about/:path*`
2. Can include named parameters: `/about/:path` matches `/about/a` and `/about/b` but not `/about/a/c`
3. Can have modifiers on named parameters (starting with `:`): `/about/:path*` matches `/about/a/b/c` because `*` is _zero or more_. `?` is _zero or one_ and `+` _one or more_
4. Can use regular expression enclosed in parenthesis: `/about/(.*)` is the same as `/about/:path*`

Read more details on [path-to-regexp](https://github.com/pillarjs/path-to-regexp#path-to-regexp-1) documentation.

Expand Down
115 changes: 13 additions & 102 deletions docs/advanced-features/react-18/server-components.md
@@ -1,116 +1,27 @@
# React Server Components (RFC)

Server Components allow us to render React components on the server. This is fundamentally different from server-side rendering (SSR) where you're pre-generating HTML on the server. With Server Components, there's **zero client-side JavaScript needed,** making page rendering faster. This improves the user experience of your application, pairing the best parts of server-rendering with client-side interactivity.
React Server Components allow developers to build applications that span the server and client, combining the rich interactivity of client-side apps with the improved performance of traditional server rendering.

### Next Router and Layouts RFC
In an upcoming Next.js release, React and Next.js developers will be able to use Server Components inside the `app` directory as part of the changes outlined by the [Layouts RFC](https://nextjs.org/blog/layouts-rfc).

We are currently implementing the [Next.js Router and Layouts RFC](https://nextjs.org/blog/layouts-rfc).
## What are React Server Components?

The new Next.js router will be built on top of React 18 features, including React Server Components.
React Server Components improve the user experience of your application by pairing the best parts of server-rendering with client-side interactivity.

One of the biggest proposed changes is that, by default, files inside a new `app` directory will be rendered on the server as React Server Components.
With traditional React applications that are client-side only, developers often had to make tradeoffs between SEO and performance. Server Components enable developers to better leverage their server infrastructure and achieve great performance by default.

This will allow you to automatically adopt React Server Components when migrating from `pages` to `app`.
For example, large dependencies that previously would impact the JavaScript bundle size on the client can instead stay entirely on the server. By sending less JavaScript to the browser, the time to interactive for the page is decreased, leading to improved [Core Web Vitals](https://vercel.com/blog/core-web-vitals).

You can find more information on the [RFC](https://nextjs.org/blog/layouts-rfc) and we welcome your feedback on [Github Discussions](https://github.com/vercel/next.js/discussions/37136).
## React Server Components vs Server-Side Rendering

### Server Components Conventions
[Server-side Rendering](/docs/basic-features/pages.md#server-side-rendering) (SSR) dynamically builds your application into HTML on the server. This creates faster load times for users by offloading work from the user's device to the server, especially those with slower internet connections or older devices. However, developers still pay the cost to download, parse, and hydrate those components after the initial HTML loads.

To run a component on the server, append `.server.js` to the end of the filename. For example, `./pages/home.server.js` will be treated as a Server Component.
React Server Components, combined with Next.js server-side rendering, help eliminate the tradeoff of all-or-nothing data fetching. You can progressively show updates as your data comes in.

For client components, append `.client.js` to the filename. For example, `./components/avatar.client.js`.
## Using React Server Components with Next.js

Server components can import server components and client components.
The Next.js team at Vercel released the [Layouts RFC](https://nextjs.org/blog/layouts-rfc) a few months ago outlining the vision for the future of routing, layouts, and data fetching in the framework. These changes **aren't available yet**, but we can start learning about how they will be used.

Client components **cannot** import server components.
Pages and Layouts in `app` will be rendered as React Server Components by default. This improves performance by reducing the amount of JavaScript sent to the client for components that are not interactive. Client components will be able to be defined through either a file name extension or through a string literal in the file.

Components without a `server` or `client` extension will be treated as shared components and can be imported by server components and client components. For example:

```jsx
// pages/home.server.js

import { Suspense } from 'react'

import Profile from '../components/profile.server'
import Content from '../components/content.client'

export default function Home() {
return (
<div>
<h1>Welcome to React Server Components</h1>
<Suspense fallback={'Loading...'}>
<Profile />
</Suspense>
<Content />
</div>
)
}
```

The `<Home>` and `<Profile>` components will always be server-side rendered and streamed to the client, and will not be included by the client-side JavaScript. However, `<Content>` will still be hydrated on the client-side, like normal React components.

> Make sure you're using default imports and exports for server components (`.server.js`). The support of named exports are a work in progress!
To see a full example, check out the [vercel/next-react-server-components demo](https://github.com/vercel/next-react-server-components).

## Supported Next.js APIs

### `next/link` and `next/image`

You can use `next/link` and `next/image` like before and they will be treated as client components to keep the interaction on client side.

### `next/document`

If you have a custom `_document`, you have to change your `_document` to a functional component like below to use server components. If you don't have one, Next.js will use the default `_document` component for you.

```jsx
// pages/_document.js
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
```

### `next/app`

The usage of `_app.js` is the same as [Custom App](/docs/advanced-features/custom-app). Using custom app as server component such as `_app.server.js` is not recommended, to keep align with non server components apps for client specific things like global CSS imports.

### Routing

Both basic routes with path and queries and dynamic routes are supported. If you need to access the router in server components(`.server.js`), they will receive `router` instance as a prop so that you can directly access them without using the `useRouter()` hook.

```jsx
// pages/index.server.js

export default function Index({ router }) {
// You can access routing information by `router.pathname`, etc.
return 'hello'
}
```

### Unsupported Next.js APIs

While RSC and SSR streaming are still in the alpha stage, not all Next.js APIs are supported. The following Next.js APIs have limited functionality within Server Components. React 18 use without SSR streaming is not affected.

#### React internals

Most React hooks, such as `useContext`, `useState`, `useReducer`, `useEffect` and `useLayoutEffect`, are not supported as of today since server components are executed per request and aren't stateful.

#### Data Fetching & Styling

Like streaming SSR, styling and data fetching within `Suspense` on the server side are not well supported. We're still working on them.

Page level exported methods like `getInitialProps`, `getStaticProps` and `getStaticPaths` are not supported.

#### `next/head` and I18n

We are still working on support for these features.
We will be providing more updates about Server Components usage in Next.js soon.
2 changes: 1 addition & 1 deletion docs/advanced-features/using-mdx.md
Expand Up @@ -33,7 +33,7 @@ The following steps outline how to setup `@next/mdx` in your Next.js project:
1. Install the required packages:

```bash
npm install @next/mdx @mdx-js/loader
npm install @next/mdx @mdx-js/loader @mdx-js/react
```

2. Require the package and configure to support top level `.mdx` pages. The following adds the `options` object key allowing you to pass in any plugins:
Expand Down
1 change: 1 addition & 0 deletions docs/api-reference/data-fetching/get-static-paths.md
Expand Up @@ -121,6 +121,7 @@ If `fallback` is `true`, then the behavior of `getStaticProps` changes in the fo

- The paths returned from `getStaticPaths` will be rendered to `HTML` at build time by `getStaticProps`.
- The paths that have not been generated at build time will **not** result in a 404 page. Instead, Next.js will serve a [“fallback”](#fallback-pages) version of the page on the first request to such a path. Web crawlers, such as Google, won't be served a fallback and instead the path will behave as in [`fallback: 'blocking'`](#fallback-blocking).
- When a page with `fallback: true` is navigated to through `next/link` or `next/router` (client-side) Next.js will _not_ serve a fallback and instead the page will behave as [`fallback: 'blocking'`](#fallback-blocking).
- In the background, Next.js will statically generate the requested path `HTML` and `JSON`. This includes running `getStaticProps`.
- When complete, the browser receives the `JSON` for the generated path. This will be used to automatically render the page with the required props. From the user’s perspective, the page will be swapped from the fallback page to the full page.
- At the same time, Next.js adds this path to the list of pre-rendered pages. Subsequent requests to the same path will serve the generated page, like other pages pre-rendered at build time.
Expand Down
19 changes: 19 additions & 0 deletions docs/api-reference/edge-runtime.md
Expand Up @@ -136,6 +136,25 @@ The following JavaScript language features are disabled, and **will not work:**

- `eval`: Evaluates JavaScript code represented as a string
- `new Function(evalString)`: Creates a new function with the code provided as an argument
- `WebAssembly.compile`
- `WebAssembly.instantiate` with [a buffer parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate#primary_overload_%E2%80%94_taking_wasm_binary_code)

In rare cases, your code could contain (or import) some dynamic code evaluation statements which _can not be reached at runtime_ and which can not be removed by treeshaking.
You can relax the check to allow specific files with your Middleware or Edge API Route exported configuration:

```javascript
export const config = {
runtime: 'experimental-edge', // for Edge API Routes only
unstable_allowDynamic: [
'/lib/utilities.js', // allows a single file
'/node_modules/function-bind/**', // use a glob to allow anything in the function-bind 3rd party module
],
}
```

`unstable_allowDynamic` is a [glob](https://github.com/micromatch/micromatch#matching-features), or an array of globs, ignoring dynamic code evaluation for specific files. The globs are relative to your application root folder.

Be warned that if these statements are executed on the Edge, _they will throw and cause a runtime error_.

## Related

Expand Down

0 comments on commit ff150dd

Please sign in to comment.