Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New docs #9301

Merged
merged 130 commits into from Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
aeb36cf
Added the docs from Notion
lfades Nov 2, 2019
065c6fc
Updated the links from notion to relative links
lfades Nov 4, 2019
9bbe8ce
Added a routes manifest to the docs
lfades Nov 4, 2019
464f324
Removed the <br> after examples toggle
lfades Nov 8, 2019
a867651
Use the name of the section instead of Introduction
lfades Nov 9, 2019
59f51aa
Merge branch 'canary' of https://github.com/zeit/next.js into new-docs
lfades Nov 12, 2019
a30affe
Fixed spelling errors
lfades Nov 12, 2019
8b81282
Merge branch 'canary' of https://github.com/zeit/next.js into new-docs
lfades Nov 21, 2019
df1baa6
Optimize the content for Algolia
lfades Nov 21, 2019
c458b7e
Add a paragraph for `pageProps`
lfades Nov 23, 2019
4c8fd45
Add welcome section
timneutkens Nov 23, 2019
e484699
Transpile -> Compile
timneutkens Nov 23, 2019
b1b97c3
Update getting-started.md
timneutkens Nov 23, 2019
660b825
Update getting-started.md
timneutkens Nov 23, 2019
cb3b9ea
Update getting-started.md
timneutkens Nov 23, 2019
ecb95e6
Update getting-started.md
timneutkens Nov 23, 2019
e823dc1
Update getting-started.md
timneutkens Nov 23, 2019
f3988db
Update getting-started.md
timneutkens Nov 23, 2019
1de4242
Update getting-started.md
timneutkens Nov 23, 2019
83e1309
Update getting-started.md
timneutkens Nov 23, 2019
a0694fc
Update getting-started.md
timneutkens Nov 23, 2019
f1d9a0a
Update getting-started.md
timneutkens Nov 23, 2019
1f9372b
Test extra room between
timneutkens Nov 23, 2019
9c895f2
Update getting-started.md
timneutkens Nov 23, 2019
bd0ae92
Update getting-started.md
timneutkens Nov 23, 2019
e5e78eb
Update getting-started.md
timneutkens Nov 23, 2019
3639fe4
Update getting-started.md
timneutkens Nov 23, 2019
77a4441
Update getting-started.md
timneutkens Nov 23, 2019
657ade0
Update manifest.json
timneutkens Nov 23, 2019
b676f85
Update getting-started.md
timneutkens Nov 23, 2019
64ffabd
Update getting-started.md
timneutkens Nov 23, 2019
00e6145
Add concepts section
timneutkens Nov 23, 2019
e6f6577
Update pages.md
timneutkens Nov 23, 2019
68be8a1
Update pages.md
timneutkens Nov 23, 2019
bc773a8
Add data fetching section
timneutkens Nov 23, 2019
f0c4ccc
Update pages.md
timneutkens Nov 24, 2019
44db89c
See how a card looks like
Nov 26, 2019
e715985
Undo card changes
Nov 26, 2019
7688fce
Added related section to getting-started
lfades Nov 27, 2019
87e4273
Merge branch 'new-docs' of https://github.com/lfades/next.js into new…
lfades Nov 27, 2019
ade1a35
Fixed wrong markdown syntax in the withRouter page
lfades Nov 27, 2019
adb0d6c
Moved the server-side-and-client-side section
lfades Nov 27, 2019
6bef154
Updated next-cli reference
lfades Nov 27, 2019
af03644
updated getInitialProps section
lfades Nov 27, 2019
2a01b53
Minor fixes
lfades Nov 27, 2019
d5dca9d
Added more Related sections
lfades Nov 27, 2019
49d6810
Add html to the related section in getting-started
lfades Nov 28, 2019
6951951
Use small for the card
lfades Nov 28, 2019
8c64ea6
Use cards for all related sections
lfades Nov 28, 2019
f078e1e
Added src directory docs
lfades Dec 2, 2019
e4dfbc2
Added src directory to the manifest
lfades Dec 2, 2019
a138eea
Add note about API routes in next export
lfades Dec 5, 2019
6868dd4
Add initial data fetching docs (private until new methods are released)
timneutkens Dec 9, 2019
8c53124
Merge branch 'new-docs' of https://github.com/lfades/next.js into pr/…
timneutkens Dec 9, 2019
cd473c6
Fix typos
timneutkens Dec 9, 2019
21bc81c
Improve wording
timneutkens Dec 9, 2019
dc266a6
Update getting-started.md
timneutkens Dec 9, 2019
1a76e25
Update getting-started.md
timneutkens Dec 9, 2019
9d257ea
Move advanced concepts to advanced section
timneutkens Dec 9, 2019
425279c
Hide server-side vs client-side
timneutkens Dec 9, 2019
0928dfa
Move AMP support
timneutkens Dec 9, 2019
d962450
Move typescript into one page
timneutkens Dec 10, 2019
bedf9eb
Add routing concepts page
timneutkens Dec 10, 2019
fa7143d
Remove introduction page
timneutkens Dec 10, 2019
6590800
Update section on different route types
timneutkens Dec 10, 2019
5a03ba7
Update routing.md
timneutkens Dec 10, 2019
1860bdd
Update routing.md
timneutkens Dec 10, 2019
57ee9f7
Update routing.md
timneutkens Dec 10, 2019
545b30b
Update routing.md
timneutkens Dec 10, 2019
5821731
Combine router injection pages
timneutkens Dec 10, 2019
87a91d4
Update pages.md
timneutkens Dec 10, 2019
080903c
Update routing.md
timneutkens Dec 10, 2019
5f914b9
Update using-link.md
timneutkens Dec 10, 2019
17ffc58
Update using-link.md
timneutkens Dec 10, 2019
4b87680
Update typescript.md
timneutkens Dec 10, 2019
2a6cbb7
Move the API Routes typescript to basic features
lfades Dec 10, 2019
1af2041
Added links to the typescript section
lfades Dec 11, 2019
1ea27e2
Updated links to useRouter and withRouter
lfades Dec 11, 2019
97b2db0
Add singleLevel prop to manifest
lfades Dec 11, 2019
bcd4a86
Added single page for router docs
lfades Dec 11, 2019
0c79a25
Updated description
lfades Dec 11, 2019
c0c0b6d
Updated the routes in the manifest
lfades Dec 11, 2019
c53bf8c
Add data fetching section
timneutkens Dec 12, 2019
91b61fe
Update data-fetching.md
timneutkens Dec 12, 2019
69ec227
Update data-fetching.md
timneutkens Dec 12, 2019
e897496
Update dynamic-routes.md
timneutkens Dec 12, 2019
e6c3c7d
Update manifest.json
timneutkens Dec 12, 2019
6f3fb60
Only use the single router API page
lfades Dec 12, 2019
22e23c2
Moved the concepts pages
lfades Dec 12, 2019
7dfbf3d
Updated links
lfades Dec 12, 2019
652e6a8
Removed extra space
lfades Dec 12, 2019
6ee8358
Updated title for Router API
lfades Dec 13, 2019
3b0d1b5
Added a description with frontmatter
lfades Dec 13, 2019
473c730
Add open prop to the manifest
lfades Dec 13, 2019
ebff333
Added datafetching section to API Reference
lfades Dec 16, 2019
ac11fd8
Updated links to the getInitialProps reference
lfades Dec 16, 2019
d802d50
Moved some sections to API
lfades Dec 16, 2019
ab882e1
Added next/head to API reference
lfades Dec 16, 2019
20ffbc7
Added next/link to the API Reference
lfades Dec 16, 2019
cf41bf5
Removed the populating-head section
lfades Dec 16, 2019
33d24d9
Updated links to the new next/link API
lfades Dec 16, 2019
28edb6a
Added link from dynamic-routes to next/link docs
lfades Dec 16, 2019
9b8d64a
use a paragraph
lfades Dec 16, 2019
31313ab
Added next/router API
lfades Dec 16, 2019
af82613
Added next/amp
lfades Dec 16, 2019
5f908d5
Updated the docs for next/amp
lfades Dec 16, 2019
5b4a035
Moved the AMP support folder
lfades Dec 16, 2019
add4588
Updated title
lfades Dec 16, 2019
d6a4f51
Content updates
lfades Dec 17, 2019
39f3d07
Added more links to the data fetching section
lfades Dec 17, 2019
f38fc3c
Added links from the API to introductions
lfades Dec 17, 2019
bd8de9f
changing the router API
lfades Dec 17, 2019
3947cac
Updates to the router API
lfades Dec 17, 2019
1abeec0
Updated the routing section
lfades Dec 17, 2019
ae1d8eb
life improvements
lfades Dec 17, 2019
2b35e1b
Added shallow routing section
lfades Dec 17, 2019
846ad3d
Small fix
lfades Dec 17, 2019
1b9b0bc
Removed old routing sections
lfades Dec 17, 2019
f2bb6ee
Updated link to shallow routing
lfades Dec 17, 2019
5de564e
Removed unrequired page
lfades Dec 17, 2019
2f9f15a
Removed /pages
lfades Dec 17, 2019
aadac15
Update data-fetching.md
timneutkens Dec 19, 2019
5dbf090
Add initial deployments section
timneutkens Dec 19, 2019
c4d9c4b
Update manifest.json
timneutkens Dec 19, 2019
47d875a
Update introduction.md
timneutkens Dec 20, 2019
4cb6be1
Update deployment doc
timneutkens Dec 20, 2019
7b07d79
Add static export section updates
timneutkens Dec 20, 2019
adf923a
link ssg/ssr
timneutkens Dec 20, 2019
cd0f0df
Update deployment.md
timneutkens Dec 20, 2019
bde9e58
Add syntax highlighting
timneutkens Dec 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/advanced-features/amp-support/adding-amp-components.md
@@ -0,0 +1,40 @@
# Adding AMP Components

The AMP community provide [many components](https://amp.dev/documentation/components/) to make AMP pages more interactive. You can add these components to your page by using `next/head`, as in the following example:

```jsx
import Head from 'next/head'

export const config = { amp: true }

function MyAmpPage() {
const date = new Date()

return (
<div>
<Head>
<script
async
key="amp-timeago"
custom-element="amp-timeago"
src="https://cdn.ampproject.org/v0/amp-timeago-0.1.js"
/>
</Head>

<p>Some time: {date.toJSON()}</p>
<amp-timeago
width="0"
height="15"
datetime={date.toJSON()}
layout="responsive"
>
.
</amp-timeago>
</div>
)
}

export default MyAmpPage
```

The above example uses the [`amp-timeago`](https://amp.dev/documentation/components/amp-timeago/?format=websites) component.
29 changes: 29 additions & 0 deletions docs/advanced-features/amp-support/amp-in-static-html-export.md
@@ -0,0 +1,29 @@
# AMP in Static HTML export

When using `next export` to do [Static HTML export](/docs/advanced-features/static-html-export.md) statically prerender pages, Next.js will detect if the page supports AMP and change the exporting behavior based on that.

For example, the hybrid AMP page `pages/about.js` would output:

- `out/about.html` - HTML page with client-side React runtime
- `out/about.amp.html` - AMP page

And if `pages/about.js` is an AMP-only page, then it would output:

- `out/about.html` - Optimized AMP page

Next.js will automatically insert a link to the AMP version of your page in the HTML version, so you don't have to, like so:

```jsx
<link rel="amphtml" href="/about.amp.html" />
```

And the AMP version of your page will include a link to the HTML page:

```jsx
<link rel="canonical" href="/about" />
```

When [`exportTrailingSlash`](/docs/api-reference/next.config.js/exportPathMap.md#0cf7d6666b394c5d8d08a16a933e86ea) is enabled the exported pages for `pages/about.js` would be:

- `out/about/index.html` - HTML page
- `out/about.amp/index.html` - AMP page
5 changes: 5 additions & 0 deletions docs/advanced-features/amp-support/amp-validation.md
@@ -0,0 +1,5 @@
# AMP Validation

AMP pages are automatically validated with [amphtml-validator](https://www.npmjs.com/package/amphtml-validator) during development. Errors and warnings will appear in the terminal where you started Next.js.

Pages are also validated during [Static HTML export](/docs/advanced-features/static-html-export.md) and any warnings / errors will be printed to the terminal. Any AMP errors will cause the export to exit with status code `1` because the export is not valid AMP.
36 changes: 36 additions & 0 deletions docs/advanced-features/amp-support/introduction.md
@@ -0,0 +1,36 @@
# AMP Support

<details>
<summary><b>Examples</b></summary>
<ul>
<li><a href="https://github.com/zeit/next.js/tree/canary/examples/amp">AMP</a></li>
</ul>
</details>

AMP means "_Accelerated Mobile Pages_", and it's, in short words: A faster HTML.

With Next.js you can turn any React page into an AMP page, with minimal config, and without leaving React.

You can read more about AMP in the official [amp.dev](https://amp.dev/) site.

## Enabling AMP

To enable AMP support for a page, and to learn more about the different AMP configs, read the [API documentation for `next/amp`](/docs/api-reference/next/amp.md).

## Related

For more information on what to do next, we recommend the following sections:

<div class="card">
<a href="/docs/advanced-features/amp-support/adding-amp-components.md">
<b>AMP Components:</b>
<small>Make your pages more interactive with AMP components.</small>
</a>
</div>

<div class="card">
<a href="/docs/advanced-features/amp-support/amp-validation.md">
<b>AMP Validation:</b>
<small>Learn about how Next.js validates AMP pages.</small>
</a>
</div>
5 changes: 5 additions & 0 deletions docs/advanced-features/amp-support/typescript.md
@@ -0,0 +1,5 @@
# TypeScript

AMP currently doesn't have built-in types for TypeScript, but it's in their roadmap ([#13791](https://github.com/ampproject/amphtml/issues/13791)).

As a workaround you can manually create a file called `amp.d.ts` inside your project and add the custom types described [here](https://stackoverflow.com/a/50601125).
34 changes: 34 additions & 0 deletions docs/advanced-features/automatic-static-optimization.md
@@ -0,0 +1,34 @@
# Automatic Static Optimization

Next.js automatically determines that a page is static (can be prerendered) if it has no blocking data requirements. This determination is made by the absence of `getInitialProps` in the page.

This feature allows Next.js to emit hybrid applications that contain **both server-rendered and statically generated pages**.

> Statically generated pages are still reactive: Next.js will hydrate your application client-side to give it full interactivity.

One of the main benefits this feature is that optimized pages require no server-side computation, and can be instantly streamed to the end-user from multiple CDN locations. The result is an _ultra fast_ loading experience for your users.

## How it works

If `getInitialProps` is present, Next.js will use its default behavior and render the page on-demand, per-request (meaning Server-Side Rendering).

If `getInitialProps` is absent, Next.js will **statically optimize** your page automatically by prerendering the page to static HTML. During prerendering, the router's `query` object will be empty since we do not have `query` information to provide during this phase. Any `query` values will be populated client-side after hydration.

`next build` will emit `.html` files for statically optimized pages. For example, the result for the page `pages/about.js` would be:

```bash
.next/server/static/${BUILD_ID}/about.html
```

And if you add `getInitialProps` to the page, it will then be JavaScript, like so:

```bash
.next/server/static/${BUILD_ID}/about.js
```

In development you'll know if `pages/about.js` is optimized or not thanks to the included [static optimization indicator](/docs/api-reference/next.config.js/static-optimization-indicator.md).

## Caveats

- If you have a [custom `App`](/docs/advanced-features/custom-app.md) with `getInitialProps` then this optimization will be disabled for all pages.
- If you have a [custom `Document`](/docs/advanced-features/custom-document.md) with `getInitialProps` be sure you check if `ctx.req` is defined before assuming the page is server-side rendered. `ctx.req` will be `undefined` for pages that are prerendered.
54 changes: 54 additions & 0 deletions docs/advanced-features/custom-app.md
@@ -0,0 +1,54 @@
# Custom `App`

Next.js uses the `App` component to initialize pages. You can override it and control the page initialization. Which allows you to do amazing things like:

- Persisting layout between page changes
- Keeping state when navigating pages
- Custom error handling using `componentDidCatch`
- Inject additional data into pages

To override the default `App`, create the file `./pages/_app.js` as shown below:

```jsx
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}

// Only uncomment this method if you have blocking data requirements for
// every single page in your application. This disables the ability to
// perform automatic static optimization, causing every page in your app to
// be server-side rendered.
//
// MyApp.getInitialProps = async (appContext) => {
// // calls page's `getInitialProps` and fills `appProps.pageProps`
// const appProps = await App.getInitialProps(appContext);
//
// return { ...appProps }
// }

export default MyApp
```

The `Component` prop is the active `page`, so whenever you navigate between routes, `Component` will change to the new `page`. Therefore, any props you send to `Component` will be received by the `page`.

`pageProps` is an object with the initial props that were preloaded for your page, it's an empty object if the page is not using [`getInitialProps`](/docs/api-reference/data-fetching/getInitialProps.md).

> Adding a custom `getInitialProps` in your `App` will disable [Automatic Static Optimization](/docs/advanced-features/automatic-static-optimization.md).

## Related

For more information on what to do next, we recommend the following sections:

<div class="card">
<a href="/docs/advanced-features/automatic-static-optimization.md">
<b>Automatic Static Optimization:</b>
<small>Learn more about how Next.js automatically optimizes your pages.</small>
</a>
</div>

<div class="card">
<a href="/docs/advanced-features/custom-error-page.md">
<b>Custom Error Page:</b>
<small>Learn more about the built-in Error page.</small>
</a>
</div>
3 changes: 3 additions & 0 deletions docs/advanced-features/custom-configuration.md
@@ -0,0 +1,3 @@
# Custom Configuration

Next.js can be enhanced with the usage of `next.config.js`. To learn more about it please refer to the [`next.config.js` documentation](/docs/api-reference/next.config.js/introduction.md).
76 changes: 76 additions & 0 deletions docs/advanced-features/custom-document.md
@@ -0,0 +1,76 @@
# Custom `Document`

A custom `Document` is commonly used to augment your application's `<html>` and `<body>` tags. This is necessary because Next.js pages skip the definition of the surrounding document's markup.

A custom `Document` can also include `getInitialProps` for expressing asynchronous server-rendering data requirements.

To override the default `Document`, create the file `./pages/_document.js` and extend the `Document` class as shown below:

```jsx
import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}

render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}

export default MyDocument
```

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

The `ctx` object is equivalent to the one received in [`getInitialProps`](/docs/api-reference/data-fetching/getInitialProps.md#context-object), with one addition:

- `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)

## Caveats

- `Document` is only rendered in the server, event handlers like `onClick` won't work
- React components outside of `<Main />` will not be initialized by the browser. Do _not_ add application logic here. If you need shared components in all your pages (like a menu or a toolbar), take a look at the [`App`](/docs/advanced-features/custom-app.md) component instead
- `Document`'s `getInitialProps` function is not called during client-side transitions, nor when a page is [statically optimized](/docs/advanced-features/automatic-static-optimization.md)
- Make sure to check if `ctx.req` / `ctx.res` are defined in `getInitialProps`. Those variables will be `undefined` when a page is being statically exported by [Automatic Static Optimization](/docs/advanced-features/automatic-static-optimization.md) or by [`next export`](/docs/advanced-features/static-html-export.md)

## Customizing `renderPage`

> It should be noted that the only reason you should be customizing `renderPage` is for usage with **css-in-js** libraries that need to wrap the application to properly work with server-side rendering.

It takes as argument an options object for further customization:

```jsx
import Document from 'next/document'

class MyDocument extends Document {
static async getInitialProps(ctx) {
const originalRenderPage = ctx.renderPage

ctx.renderPage = () =>
originalRenderPage({
// useful for wrapping the whole react tree
enhanceApp: App => App,
// useful for wrapping in a per-page basis
enhanceComponent: Component => Component,
})

// Run the parent `getInitialProps`, it now includes the custom `renderPage`
const initialProps = await Document.getInitialProps(ctx)

return initialProps
}
}

export default MyDocument
```
53 changes: 53 additions & 0 deletions docs/advanced-features/custom-error-page.md
@@ -0,0 +1,53 @@
# Custom Error Page

**404** or **500** errors are handled both client-side and server-side by the `Error` component. If you wish to override it, define the file `pages/_error.js` and add the following code:

```jsx
function Error({ statusCode }) {
return (
<p>
{statusCode
? `An error ${statusCode} occurred on server`
: 'An error occurred on client'}
</p>
)
}

Error.getInitialProps = ({ res, err }) => {
const statusCode = res ? res.statusCode : err ? err.statusCode : 404
return { statusCode }
}

export default Error
```

> `pages/_error.js` is only used in production. In development you'll get an error with the call stack to know where the error originated from.

## Reusing the built-in error page

If you want to render the built-in error page you can by importing the `Error` component:

```jsx
import Error from 'next/error'
import fetch from 'isomorphic-unfetch'

const Page = ({ errorCode, stars }) => {
if (errorCode) {
return <Error statusCode={errorCode} />
}

return <div>Next stars: {stars}</div>
}

Page.getInitialProps = async () => {
const res = await fetch('https://api.github.com/repos/zeit/next.js')
const errorCode = res.statusCode > 200 ? res.statusCode : false
const json = await res.json()

return { errorCode, stars: json.stargazers_count }
}

export default Page
```

The `Error` component also takes `title` as a property if you want to pass in a text message along with a `statusCode`.