Skip to content

Commit c5eb535

Browse files
Luis Alvarez DTimerchibicode
authored
[Docs] Apply updates based on feedback (vercel#10352)
* Expand on the docs for the serverless target * Added catch all routes to API docs * Added caveat for catch all routes * Added link to Rauch's post about serverless * Add mention of `lang` * Add create-next-app to docs * Updated dynamic import examples to be more descriptive * Even better * Clarify valid catch all routes * Removed unexpected word * Apply suggestions from Joe Co-Authored-By: Joe Haddad <joe.haddad@zeit.co> * Keep #setup Co-Authored-By: Joe Haddad <joe.haddad@zeit.co> * Updated docs for the serverless target * Apply suggestions from code review Co-Authored-By: Shu Uesugi <shu@chibicode.com> * Update docs/getting-started.md Co-Authored-By: Shu Uesugi <shu@chibicode.com> * Added suggestion from chibicode Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Shu Uesugi <shu@chibicode.com>
1 parent 04d5bbc commit c5eb535

File tree

7 files changed

+84
-9
lines changed

7 files changed

+84
-9
lines changed

docs/advanced-features/custom-document.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export default MyDocument
3737

3838
`<Html>`, `<Head />`, `<Main />` and `<NextScript />` are required for the page to be properly rendered.
3939

40+
Custom attributes are allowed as props, like `lang`:
41+
42+
```jsx
43+
<Html lang="en">
44+
```
45+
4046
The `ctx` object is equivalent to the one received in [`getInitialProps`](/docs/api-reference/data-fetching/getInitialProps.md#context-object), with one addition:
4147

4248
- `renderPage`: `Function` - a callback that executes the actual React rendering logic (synchronously). It's useful to decorate this function in order to support server-rendering wrappers like Aphrodite's [`renderStatic`](https://github.com/Khan/aphrodite#server-side-rendering)

docs/advanced-features/dynamic-import.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ You can think of dynamic imports as another way to split your code into manageab
1717

1818
## Basic usage
1919

20+
In the following example, the module `../components/hello` will be dynamically loaded by the page:
21+
2022
```jsx
2123
import dynamic from 'next/dynamic'
2224

@@ -35,14 +37,21 @@ function Home() {
3537
export default Home
3638
```
3739

40+
`DynamicComponent` will be the default component returned by `../components/hello`. It works like a regular React Component, and you can pass props to it as you normally would.
41+
3842
## With named exports
3943

44+
If the dynamic component is not the default export, you can use a named export too. Consider the module `../components/hello.js` which has a named export `Hello`:
45+
4046
```jsx
41-
// components/hello.js
4247
export function Hello() {
4348
return <p>Hello!</p>
4449
}
50+
```
4551

52+
To dynamically import the `Hello` component, you can return it from the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) returned by [`import()`](https://github.com/tc39/proposal-dynamic-import#example), like so:
53+
54+
```jsx
4655
import dynamic from 'next/dynamic'
4756

4857
const DynamicComponent = dynamic(() =>
@@ -64,6 +73,8 @@ export default Home
6473

6574
## With custom loading component
6675

76+
An optional `loading` component can be added to render a loading state while the dynamic component is being loaded. For example:
77+
6778
```jsx
6879
import dynamic from 'next/dynamic'
6980

docs/api-reference/next.config.js/build-target.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ Next.js supports various build targets, each changing the way your application i
88

99
## `server` target
1010

11-
> This is the default target, however, we highly recommend the [`serverless` target](#serverless-target). The `serverless` target enforces additional constraints to keep you in the [Pit of Success](https://blog.codinghorror.com/falling-into-the-pit-of-success/).
11+
> This is the default target, however, we highly recommend the [`serverless` target](#serverless-target). The `serverless` target enforces [additional constraints](https://rauchg.com/2020/2019-in-review#serverless-upgrades-itself) to keep you in the [Pit of Success](https://blog.codinghorror.com/falling-into-the-pit-of-success/).
1212
1313
This target is compatible with both `next start` and [custom server](/docs/advanced-features/custom-server.md) setups (it's mandatory for a custom server).
1414

1515
Your application will be built and deployed as a monolith. This is the default target and no action is required on your part to opt-in.
1616

1717
## `serverless` target
1818

19-
> Deployments to [ZEIT Now](https://zeit.co/home) will automatically enable this target. You do not need to opt-into it yourself, but you can.
19+
> Deployments to [ZEIT Now](https://zeit.co) will automatically enable this target. You do not need to opt-into it yourself, but you can.
2020
21-
This target will output each page in a self-contained Serverless Function. It's only compatible with `next start` or Serverless deployment platforms (like ZEIT Now) — you cannot use the custom server API.
21+
This target will output independent pages that don't require a monolithic server.
22+
23+
It's only compatible with `next start` or Serverless deployment platforms (like [ZEIT Now](https://zeit.co)) — you cannot use the custom server API.
2224

2325
To opt-into this target, set the following configuration in your `next.config.js`:
2426

docs/api-reference/next/link.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function NavLink({ href, name }) {
9999
export default NavLink
100100
```
101101

102-
> **Note:** If you’re using [emotion](https://emotion.sh/)’s JSX pragma feature (`@jsx jsx`), you must use `passHref` even if you use an `<a>` tag directly.
102+
> **Note**: If you’re using [emotion](https://emotion.sh/)’s JSX pragma feature (`@jsx jsx`), you must use `passHref` even if you use an `<a>` tag directly.
103103
104104
## If the child is a function component
105105

docs/api-routes/dynamic-api-routes.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,44 @@ export default (req, res) => {
1919
```
2020

2121
Now, a request to `/api/post/abc` will respond with the text: `Post: abc`.
22+
23+
### Catch all API routes
24+
25+
API Routes can be extended to catch all paths by adding three dots (`...`) inside the brackets. For example:
26+
27+
- `pages/api/post/[...slug].js` matches `/api/post/a`, but also `/api/post/a/b`, `/api/post/a/b/c` and so on.
28+
29+
> **Note**: You can use names other than `slug`, such as: `[...param]`
30+
31+
Matched parameters will be sent as a query parameter (`slug` in the example) to the page, and it will always be an array, so, the path `/api/post/a` will have the following `query` object:
32+
33+
```json
34+
{ "slug": ["a"] }
35+
```
36+
37+
And in the case of `/api/post/a/b`, and any other matching path, new parameters will be added to the array, like so:
38+
39+
```json
40+
{ "slug": ["a", "b"] }
41+
```
42+
43+
An API route for `pages/api/post/[...slug].js` could look like this:
44+
45+
```js
46+
export default (req, res) => {
47+
const {
48+
query: { slug },
49+
} = req
50+
51+
res.end(`Post: ${slug.join(', ')}`)
52+
}
53+
```
54+
55+
Now, a request to `/api/post/a/b/c` will respond with the text: `Post: a, b, c`.
56+
57+
## Caveats
58+
59+
- Predefined API routes take precedence over dynamic API routes, and dynamic API routes over catch all API routes. Take a look at the following examples:
60+
- `pages/api/post/create.js` - Will match `/api/post/create`
61+
- `pages/api/post/[pid].js` - Will match `/api/post/1`, `/api/post/abc`, etc. But not `/api/post/create`
62+
- `pages/api/post/[...slug].js` - Will match `/api/post/1/2`, `/api/post/a/b/c`, etc. But not `/api/post/create`, `/api/post/abc`

docs/getting-started.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ The interactive course with quizzes will guide you through everything you need t
1717

1818
## Setup
1919

20+
We recommend creating a new Next.js app using `create-next-app`, which sets up everything automatically for you. To create a project, run:
21+
22+
```bash
23+
npm init next-app
24+
# or
25+
yarn create next-app
26+
```
27+
28+
After the installation is complete, follow the instructions to start the development server. Try editing `pages/index.js` and see the result on your browser.
29+
30+
## Manual Setup
31+
2032
Install `next`, `react` and `react-dom` in your project:
2133

2234
```bash

docs/routing/dynamic-routes.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ Client-side navigations to a dynamic route can be handled with [`next/link`](/do
6767

6868
Dynamic routes can be extended to catch all paths by adding three dots (`...`) inside the brackets. For example:
6969

70-
- `pages/post/[...slug].js` matches `/post/a`, but also `post/a/b`, `post/a/b/c` and so on.
70+
- `pages/post/[...slug].js` matches `/post/a`, but also `/post/a/b`, `/post/a/b/c` and so on.
71+
72+
> **Note**: You can use names other than `slug`, such as: `[...param]`
7173
7274
Matched parameters will be sent as a query parameter (`slug` in the example) to the page, and it will always be an array, so, the path `/post/a` will have the following `query` object:
7375

7476
```json
7577
{ "slug": ["a"] }
7678
```
7779

78-
And in the case of `post/a/b`, and any other matching path, new parameters will be added to the array, like so:
80+
And in the case of `/post/a/b`, and any other matching path, new parameters will be added to the array, like so:
7981

8082
```json
8183
{ "slug": ["a", "b"] }
@@ -85,9 +87,10 @@ And in the case of `post/a/b`, and any other matching path, new parameters will
8587
8688
## Caveats
8789

88-
- Predefined routes take precedence over dynamic routes. Take a look at the following examples:
90+
- Predefined routes take precedence over dynamic routes, and dynamic routes over catch all routes. Take a look at the following examples:
8991
- `pages/post/create.js` - Will match `/post/create`
90-
- `pages/post/[pid].js` - Will match `/post/1`, `/post/abc`, etc. but not `/post/create`
92+
- `pages/post/[pid].js` - Will match `/post/1`, `/post/abc`, etc. But not `/post/create`
93+
- `pages/post/[...slug].js` - Will match `/post/1/2`, `/post/a/b/c`, etc. But not `/post/create`, `/post/abc`
9194
- Pages that are statically optimized by [Automatic Static Optimization](/docs/advanced-features/automatic-static-optimization.md) will be hydrated without their route parameters provided, i.e `query` will be an empty object (`{}`).
9295

9396
After hydration, Next.js will trigger an update to your application to provide the route parameters in the `query` object.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy