Skip to content

Commit 303faa8

Browse files
authored
Merge branch 'canary' into fix/wasm-loading
2 parents 7466c76 + 719fe52 commit 303faa8

File tree

32 files changed

+224
-96
lines changed

32 files changed

+224
-96
lines changed

docs/api-reference/next/server.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The function can be a default export and as such, does **not** have to be named
2525
2626
The `NextRequest` object is an extension of the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) interface, with the following added methods and properties:
2727
28-
- `cookies` - Has the cookies from the `Request`
28+
- `cookie` - Has the cookies from the `Request`
2929
- `nextUrl` - Includes an extended, parsed, URL object that gives you access to Next.js specific properties such as `pathname`, `basePath`, `trailingSlash` and `i18n`
3030
- `geo` - Has the geo location from the `Request`
3131
- `geo.country` - The country code
@@ -88,6 +88,23 @@ The introduction of the `307` status code means that the request method is prese
8888

8989
The `redirect()` method uses a `307` by default, instead of a `302` temporary redirect, meaning your requests will _always_ be preserved as `POST` requests.
9090

91+
### How do I access Environment Variables?
92+
93+
`process.env` can be used to access [Environment Variables](/docs/basic-features/environment-variables.md) from Middleware. These are evaluated at build time, so only environment variables _actually_ used will be included.
94+
95+
Any variables in `process.env` must be accessed directly, and **cannot** be destructured:
96+
97+
```ts
98+
// Accessed directly, and not destructured works. process.env.NODE_ENV is `"development"` or `"production"`
99+
console.log(process.env.NODE_ENV)
100+
// This will not work
101+
const { NODE_ENV } = process.env
102+
// NODE_ENV is `undefined`
103+
console.log(NODE_ENV)
104+
// process.env is `{}`
105+
console.log(process.env)
106+
```
107+
91108
## Related
92109

93110
<div class="card">

errors/manifest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"title": "Messages",
55
"heading": true,
66
"routes": [
7+
{
8+
"title": "react-hydration-error",
9+
"path": "/errors/react-hydration-error.md"
10+
},
711
{
812
"title": "beta-middleware",
913
"path": "/errors/beta-middleware.md"

errors/react-hydration-error.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# React Hydration Error
2+
3+
#### Why This Error Occurred
4+
5+
While rendering your application, there was a difference between the React tree that was pre-rendered (SSR/SSG) and the React tree that rendered during the first render in the Browser. The first render is called Hydration which is a [feature of React](https://reactjs.org/docs/react-dom.html#hydrate).
6+
7+
This can cause the React tree to be out of sync with the DOM and result in unexpected content/attributes being present.
8+
9+
#### Possible Ways to Fix It
10+
11+
In general this issue is caused by using a specific library or application code that is relying on something that could differ between pre-rendering and the browser. An example of this is using `window` in a component's rendering.
12+
13+
An example:
14+
15+
```jsx
16+
function MyComponent() {
17+
// This condition depends on `window`. During the first render of the browser the `color` variable will be different
18+
const color = typeof window !== 'undefined' ? 'red' : 'blue
19+
// As color is passed as a prop there is a mismatch between what was rendered server-side vs what was rendered in the first render
20+
return <h1 className={`title ${color}`}>Hello World!</h1>
21+
}
22+
```
23+
24+
How to fix it:
25+
26+
```jsx
27+
// In order to prevent the first render from being different you can use `useEffect` which is only executed in the browser and is executed during hydration
28+
import { useEffect, useState } from 'react'
29+
function MyComponent() {
30+
// The default value is 'blue', it will be used during pre-rendering and the first render in the browser (hydration)
31+
const [color, setColor] = useState('blue')
32+
// During hydration `useEffect` is called. `window` is available in `useEffect`. In this case because we know we're in the browser checking for window is not needed. If you need to read something from window that is fine.
33+
// By calling `setColor` in `useEffect` a render is triggered after hydrating, this causes the "browser specific" value to be available. In this case 'red'.
34+
useEffect(() => setColor('red'), [])
35+
// As color is a state passed as a prop there is no mismatch between what was rendered server-side vs what was rendered in the first render. After useEffect runs the color is set to 'red'
36+
return <h1 className={`title ${color}`}>Hello World!</h1>
37+
}
38+
```
39+
40+
Common causes with css-in-js libraries:
41+
42+
- When using Styled Components / Emotion
43+
- When css-in-js libraries are not set up for pre-rendering (SSR/SSG) it will often lead to a hydration mismatch. In general this means the application has to follow the Next.js example for the library. For example if `pages/_document` is missing and the Babel plugin is not added.
44+
- Possible fix for Styled Components: https://github.com/vercel/next.js/tree/canary/examples/with-styled-components
45+
- If you want to leverage Styled Components with the new Next.js Compiler in Next.js 12 there is an [experimental flag available](https://github.com/vercel/next.js/discussions/30174#discussion-3643870)
46+
- Possible fix for Emotion: https://github.com/vercel/next.js/tree/canary/examples/with-emotion
47+
- When using other css-in-js libraries
48+
- Similar to Styled Components / Emotion css-in-js libraries generally need configuration specified in their examples in the [examples directory](https://github.com/vercel/next.js/tree/canary/examples)
49+
50+
### Useful Links
51+
52+
- [React Hydration Documentation](https://reactjs.org/docs/react-dom.html#hydrate)
53+
- [Josh Comeau's article on React Hydration](https://www.joshwcomeau.com/react/the-perils-of-rehydration/)

examples/progressive-web-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"dependencies": {
99
"next": "latest",
10-
"next-pwa": "^3.1.0",
10+
"next-pwa": "^5.4.1",
1111
"react": "^17.0.2",
1212
"react-dom": "^17.0.2"
1313
}

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
"registry": "https://registry.npmjs.org/"
1818
}
1919
},
20-
"version": "12.0.5-canary.8"
20+
"version": "12.0.5-canary.9"
2121
}

packages/create-next-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-next-app",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"keywords": [
55
"react",
66
"next",

packages/eslint-config-next/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-config-next",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"description": "ESLint configuration used by NextJS.",
55
"main": "index.js",
66
"license": "MIT",
@@ -9,7 +9,7 @@
99
"directory": "packages/eslint-config-next"
1010
},
1111
"dependencies": {
12-
"@next/eslint-plugin-next": "12.0.5-canary.8",
12+
"@next/eslint-plugin-next": "12.0.5-canary.9",
1313
"@rushstack/eslint-patch": "^1.0.8",
1414
"@typescript-eslint/parser": "^5.0.0",
1515
"eslint-import-resolver-node": "^0.3.4",

packages/eslint-plugin-next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/eslint-plugin-next",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"description": "ESLint plugin for NextJS.",
55
"main": "lib/index.js",
66
"license": "MIT",

packages/next-bundle-analyzer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/bundle-analyzer",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"main": "index.js",
55
"license": "MIT",
66
"repository": {

packages/next-codemod/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/codemod",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"license": "MIT",
55
"dependencies": {
66
"chalk": "4.1.0",

packages/next-env/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/env",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"keywords": [
55
"react",
66
"next",

packages/next-mdx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/mdx",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"main": "index.js",
55
"license": "MIT",
66
"repository": {

packages/next-plugin-storybook/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/plugin-storybook",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"repository": {
55
"url": "vercel/next.js",
66
"directory": "packages/next-plugin-storybook"

packages/next-polyfill-module/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/polyfill-module",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
55
"main": "dist/polyfill-module.js",
66
"license": "MIT",

packages/next-polyfill-nomodule/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/polyfill-nomodule",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"description": "A polyfill for non-dead, nomodule browsers.",
55
"main": "dist/polyfill-nomodule.js",
66
"license": "MIT",

packages/next-swc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next/swc",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"private": true,
55
"scripts": {
66
"build-native": "napi build --platform --cargo-name next_swc_napi native"

packages/next/build/output/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ let serverWebWasLoading = false
107107
buildStore.subscribe((state) => {
108108
const { amp, client, server, serverWeb, trigger } = state
109109

110-
const { bootstrap: bootstrapping, appUrl } = consoleStore.getState()
111-
if (bootstrapping && (client.loading || server.loading)) {
112-
return
113-
}
110+
const { appUrl } = consoleStore.getState()
114111

115112
if (client.loading || server.loading || serverWeb?.loading) {
116113
consoleStore.setState(

packages/next/build/output/store.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,20 @@ store.subscribe((state) => {
5656
if (state.appUrl) {
5757
Log.ready(`started server on ${state.bindAddr}, url: ${state.appUrl}`)
5858
}
59-
if (startTime === 0) startTime = Date.now()
6059
return
6160
}
6261

6362
if (state.loading) {
6463
if (state.trigger) {
65-
Log.wait(`compiling ${state.trigger}...`)
64+
if (state.trigger !== 'initial') {
65+
Log.wait(`compiling ${state.trigger}...`)
66+
}
6667
} else {
6768
Log.wait('compiling...')
6869
}
69-
if (startTime === 0) startTime = Date.now()
70+
if (startTime === 0) {
71+
startTime = Date.now()
72+
}
7073
return
7174
}
7275

packages/next/client/dev/noop.js

Whitespace-only changes.

packages/next/client/next-dev.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* globals __REPLACE_NOOP_IMPORT__ */
21
import { initNext, version, router, emitter, render, renderError } from './'
32
import initOnDemandEntries from './dev/on-demand-entries-client'
43
import initWebpackHMR from './dev/webpack-hot-middleware-client'
@@ -10,13 +9,6 @@ import {
109
urlQueryToSearchParams,
1110
} from '../shared/lib/router/utils/querystring'
1211

13-
// Temporary workaround for the issue described here:
14-
// https://github.com/vercel/next.js/issues/3775#issuecomment-407438123
15-
// The runtimeChunk doesn't have dynamic import handling code when there hasn't been a dynamic import
16-
// The runtimeChunk can't hot reload itself currently to correct it when adding pages using on-demand-entries
17-
// eslint-disable-next-line no-unused-expressions
18-
__REPLACE_NOOP_IMPORT__
19-
2012
const {
2113
__NEXT_DATA__: { assetPrefix },
2214
} = window
@@ -26,6 +18,25 @@ const webpackHMR = initWebpackHMR()
2618

2719
connectHMR({ assetPrefix: prefix, path: '/_next/webpack-hmr' })
2820

21+
if (!window._nextSetupHydrationWarning) {
22+
const origConsoleError = window.console.error
23+
window.console.error = (...args) => {
24+
const isHydrateError = args.some(
25+
(arg) =>
26+
typeof arg === 'string' &&
27+
arg.match(/Warning:.*?did not match.*?Server:/)
28+
)
29+
if (isHydrateError) {
30+
args = [
31+
...args,
32+
`\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`,
33+
]
34+
}
35+
origConsoleError.apply(window.console, args)
36+
}
37+
window._nextSetupHydrationWarning = true
38+
}
39+
2940
window.next = {
3041
version,
3142
// router is initialized later so it has to be live-binded

packages/next/compiled/webpack/bundle5.js

Lines changed: 33 additions & 14 deletions
Large diffs are not rendered by default.

packages/next/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next",
3-
"version": "12.0.5-canary.8",
3+
"version": "12.0.5-canary.9",
44
"description": "The React Framework",
55
"main": "./dist/server/next.js",
66
"license": "MIT",
@@ -69,10 +69,10 @@
6969
"@babel/runtime": "7.15.4",
7070
"@hapi/accept": "5.0.2",
7171
"@napi-rs/triples": "1.0.3",
72-
"@next/env": "12.0.5-canary.8",
73-
"@next/polyfill-module": "12.0.5-canary.8",
74-
"@next/react-dev-overlay": "12.0.5-canary.8",
75-
"@next/react-refresh-utils": "12.0.5-canary.8",
72+
"@next/env": "12.0.5-canary.9",
73+
"@next/polyfill-module": "12.0.5-canary.9",
74+
"@next/react-dev-overlay": "12.0.5-canary.9",
75+
"@next/react-refresh-utils": "12.0.5-canary.9",
7676
"acorn": "8.5.0",
7777
"assert": "2.0.0",
7878
"browserify-zlib": "0.2.0",
@@ -155,7 +155,7 @@
155155
"@babel/traverse": "7.15.0",
156156
"@babel/types": "7.15.0",
157157
"@napi-rs/cli": "1.2.1",
158-
"@next/polyfill-nomodule": "12.0.5-canary.8",
158+
"@next/polyfill-nomodule": "12.0.5-canary.9",
159159
"@peculiar/webcrypto": "1.1.7",
160160
"@taskr/clear": "1.1.0",
161161
"@taskr/esnext": "1.1.0",
@@ -267,7 +267,7 @@
267267
"webpack-sources1": "npm:[email protected]",
268268
"webpack-sources3": "npm:[email protected]",
269269
"webpack4": "npm:[email protected]",
270-
"webpack5": "npm:[email protected].1",
270+
"webpack5": "npm:[email protected].3",
271271
"ws": "8.2.3"
272272
},
273273
"resolutions": {

0 commit comments

Comments
 (0)