Skip to content

Commit

Permalink
Merge branch 'canary' into swc-up-2
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed May 19, 2022
2 parents 2795f9a + 5b21f09 commit 3f0e4e3
Show file tree
Hide file tree
Showing 201 changed files with 3,487 additions and 2,872 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-features/i18n-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ module.exports = {
Next, we can use [Middleware](/docs/middleware.md) to add custom routing rules:

```js
// pages/_middleware.ts
// middleware.ts

import { NextRequest, NextResponse } from 'next/server'

Expand Down
33 changes: 4 additions & 29 deletions docs/advanced-features/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ Middleware enables you to use code over configuration. This gives you full flexi
npm install next@latest
```

2. Then, create a `_middleware.ts` file under your `/pages` directory.
2. Then, create a `middleware.ts` file under your project root directory.

3. Finally, export a middleware function from the `_middleware.ts` file.
3. Finally, export a middleware function from the `middleware.ts` file.

```jsx
// pages/_middleware.ts
// middleware.ts

import type { NextFetchEvent, NextRequest } from 'next/server'

Expand All @@ -42,7 +42,7 @@ In this example, we use the standard Web API Response ([MDN](https://developer.m

## API

Middleware is created by using a `middleware` function that lives inside a `_middleware` file. Its API is based upon the native [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), and [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) objects.
Middleware is created by using a `middleware` function that lives inside a `middleware` file. Its API is based upon the native [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), and [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) objects.

These native Web API objects are extended to give you more control over how you manipulate and configure a response, based on the incoming requests.

Expand Down Expand Up @@ -75,31 +75,6 @@ Middleware can be used for anything that shares logic for a set of pages, includ
## Execution Order
If your Middleware is created in `/pages/_middleware.ts`, it will run on all routes within the `/pages` directory. The below example assumes you have `about.tsx` and `teams.tsx` routes.
```bash
- package.json
- /pages
_middleware.ts # Will run on all routes under /pages
index.tsx
about.tsx
teams.tsx
```
If you _do_ have sub-directories with nested routes, Middleware will run from the top down. For example, if you have `/pages/about/_middleware.ts` and `/pages/about/team/_middleware.ts`, `/about` will run first and then `/about/team`. The below example shows how this works with a nested routing structure.
```bash
- package.json
- /pages
index.tsx
- /about
_middleware.ts # Will run first
about.tsx
- /teams
_middleware.ts # Will run second
teams.tsx
```
Middleware runs directly after `redirects` and `headers`, before the first filesystem lookup. This excludes `/_next` files.
## Deployment
Expand Down
4 changes: 2 additions & 2 deletions docs/api-reference/next.config.js/custom-page-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {

> **Note**: The default value of `pageExtensions` is [`['tsx', 'ts', 'jsx', 'js']`](https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161).
> **Note**: configuring `pageExtensions` also affects `_document.js`, `_app.js`, `_middleware.js` as well as files under `pages/api/`. For example, setting `pageExtensions: ['page.tsx', 'page.ts']` means the following files: `_document.tsx`, `_app.tsx`, `_middleware.ts`, `pages/users.tsx` and `pages/api/users.ts` will have to be renamed to `_document.page.tsx`, `_app.page.tsx`, `_middleware.page.ts`, `pages/users.page.tsx` and `pages/api/users.page.ts` respectively.
> **Note**: configuring `pageExtensions` also affects `_document.js`, `_app.js`, `middleware.js` as well as files under `pages/api/`. For example, setting `pageExtensions: ['page.tsx', 'page.ts']` means the following files: `_document.tsx`, `_app.tsx`, `middleware.ts`, `pages/users.tsx` and `pages/api/users.ts` will have to be renamed to `_document.page.tsx`, `_app.page.tsx`, `middleware.page.ts`, `pages/users.page.tsx` and `pages/api/users.page.ts` respectively.
## Including non-page files in the `pages` directory

Expand All @@ -32,7 +32,7 @@ module.exports = {

Then rename your pages to have a file extension that includes `.page` (ex. rename `MyPage.tsx` to `MyPage.page.tsx`).

> **Note**: Make sure you also rename `_document.js`, `_app.js`, `_middleware.js`, as well as files under `pages/api/`.
> **Note**: Make sure you also rename `_document.js`, `_app.js`, `middleware.js`, as well as files under `pages/api/`.
Without this config, Next.js assumes every tsx/ts/jsx/js file in the `pages` directory is a page or API route, and may expose unintended routes vulnerable to denial of service attacks, or throw an error like the following when building the production bundle:

Expand Down
14 changes: 7 additions & 7 deletions docs/api-reference/next/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ The `<Image />` component accepts a number of additional properties beyond those

The layout behavior of the image as the viewport changes size.

| `layout` | Behavior | `srcSet` | `sizes` | Has wrapper and sizer |
| ---------------------------------------- | ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | -------- | --------------------- |
| `intrinsic` (default) | Scale *down* to fit width of container, up to image size | `1x``2x` (based on [imageSizes](#image-sizes)) | N/A | yes |
| `fixed` | Sized to `width` and `height` exactly | `1x``2x` (based on [imageSizes](#image-sizes)) | N/A | yes |
| `responsive` | Scale to fit width of container | `640w``750w`, ... `2048w``3840w` (based on [imageSizes](#image-sizes) and [deviceSizes](#device-sizes)) | `100vw` | yes |
| `fill` | Grow in both X and Y axes to fill container | `640w``750w`, ... `2048w``3840w` (based on [imageSizes](#image-sizes) and [deviceSizes](#device-sizes)) | `100vw` | yes |
| `raw`[\*](#experimental-raw-layout-mode) | Insert the image element with no automatic layout behavior | Behaves like `responsive` if the image has the `sizes` prop, and like `fixed` if it does not | optional | no |
| `layout` | Behavior | `srcSet` | `sizes` | Has wrapper and sizer |
| ---------------------------------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | -------- | --------------------- |
| `intrinsic` (default) | Scale *down* to fit width of container, up to image size | `1x``2x` (based on [imageSizes](#image-sizes)) | N/A | yes |
| `fixed` | Sized to `width` and `height` exactly | `1x``2x` (based on [imageSizes](#image-sizes)) | N/A | yes |
| `responsive` | Scale to fit width of container | `640w``750w`, ... `2048w``3840w` (based on [imageSizes](#image-sizes) and [deviceSizes](#device-sizes)) | `100vw` | yes |
| `fill` | Grow in both X and Y axes to fill container | `640w``750w`, ... `2048w``3840w` (based on [imageSizes](#image-sizes) and [deviceSizes](#device-sizes)) | `100vw` | yes |
| `raw`[\*](#experimental-raw-layout-mode) | Raw `<img>` without styles and native lazy loading | Behaves like `responsive` if the image has the `sizes` prop, and like `fixed` if it does not | optional | no |

- [Demo the `intrinsic` layout (default)](https://image-component.nextjs.gallery/layout-intrinsic)
- When `intrinsic`, the image will scale the dimensions down for smaller viewports, but maintain the original dimensions for larger viewports.
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/next/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The `next/server` module provides several exports for server-only helpers, such

## NextMiddleware

Middleware is created by using a `middleware` function that lives inside a `_middleware` file. The Middleware API is based upon the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request), [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects.
Middleware is created by using a `middleware` function that lives inside a `middleware` file. The Middleware API is based upon the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request), [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects.

These native Web API objects are extended to give you more control over how you manipulate and configure a response, based on the incoming requests.

Expand Down
4 changes: 4 additions & 0 deletions errors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@
"title": "middleware-relative-urls",
"path": "/errors/middleware-relative-urls.md"
},
{
"title": "nested-middleware",
"path": "/errors/nested-middleware.md"
},
{
"title": "deleting-query-params-in-middlewares",
"path": "/errors/deleting-query-params-in-middlewares.md"
Expand Down
4 changes: 2 additions & 2 deletions errors/middleware-new-signature.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Your application is using a Middleware function that is using parameters from the deprecated API.

```typescript
// _middleware.js
// middleware.js
import { NextResponse } from 'next/server'

export function middleware(event) {
Expand All @@ -24,7 +24,7 @@ export function middleware(event) {
Update to use the new API for Middleware:

```typescript
// _middleware.js
// middleware.js
import { NextResponse } from 'next/server'

export function middleware(request) {
Expand Down
26 changes: 26 additions & 0 deletions errors/nested-middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Nested Middleware

#### Why This Error Occurred

You are defining a middleware file in a location different from `<root>/middleware` which is not allowed.

While in beta, a middleware file under specific pages implied that it would _only_ be executed when pages below its declaration were matched.
This execution model allowed the nesting of multiple middleware, which is hard to reason about and led to consequences such as dragging effects between different middleware executions.

The API has been removed in favor of a simpler model with a single root middleware.

#### Possible Ways to Fix It

To fix this error, declare your middleware in the root folder and use `NextRequest` parsed URL to define which path the middleware code should be executed for. For example, a middleware declared under `pages/about/_middleware.js` can be moved to `middleware`. A conditional can be used to ensure the middleware executes only when it matches the `about/*` path:

```typescript
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/about')) {
// Execute pages/about/_middleware.js
}
}
```

If you have more than one middleware, you will need to combine them into a single file and model their execution depending on the request.
6 changes: 3 additions & 3 deletions errors/no-server-import-in-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

### Why This Error Occurred

`next/server` was imported outside of `pages/**/_middleware.{js,ts}`.
`next/server` was imported outside of `middleware.{js,ts}`.

### Possible Ways to Fix It

Only import and use `next/server` in a file located within the pages directory: `pages/**/_middleware.{js,ts}`.
Only import and use `next/server` in a file located within the project root directory: `middleware.{js,ts}`.

```ts
// pages/_middleware.ts
// middleware.ts

import type { NextFetchEvent, NextRequest } from 'next/server'

Expand Down
32 changes: 32 additions & 0 deletions examples/markdoc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
*.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
23 changes: 23 additions & 0 deletions examples/markdoc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Example app with Markdoc

This example shows using [Markdoc](https://github.com/markdoc/markdoc) as top level pages for your Next.js app.

## Deploy your own

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/markdoc)

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/markdoc&project-name=markdoc&repository-name=markdoc)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example markdoc markdoc-app
# or
yarn create next-app --example markdoc markdoc-app
# or
pnpm create next-app -- --example markdoc markdoc-app
```

Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
2 changes: 2 additions & 0 deletions examples/markdoc/markdoc/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Use this file to export your Markdoc functions */
export default {}
2 changes: 2 additions & 0 deletions examples/markdoc/markdoc/nodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Use this file to export your Markdoc nodes */
export default {}
2 changes: 2 additions & 0 deletions examples/markdoc/markdoc/tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Use this file to export your Markdoc tags */
export default {}
5 changes: 5 additions & 0 deletions examples/markdoc/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
6 changes: 6 additions & 0 deletions examples/markdoc/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const withMarkdoc = require('@markdoc/next.js')

module.exports =
withMarkdoc(/* config: https://markdoc.io/docs/nextjs#options */)({
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdoc'],
})
21 changes: 21 additions & 0 deletions examples/markdoc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@markdoc/markdoc": "latest",
"@markdoc/next.js": "latest",
"next": "latest",
"react": "latest",
"react-dom": "latest"
},
"devDependencies": {
"@types/node": "latest",
"@types/react": "latest",
"@types/react-dom": "latest",
"typescript": "latest"
}
}
7 changes: 7 additions & 0 deletions examples/markdoc/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { AppProps } from 'next/app'

import '../public/globals.css'

export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
18 changes: 18 additions & 0 deletions examples/markdoc/pages/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Get started with Markdoc
description: How to get started with Markdoc
---

# Markdoc

This website is a lightweight boilerplate to spin up a documentation website with Markdoc.

## Quick start

If you want to get started right away with this boilerplate, either clone the [GitHub repository](https://github.com/markdoc/docs/tree/main/examples/simple-nextjs) or deploy a version of this site to Vercel by clicking the button below.

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/markdoc/docs/tree/main/examples/simple-nextjs)

## Get started from scratch

If you'd prefer to start from scratch, feel free to check out the [official repository](https://github.com/markdoc/markdoc) and [documentation site](https://markdoc.io/docs/getting-started).
Binary file added examples/markdoc/public/favicon.ico
Binary file not shown.
38 changes: 38 additions & 0 deletions examples/markdoc/public/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
body {
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif,
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
color: rgba(60, 66, 87, 1);
margin: 0;
}

#__next {
display: grid;
grid-template-columns: 0.1fr 1fr 0.15fr;
grid-template-rows: 1fr 0.05fr;
height: 100vh;
}

article {
grid-column: 2 / 3;
grid-row: 1;
font-size: 16px;
padding: 4em 2em;
}

h1 {
font-size: 40px;
}

a {
color: rgba(75, 85, 99, 1);
}

h2 {
margin: 2em 0;
}

p {
line-height: 1.5em;
font-weight: 300;
}
20 changes: 20 additions & 0 deletions examples/markdoc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "12.1.7-canary.7"
"version": "12.1.7-canary.9"
}
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "12.1.7-canary.7",
"version": "12.1.7-canary.9",
"keywords": [
"react",
"next",
Expand Down
Loading

0 comments on commit 3f0e4e3

Please sign in to comment.