-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into i18n/api-rewrite
- Loading branch information
Showing
93 changed files
with
1,767 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
docs/api-reference/next.config.js/static-optimization-indicator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
description: Optimized pages include an indicator to let you know if it's being statically optimized. You can opt-out of it here. | ||
--- | ||
|
||
# Static Optimization Indicator | ||
|
||
> **Note:** This indicator was removed in Next.js version 10.0.1. We recommend upgrading to the latest version of Next.js. | ||
When a page qualifies for [Automatic Static Optimization](/docs/advanced-features/automatic-static-optimization.md) we show an indicator to let you know. | ||
|
||
This is helpful since automatic static optimization can be very beneficial and knowing immediately in development if the page qualifies can be useful. | ||
|
||
In some cases this indicator might not be useful, like when working on electron applications. To remove it open `next.config.js` and disable the `autoPrerender` config in `devIndicators`: | ||
|
||
```js | ||
module.exports = { | ||
devIndicators: { | ||
autoPrerender: false, | ||
}, | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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 | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# vercel | ||
.vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# API Routes Rate Limiting Example | ||
|
||
This example uses `lru-cache` to implement a simple rate limiter for API routes ([Serverless Functions](https://vercel.com/docs/serverless-functions/introduction)). | ||
|
||
**Demo: https://nextjs-rate-limit.vercel.app/** | ||
|
||
```bash | ||
curl http://localhost:3000/api/user -I | ||
HTTP/1.1 200 OK | ||
X-RateLimit-Limit: 10 | ||
X-RateLimit-Remaining: 9 | ||
|
||
curl http://localhost:3000/api/user -I | ||
HTTP/1.1 429 Too Many Requests | ||
X-RateLimit-Limit: 10 | ||
X-RateLimit-Remaining: 0 | ||
``` | ||
|
||
## Deploy your own | ||
|
||
Deploy the example using [Vercel](https://vercel.com): | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/api-routes-rate-limit) | ||
|
||
## 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 api-routes api-routes-rate-limit | ||
# or | ||
yarn create next-app --example api-routes api-routes-rate-limit | ||
``` | ||
|
||
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "nextjs-rate-limit", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"lru-cache": "^6.0.0", | ||
"next": "10.0.3", | ||
"react": "17.0.1", | ||
"react-dom": "17.0.1", | ||
"uuid": "^8.3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as uuid from 'uuid' | ||
import rateLimit from '../../utils/rate-limit' | ||
|
||
const limiter = rateLimit({ | ||
interval: 60 * 1000, // 60 seconds | ||
uniqueTokenPerInterval: 500, // Max 500 users per second | ||
}) | ||
|
||
export default async function handler(req, res) { | ||
try { | ||
await limiter.check(res, 10, 'CACHE_TOKEN') // 10 requests per minute | ||
res.status(200).json({ id: uuid.v4() }) | ||
} catch { | ||
res.status(429).json({ error: 'Rate limit exceeded' }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useState } from 'react' | ||
import styles from '../styles.module.css' | ||
|
||
export default function Index() { | ||
const [response, setResponse] = useState() | ||
|
||
const makeRequest = async () => { | ||
const res = await fetch('/api/user') | ||
|
||
setResponse({ | ||
status: res.status, | ||
body: await res.json(), | ||
limit: res.headers.get('X-RateLimit-Limit'), | ||
remaining: res.headers.get('X-RateLimit-Remaining'), | ||
}) | ||
} | ||
|
||
return ( | ||
<main className={styles.container}> | ||
<h1>Next.js API Routes Rate Limiting</h1> | ||
<p> | ||
This example uses <code className={styles.inlineCode}>lru-cache</code>{' '} | ||
to implement a simple rate limiter for API routes (Serverless | ||
Functions). | ||
</p> | ||
<button onClick={() => makeRequest()}>Make Request</button> | ||
<code className={styles.code}> | ||
<div> | ||
<b>Status Code: </b> | ||
{response?.status || 'None'} | ||
</div> | ||
<div> | ||
<b>Request Limit: </b> | ||
{response?.limit || 'None'} | ||
</div> | ||
<div> | ||
<b>Remaining Requests: </b> | ||
{response?.remaining || 'None'} | ||
</div> | ||
<div> | ||
<b>Body: </b> | ||
{JSON.stringify(response?.body) || 'None'} | ||
</div> | ||
</code> | ||
<div className={styles.links}> | ||
<a href="#">View Source</a> | ||
{' | '} | ||
<a href="#">Deploy You Own ▲</a> | ||
</div> | ||
</main> | ||
) | ||
} |
Oops, something went wrong.