Skip to content

Commit

Permalink
Merge branch 'script-examples' of github.com:vercel/next.js into scri…
Browse files Browse the repository at this point in the history
…pt-examples
  • Loading branch information
goncy committed Nov 15, 2021
2 parents 16d1cc6 + 3adea74 commit ce35189
Show file tree
Hide file tree
Showing 132 changed files with 3,887 additions and 472 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ packages/next-codemod/**/*.d.ts
packages/next-env/**/*.d.ts
packages/create-next-app/templates/**
test/integration/eslint/**
test/development/basic/legacy-decorators/**/*
test-timings.json
packages/next/build/swc/tests/**
bench/nested-deps/pages/**
Expand Down
1 change: 1 addition & 0 deletions .github/.kodiak.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ notify_on_conflict = false
[merge.message]
title = "pull_request_title"
body = "pull_request_body"
include_coauthors= true
include_pr_number = true
body_type = "markdown"
strip_html_comments = true
19 changes: 18 additions & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,24 @@ There are two options to develop with your local version of the codebase:
yarn install --force
```

or
#### Troubleshooting

- If you see the below error while running `yarn dev` with next:

```
Failed to load SWC binary, see more info here: https://nextjs.org/docs/messages/failed-loading-swc
```

Try to add the below section to your `package.json`, then run again

```json
"optionalDependencies": {
"@next/swc-linux-x64-gnu": "canary",
"@next/swc-win32-x64-msvc": "canary",
"@next/swc-darwin-x64": "canary",
"@next/swc-darwin-arm64": "canary"
},
```

### Develop inside the monorepo

Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/data-fetching/getInitialProps.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ 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 about how Nextjs automatically optimizes your pages.</small>
<small>Learn about how Next.js automatically optimizes your pages.</small>
</a>
</div>
2 changes: 1 addition & 1 deletion docs/api-reference/next.config.js/headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ module.exports = {
headers: [
{
key: 'x-hello',
value: 'worlld',
value: 'world',
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/next/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ If you are using `layout="fill"` or `layout="responsive"`, it's important to ass

For example, when the parent element will constrain the image to always be less than half the viewport width, use `sizes="50vw"`. Without `sizes`, the image will be sent at twice the necessary resolution, decreasing performance.

If you are using `layout="intrinsic"` or `layout="fixed"`, then `sizes` is not needed because the upperbound width is constrained already.
If you are using `layout="intrinsic"` or `layout="fixed"`, then `sizes` is not needed because the upper bound width is constrained already.

[Learn more](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes).

Expand Down
2 changes: 1 addition & 1 deletion docs/api-routes/response-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type ResponseData {
message: string
}

export default function handler(req: NextApiRequest, res: NextApiResponse<ResponseData>) => {
export default function handler(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
```
Expand Down
9 changes: 3 additions & 6 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const Profile = () => {
export default Profile
```

You can view this [example in action](https://next-with-iron-session.vercel.app/). Check out the [`with-iron-session`](https://github.com/vercel/next.js/tree/canary/examples/with-iron-session) example to see how it works.
You can view this [example in action](https://iron-session-example.vercel.app/). Check out the [`with-iron-session`](https://github.com/vercel/next.js/tree/canary/examples/with-iron-session) example to see how it works.

### Authenticating Server-Rendered Pages

Expand All @@ -71,10 +71,7 @@ import withSession from '../lib/session'
import Layout from '../components/Layout'

export const getServerSideProps = withSession(async function ({ req, res }) {
// Get the user's session based on the request
const user = req.session.get('user')

if (!user) {
if (!req.session.user) {
return {
redirect: {
destination: '/login',
Expand Down Expand Up @@ -119,7 +116,7 @@ Now that we've discussed authentication patterns, let's look at specific provide

If you have an existing database with user data, you'll likely want to utilize an open-source solution that's provider agnostic.

- If you want a low-level, encrypted, and stateless session utility use [`next-iron-session`](https://github.com/vercel/next.js/tree/canary/examples/with-iron-session).
- If you want a low-level, encrypted, and stateless session utility use [`iron-session`](https://github.com/vercel/next.js/tree/canary/examples/with-iron-session).
- If you want a full-featured authentication system with built-in providers (Google, Facebook, GitHub…), JWT, JWE, email/password, magic links and more… use [`next-auth`](https://github.com/nextauthjs/next-auth-example).

Both of these libraries support either authentication pattern. If you're interested in [Passport](http://www.passportjs.org/), we also have examples for it using secure and encrypted cookies:
Expand Down
2 changes: 1 addition & 1 deletion docs/basic-features/data-fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ The `context` parameter is an object containing the following keys:
- `previewData` contains the preview data set by `setPreviewData`. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md).
- `locale` contains the active locale (if you've enabled [Internationalized Routing](/docs/advanced-features/i18n-routing.md)).
- `locales` contains all supported locales (if you've enabled [Internationalized Routing](/docs/advanced-features/i18n-routing.md)).
- `defaultLocale` contains the configured default locale (if you've enabled Internationalized Routing).
- `defaultLocale` contains the configured default locale (if you've enabled [Internationalized Routing](/docs/advanced-features/i18n-routing.md)).

`getStaticProps` should return an object with:

Expand Down
2 changes: 1 addition & 1 deletion docs/basic-features/image-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ The recommended way to style the inner `<img>` is to set the `className` prop on

Alternatively, you can import a [global stylesheet](/docs/basic-features/built-in-css-support#adding-a-global-stylesheet) and manually set the `className` prop to the same name used in the global stylesheet.

You cannot use [styled-jsx](/basic-features/built-in-css-support.md#css-in-js) because its scoped to the current component.
You cannot use [styled-jsx](/docs/basic-features/built-in-css-support.md#css-in-js) because it's scoped to the current component.

You cannot use the `style` prop because the `<Image>` component does not pass it through to the underlying `<img>`.

Expand Down
2 changes: 1 addition & 1 deletion docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Next.js can be deployed to any hosting provider that supports Node.js. Make sure
```json
{
"scripts": {
"dev": "next",
"dev": "next dev",
"build": "next build",
"start": "next start"
}
Expand Down
8 changes: 4 additions & 4 deletions docs/routing/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Next.js has a built-in, opinionated, and file-system based Router.

Next.js has a file-system based router built on the [concept of pages](/docs/basic-features/pages.md).

When a file is added to the `pages` directory it's automatically available as a route.
When a file is added to the `pages` directory, it's automatically available as a route.

The files inside the `pages` directory can be used to define most common patterns.

Expand All @@ -19,14 +19,14 @@ The router will automatically route files named `index` to the root of the direc

#### Nested routes

The router supports nested files. If you create a nested folder structure files will be automatically routed in the same way still.
The router supports nested files. If you create a nested folder structure, files will automatically be routed in the same way still.

- `pages/blog/first-post.js``/blog/first-post`
- `pages/dashboard/settings/username.js``/dashboard/settings/username`

#### Dynamic route segments

To match a dynamic segment you can use the bracket syntax. This allows you to match named parameters.
To match a dynamic segment, you can use the bracket syntax. This allows you to match named parameters.

- `pages/blog/[slug].js``/blog/:slug` (`/blog/hello-world`)
- `pages/[username]/settings.js``/:username/settings` (`/foo/settings`)
Expand Down Expand Up @@ -68,7 +68,7 @@ function Home() {
export default Home
```

In the example above we have multiple links, each one maps a path (`href`) to a known page:
The example above uses multiple links. Each one maps a path (`href`) to a known page:

- `/``pages/index.js`
- `/about``pages/about.js`
Expand Down
4 changes: 2 additions & 2 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: Learn how to set up Next.js with three commonly used testing tools
</ul>
</details>

Learn how to set up Next.js with three commonly used testing tools: [Cypress](https://www.cypress.io/blog/2021/04/06/cypress-component-testing-react/), [Jest](https://jestjs.io/docs/tutorial-react), and [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/).
Learn how to set up Next.js with commonly used testing tools: [Cypress](https://nextjs.org/docs/testing#cypress), [Playwright](https://nextjs.org/docs/testing#playwright), and [Jest with React Testing Library](https://nextjs.org/docs/testing#jest-and-react-testing-library).

## Cypress

Expand Down Expand Up @@ -114,7 +114,7 @@ Since Cypress is testing a real Next.js application, it requires the Next.js ser

Run `npm run build` and `npm run start`, then run `npm run cypress` in another terminal window to start Cypress.

> **Note:** Alternatively, you can install the `start-server-and-test` package and add it to the `package.json` scripts field: `"test": "start-server-and-test start http://localhost:3000 cypress"` to start the Next.js production server in conjuction with Cypress. Remember to rebuild your application after new changes.
> **Note:** Alternatively, you can install the `start-server-and-test` package and add it to the `package.json` scripts field: `"test": "start-server-and-test start http://localhost:3000 cypress"` to start the Next.js production server in conjunction with Cypress. Remember to rebuild your application after new changes.
### Getting ready for Continuous Integration (CI)

Expand Down
4 changes: 4 additions & 0 deletions errors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@
"title": "no-router-instance",
"path": "/errors/no-router-instance.md"
},
{
"title": "no-server-import-in-page",
"path": "/errors/no-server-import-in-page.md"
},
{
"title": "no-sync-scripts",
"path": "/errors/no-sync-scripts.md"
Expand Down
23 changes: 23 additions & 0 deletions errors/no-server-import-in-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# No Server Import In Page

### Why This Error Occurred

`next/server` was imported in a page outside of `pages/_middleware.js` (or `pages/_middleware.tsx` if you are using TypeScript)

### Possible Ways to Fix It

Only import and use `next/server` within `pages/_middleware.js` (or `pages/_middleware.tsx`) to add middlewares.

```jsx
// pages/_middleware.ts

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

export function middleware(req: NextRequest, ev: NextFetchEvent) {
return new Response('Hello, world!')
}
```

### Useful Links

- [Middleware](https://nextjs.org/docs/middleware)
2 changes: 1 addition & 1 deletion examples/auth0/pages/advanced/ssr-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function getServerSideProps({ req, res }) {
// Here you can check authentication status directly before rendering the page,
// however the page would be a serverless function, which is more expensive and
// slower than a static page with client side authentication
const session = await auth0.getSession(req)
const session = await auth0.getSession(req, res)

if (!session || !session.user) {
res.writeHead(302, {
Expand Down
4 changes: 2 additions & 2 deletions examples/with-google-analytics/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Example app with analytics

This example shows how to use [Next.js](https://github.com/vercel/next.js) along with [Google Analytics](https://developers.google.com/analytics/devguides/collection/gtagjs/). A custom [\_document](https://nextjs.org/docs/advanced-features/custom-document) is used to inject [tracking snippet](https://developers.google.com/analytics/devguides/collection/gtagjs/) and track [pageviews](https://developers.google.com/analytics/devguides/collection/gtagjs/pages) and [event](https://developers.google.com/analytics/devguides/collection/gtagjs/events).
This example shows how to use [Next.js](https://github.com/vercel/next.js) along with [Google Analytics](https://developers.google.com/analytics/devguides/collection/gtagjs/). A custom [\_app](https://nextjs.org/docs/advanced-features/custom-app) is used to inject [tracking snippet](https://developers.google.com/analytics/devguides/collection/gtagjs/) and track [pageviews](https://developers.google.com/analytics/devguides/collection/gtagjs/pages) and [event](https://developers.google.com/analytics/devguides/collection/gtagjs/events).

## Deploy your own

Expand All @@ -10,7 +10,7 @@ Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_mediu

## 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::
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 with-google-analytics with-google-analytics-app
Expand Down
35 changes: 35 additions & 0 deletions examples/with-playwright/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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
test-results/
27 changes: 27 additions & 0 deletions examples/with-playwright/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Next.js + Playwright

This example shows how to configure Playwright to work with Next.js.

## Preview

Preview the example live on [StackBlitz](http://stackblitz.com/):

[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-playwright)

## Deploy your own

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):

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

## 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 with-playwright with-playwright-app
# or
yarn create next-app --example with-playwright with-playwright-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)).
12 changes: 12 additions & 0 deletions examples/with-playwright/e2e/example.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test, expect } from '@playwright/test'

test('should navigate to the about page', async ({ page }) => {
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
await page.goto('/')
// Find an element with the text 'About Page' and click on it
await page.click('text=About Page')
// The new url should be "/about" (baseURL is used there)
await expect(page).toHaveURL('/about')
// The new page should contain an h1 with "About Page"
await expect(page.locator('h1')).toContainText('About Page')
})
17 changes: 17 additions & 0 deletions examples/with-playwright/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"test:e2e": "playwright test"
},
"dependencies": {
"next": "latest",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"@playwright/test": "^1.15.0"
}
}
7 changes: 7 additions & 0 deletions examples/with-playwright/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import '../styles/globals.css'

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

export default MyApp
17 changes: 17 additions & 0 deletions examples/with-playwright/pages/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import styles from '../styles/Home.module.css'
import Link from 'next/link'

export default function About() {
return (
<div className={styles.container}>
<main className={styles.main}>
<h1>About Page</h1>
<p className={styles.description}>
<Link href="/">
<a>&larr; Go Back</a>
</Link>
</p>
</main>
</div>
)
}
Loading

0 comments on commit ce35189

Please sign in to comment.