Commit graph

519 commits

Author SHA1 Message Date
Hannes Bornö
435eca3fc5
Add next/font import (#45891)
Enables using `next/font` by adding `@next/font` as a dependency and
reexporting its loaders.

Always generates the `font-loader-manifest` as we can't know beforehand
if the user intends to use `next/font` or not.

Also adds telemetry for `next/font` usage.

The tests are updated to use `next/font`. But `@next/font` is tested in
`test/e2e/next-font/index.test.ts` and `test/e2e/app-dir/next-font` as
well to ensure it doesn't break.

Fixes NEXT-351

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-16 15:33:39 +01:00
Wyatt Johnson
74ca99c866
Server Router Improvements (#45716)
This adds updated matching handle for the server to separate out the matching and executing of different route types e.g. page routes, API routes, and app routes. 

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-02-15 00:16:45 +00:00
Jan Kaifer
90bfb762c8
Update app-static-to-dynamic-error.md (#45704)
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-08 16:08:35 +00:00
Dawid Czesak
e91f2b0309
Update no-img-element.md (#45701) 2023-02-08 14:11:39 +00:00
Sam Denty
d9412b7621
Update react-hydration-error.md (#43671)
This adds a new section about local overrides, as I ran into this error and it took me ages to figure out what was going wrong

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-02-07 22:38:03 +00:00
Andrés Bedoya (SrHart)
91e0be1c3e
Update no-html-link-for-pages.md (#45247)
Update no-html-link-for-pages.md based on the new link API https://nextjs.org/docs/api-reference/next/link
2023-01-25 03:17:09 +00:00
ilyasmez
57d0e77f78
chore: fix invalid-new-link error typo (#45157) 2023-01-23 17:06:50 -08:00
JJ Kasper
6b7c69fa4d
Add proper error when app path switches static to dynamic (#44989) 2023-01-18 10:24:35 -08:00
Hannes Bornö
9a5c8cebf0
Improve errors caused by Class Component rendered in a server component. (#44726)
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: Steven <steven@ceriously.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-01-10 14:47:20 +01:00
Hannes Bornö
5f2c9d0b30
Update subset validation in @next/font/google and fix CJK bug (#44594)
Currently there's a bug when selecting Chinese, Japanese or Korean (CJK)
as subsets.
```js
const notoSans = Noto_Sans_JP({
  subsets: ['japanese'],
})
```
It actually doesn't work, nothing preloads. This PR solves this by
removing CJK languages as candidates for preloading. The reason is that
they contain so many glyphs that each font-family is split up in 100+
font files. It doesn't make sense to preload all of them.

So CJK users will have to disable preloading.
```js
const notoSansJapanese = Noto_Sans_JP({
  weight: '400',
  preload: false,
})
```
In case you do manually disable preloading like above, the default
`font-display` is changed to `swap`.

This PR also improves the validation errors of subsets.
1. Providing unknown subset
```
`@next/font` error:
Unknown subset `japanese` for font `Inter`.
Available subsets: `cyrillic`, `cyrillic-ext`, `greek`, `greek-ext`, `latin`, `latin-ext`, `vietnamese`
```
2. Missing specified subset. The error has a link with further
instructions.
```
`@next/font` error:
Missing selected subsets for font `Inter`. Please specify subsets in the function call or in your `next.config.js`. Read more: https://nextjs.org/docs/messages/google-fonts-missing-subsets
```

fixes NEXT-336

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-01-05 15:51:38 -08:00
Hannes Bornö
d89bdb2b0c
Improve errors caused by client hook called in a server component (#44538)
Improve errors caused by client hook called in a server component.

Before
![image](https://user-images.githubusercontent.com/25056922/210399932-02d1071c-0d06-4260-8f1d-7da436e8bca9.png)


After
![image](https://user-images.githubusercontent.com/25056922/210399817-f949fb14-2f81-4309-bfcb-27af38ea1329.png)

Fixes NEXT-311

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-01-04 11:01:50 +00:00
Erik Hedberg
2b3a38ee47
docs: fix spelling error (#44191)
## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-12-20 14:51:14 +00:00
Damien Simonin Feugas
35f1a3501a
feat: enables 'edge' as a possible runtime for API routes (#44045) 2022-12-15 10:08:03 +01:00
Steven
297459d534
Update no-img-element lint rule (#43982)
This updates the `no-img-element` lint rule to explain the trade-offs of enabling or disabling Image Optimization.
2022-12-13 01:50:59 +00:00
Hannes Bornö
50caf8b191
Add helpful error for createContext used in Server Components (#43747)
Format runtime server errors similarly to how build errors [are
formatted](https://github.com/vercel/next.js/blob/canary/packages/next/client/dev/error-overlay/format-webpack-messages.js).
Add helpful message when createContext is used in Server Components.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-12-11 20:46:29 -06:00
Michael Novotny
bd9148d498
Changes vercel/examples links from linking to GitHub repo to template marketplace (#43780)
### Description

* Gives us [Vercel] more SEO juice rather than GitHub.
* Gives us the freedom to control GitHub repo file structure independent from URLs.

## Feature

- [x] Documentation added
2022-12-07 00:27:43 +00:00
Hannes Bornö
cd8a1a4be5
Remove stack trace from full reload warning (#43453)
Adds a new error link with possible ways to fix the cause of the full reload. Removes the stack trace and adds the file causing the error to the message.

Before
![image](https://user-images.githubusercontent.com/25056922/204278894-dd773e6d-9d79-459a-abc4-243c1b22b206.png)

After:
![image](https://user-images.githubusercontent.com/25056922/204278538-5e26a174-bae4-457f-aac4-f239398227f8.png)

Fixes #43448

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-28 13:18:26 +00:00
Hannes Bornö
0f195f0270
App directory next/link dynamic href dev error (#43074)
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Fixes https://github.com/vercel/next.js/issues/42715
2022-11-25 15:03:00 +01:00
Balázs Orbán
aa51c26c2e
docs: add error link when missing appDir: true (#43293)
[Slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1669206360940399)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2022-11-23 12:07:14 -08:00
Lee Robinson
21d7d22454
Add JWT example to error page. (#43162)
https://github.com/vercel/feedback/issues/23531
2022-11-21 06:48:13 +00:00
JJ Kasper
8b4d9e652e
Add more details to invalid-next-config doc (#42917)
This adds some more clarity to the error doc and we can also iterate on
the warnings to show suggestions as well in follow-ups.

x-ref: [slack
thread](https://vercel.slack.com/archives/CHZGRCJSD/p1668430036939189?thread_ts=1668394104.483679&cid=CHZGRCJSD)

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-14 13:59:59 -08:00
Hannes Bornö
584d842ae0
Fix @next/font imports from outside of the root directory (#42678)
Similar to https://github.com/vercel/next.js/pull/42106. Make
`@next/font` works as expected when using `transpilePackages`.

Also makes sure `@next/font` is auto-configured correctly when in a
monorepo.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)


Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-11-09 13:22:45 -08:00
Wyatt Johnson
fa19c4410f
next/compat/router (#42502)
After speaking with @timneutkens, this PR provides a smoother experience to users trying to migrate over to app without affecting users in pages.

 This PR adds a new export available from `next/compat/router` that exposes a `useRouter()` hook that can be used in both `app/` and `pages/`. It differs from `next/router` in that it does not throw an error when the pages router is not mounted, and instead has a return type of `NextRouter | null`. This allows developers to convert components to support running in both `app/` and `pages/` as they are transitioning over to `app/`.

A component that before looked like this:

```tsx
import { useRouter } from 'next/router';

const MyComponent = () => {
  const { isReady, query } = useRouter();
  // ...
};
```

Will error when converted over to `next/compat/router`, as `null` cannot be destructured. Instead, developers will be able to take advantage of new hooks:

```tsx
import { useEffect } from 'react';
import { useRouter } from 'next/compat/router';
import { useSearchParams } from 'next/navigation';

const MyComponent = () => {
  const router = useRouter() // may be null or a NextRouter instance
  const searchParams = useSearchParams()

  useEffect(() => {
    if (router && !router.isReady) {
      return
    }

    // In `app/`, searchParams will be ready immediately with the values, in
    // `pages/` it will be available after the router is ready.
    const search = searchParams.get('search')
    // ...
  }, [router, searchParams])

  // ...
}
```

This component will now work in both `pages/` and `app/`. When the component is no longer used in `pages/`, you can remove the references to the compat router:

```tsx
import { useSearchParams } from 'next/navigation';

const MyComponent = () => {
  const searchParams = useSearchParams()

  // As this component is only used in `app/`, the compat router can be removed.
  const search = searchParams.get('search')

  // ...
}

```

Note that as of Next.js 13, calling `useRouter` from `next/router` will throw an error when not mounted. This now includes an error page that can be used to assist developers.

We hope to introduce a codemod that can convert instances of your `useRouter` from `next/router` to `next/compat/router` in the future.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-11-07 18:16:28 +00:00
Steven Tey
cf3bb583d9
Included negative matcher docs in upgrade guide (#42489)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-04 10:20:53 -07:00
davidnx
ee62acacfe
react-version.md: Update minimum React version for Next.js 13 (#42490)
Updated minimum React version to match information at https://nextjs.org/blog/next-13. This page is the first Google result for "next.js 13 react 17" and the info in it is out of data after Next.js 13 was released.

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-04 15:44:22 +00:00
Steven
73c5b77a84
Fix docs for next/image upgrade guide (#42424)
This fixes a typo and also clarifies when the codemod might fail to
migrate properly.
2022-11-03 11:11:06 -07:00
Shu Ding
b72dc5b69f
Improve the error message when custom export fields are used in an entry (#42221) 2022-11-01 11:17:06 -07:00
Duncan Ogle
848bb3af73
Escape HTML on "<Link> with <a> child" page (#42114)
Non-escaped characters is leading this page to attempt to render the title as HTML tags. This change escapes those values so they may present correctly

https://nextjs.org/docs/messages/invalid-new-link-with-extra-anchor

<img width="524" alt="Screenshot 2022-10-29 at 06 57 16" src="https://user-images.githubusercontent.com/5629994/198817257-47b45ce9-2841-4db1-82f2-91e2c9b22808.png">



## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-10-31 16:16:42 +00:00
Steven
191710ddfe
Show error message when using legacy props on new next/image (#41930)
This PR shows a better error message when using legacy props on new
`next/image` (introduced in Next.js 13) and makes the codemod more
discoverable.
2022-10-27 12:17:28 -04:00
Balázs Orbán
4d20beb7c5
BREAKING CHANGE: feat(edge): split NextCookies to RequestCookies and ResponseCookies (#41526)
Ref: [Slack
thread](https://vercel.slack.com/archives/C035J346QQL/p1666056382299069?thread_ts=1666041444.633059&cid=C035J346QQL),
[docs update](https://github.com/vercel/front/pull/17090)

Spec: https://wicg.github.io/cookie-store/

BREAKING CHANGE:

Ref: https://github.com/vercel/edge-runtime/pull/177,
https://github.com/vercel/edge-runtime/pull/181

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-10-27 00:20:39 -07:00
Hannes Bornö
e64bf98511
Update font links and add missing error (#41910)
Adds an error that was missing. Link to beta until the changes are live.

closes #41900

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-10-26 23:00:40 -07:00
Lee Robinson
a05a0c7784
Update the documentation. (#41758)
Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Matt Kane <m@mk.gg>
Co-authored-by: Ismael <ismael.rumzan@gmail.com>
Co-authored-by: Balázs Orbán <info@balazsorban.com>
2022-10-26 14:04:45 -07:00
Steven
e107bc4561
Throw error <a> child in <Link> (#41756)
This PR improves the error message when `<a>` is a child of `<Link>`
2022-10-24 22:39:22 -07:00
Steven
7102050ec7
Add support for images.loaderFile config (#41585)
This PR adds a new configure property, `images.loaderFile` that allow
you to define a path to a file with an exported image loader function.

This is useful when migrating from `next/legacy/image` to `next/image`
because it lets you configure the loader for every instance of
`next/image` once, similar to the legacy "built-in loaders".
2022-10-21 12:31:53 -07:00
Steven
9c5bb5bfe9
BREAKING CHANGE: Remove target: serverless (#41495)
The `target: serverless` config was deprecated a year ago starting in
[Next.js 12](https://nextjs.org/blog/next-12).

Tests were disabled in #41252 so we can now remove `target: serverless`
and all usage of `target` in `next.config.js`.

Co-authored-by: Balázs Orbán <info@balazsorban.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-10-18 09:47:13 -07:00
Lee Robinson
6101bf69ad
Add remotePatterns link to next-image-unconfigured-host (#41504)
Didn't realize the `remotePatterns` usage was already in here (not live
yet, but merged into `canary`) - so just also added on this link for the
end 👍
2022-10-17 22:06:50 -07:00
Damien Simonin Feugas
d8395e4eeb
docs: more detailed error message when failing to parse a middleware matcher (#41390)
## 📖 What's in there?

This is a follow-up for https://github.com/vercel/next.js/pull/40180
Someone made the good point that users may get directed to this error page while setting up middleware matchers. And that page has no information (yet) about it.

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)



Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
2022-10-13 18:03:10 +00:00
Lee Robinson
a315c748c7
Update node-module-in-edge-runtime.md (#41375)
To be a bit more clear.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-10-13 04:22:04 +00:00
Damien Simonin Feugas
bef709bc74
fix(middleware): 'instanceof Function' is dynamic code false-positive (#41249)
## 🐛 What's in there?

`foo instanceof Function` is wrongly considered as Dynamic code evaluation by our static analyzer.
This PR fixes it.

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have a helpful link attached, see `contributing.md`

## 🧪 How to reproduce?

1. Create a simple repro in `examples` folder:
   ```js
   // examples/instance-of-function/pages/index.js
   export default function Home() {
     return <h1>home</h1>
   }
   
   // examples/instance-of-function/middleware.js
   import { NextResponse } from 'next/server'
   
   export default async function handler() {
     console.log('is arrow a function?', (() => {}) instanceof Function)
     return NextResponse.next()
   }
   ```
1. build with next `pnpm next build examples/instance-of-function`
   > the build fails
1. rebuild next to include PR's fix `pnpm build`
1. build with new next `pnpm next build examples/instance-of-function`
   > the build works

## 📔 Notes to reviewers

`hooks.expression.for(`${prefix}Function`).tap(NAME, handleExpression)` is actually legacy code from the original implementation. It's used when finding `Function` regardless of how it is used. We only want to find `new Function()` or `Function()`, which `hooks.calls` and `hooks.new` are covering.

`eval instanceof Function` is perfectly legit code on the edge, despite its uselessness :lol-think: 

Because we got multiple people asking "how do I relax this error when my code contains unreachable dynamic code evaluation", I've copy-pasted details about `config.unstable_allowDynamic` into the error page. Because users do not always click links :blob_shrug:
2022-10-07 14:14:11 +00:00
Steven
5e6b008b56
Fix docs for next/image unconfigured hosts (#41223)
Fix docs for next/image unconfigured hosts to mention `remotePatterns`.
2022-10-06 16:51:41 +00:00
Hannes Bornö
e5be49eec1
Font loader with babel error (#41151)
Adds build error when using font loaders with babel. Otherwise you'll
get other unrelated errors.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-10-05 22:14:31 -07:00
Balázs Orbán
cbda3b52dc
docs(image): improve error message when sharp is missing in standalone mode (#41133)
Partially addresses #41111

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-10-05 17:28:29 +00:00
Eliaz Bobadilla
1d026ec074
class component -> functional component (#41089)
easier to read

## Documentation / Examples

- [x ] Make sure the linting passes by running `pnpm lint`
- [ x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)


Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
2022-10-01 20:18:49 +00:00
Artur Sedlukha
377a39a88d
Typo in middleware upgrade guide (#40963)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-09-27 12:40:50 -07:00
Abdallah NourAldin
194b6ad1c1
update url for React Hydration Documentation (#40883)
Docs point to old
__[hydrate()](https://reactjs.org/docs/react-dom.html#hydrate)__, which
had been replaced with
__[hydrateRoot()](https://reactjs.org/docs/react-dom-client.html#hydrateroot)__
as React 18 came:

> Note:
hydrate has been replaced with hydrateRoot in React 18. See
[hydrateRoot](https://reactjs.org/docs/react-dom-client.html#hydrateroot)
for more info.

From [react 18 docs](https://reactjs.org/docs/react-dom.html#hydrate).
2022-09-26 12:35:12 -07:00
abdennor
1bbd264216
Add additional fix in hydration error document (#40675)
I had the same issue, so the fix that worked for me was pulled from this
thread https://stackoverflow.com/a/71870995

I have been experiencing the same problem lately with NextJS and i am
not sure if my observations are applicable to other libraries. I had
been wrapping my components with an improper tag that is, NextJS is not
comfortable having a p tag wrapping your divs, sections etc so it will
yell "Hydration failed because the initial UI does not match what was
rendered on the server". So I solved this problem by examining how my
elements were wrapping each other. With material UI you would need to be
cautious for example if you use a Typography component as a wrapper, the
default value of the component prop is "p" so you will experience the
error if you don't change the component value to something semantic. So
in my own opinion based on my personal experience the problem is caused
by improper arrangement of html elements and to solve the problem in the
context of NextJS one will have to reevaluate how they are arranging
their html element

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->


## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-09-22 18:14:18 -07:00
Balázs Orbán
3ff21ed178
refactor: split up CONTRIBUTING.md (#40515)
Continues #39778

Closes #40499

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-09-16 14:54:58 -07:00
Jan Klimo
c11310b0f1
Fix a typo in docs (#40501)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
In order to make sure your PR is handled as smoothly as possible we
request that you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: Balázs Orbán <info@balazsorban.com>
2022-09-13 13:22:25 +02:00
Ryan Parker
0a093c0e82
docs(errors/large-page-data): how to see data being passed to page (#40491)
## Summary

This PR adds a note about how to see the data that this error is complaining about:

```sh
Warning: data for page "/" is xxx which exceeds the threshold of 128 kB, this amount of data can reduce performance.
```

This debug trick was something I came across in a [discussion](https://github.com/vercel/next.js/discussions/39880) and I thought it would be helpful to others like it was for me.

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-09-13 00:44:43 +00:00
Damien Simonin Feugas
97ac344468
feat(edge): allows configuring Dynamic code execution guard (#39539)
### 📖  What's in there?

Dynamic code evaluation (`eval()`, `new Function()`, ...) is not
supported on the edge runtime, hence why we fail the build when
detecting such statement in the middleware or `experimental-edge` routes
at build time.

However, there could be false positives, which static analysis and
tree-shaking can not exclude:
- `qs` through these dependencies (get-intrinsic:
[source](https://github.com/ljharb/get-intrinsic/blob/main/index.js#L12))
- `function-bind`
([source](https://github.com/Raynos/function-bind/blob/master/implementation.js#L42))
- `has`
([source](https://github.com/tarruda/has/blob/master/src/index.js#L5))

This PR leverages the existing `config` export to let user allow some of
their files.
it’s meant for allowing users to import 3rd party modules who embed
dynamic code evaluation, but do not use it (because or code paths), and
can't be tree-shaked.

By default, it’s keeping the existing behavior: warn in dev, fails to
build.
If users allow dynamic code, and that code is reached at runtime, their
app stills breaks.

### 🧪 How to test?

- (existing) integration tests for disallowing dynamic code evaluation:
`pnpm testheadless --testPathPattern=runtime-dynamic`
- (new) integration tests for allowing dynamic code evaluation: `pnpm
testheadless --testPathPattern=runtime-configurable`
- (amended) production tests for validating the new configuration keys:
`pnpm testheadless --testPathPattern=config-validations`

To try it live, you could have an application such as:
```js
// lib/index.js
/* eslint-disable no-eval */
export function hasUnusedDynamic() {
  if ((() => false)()) {
    eval('100')
  }
}

export function hasDynamic() {
  eval('100')
}

// pages/index.jsx
export default function Page({ edgeRoute }) {
  return <p>{edgeRoute}</p>
}

export const getServerSideProps = async (req) => {
  const res = await fetch(`http://localhost:3000/api/route`)
  const data = await res.json()
  return { props: { edgeRoute: data.ok ? `Hi from the edge route` : '' } }
}

// pages/api/route.js
import { hasDynamic } from '../../lib'

export default async function handle() {
  hasDynamic()
  return Response.json({ ok: true })
}

export const config = { 
  runtime: 'experimental-edge' ,
  allowDynamic: '/lib/**'
}
```

Playing with `config.allowDynamic`, you should be able to:
- build the app even if it uses `eval()` (it will obviously fail at
runtime)
- build the app that _imports but does not use_ `eval()`
- run the app in dev, even if it uses `eval()` with no warning

### 🆙 Notes to reviewers

Before adding documentation and telemetry, I'd like to collect comments
on a couple of points:
- the overall design for this feature: is a list of globs useful and
easy enough?
- should the globs be relative to the application root (current
implementation) to to the edge route/middleware file?
- (especially to @sokra) is the implementation idiomatic enough? I've
leverage loaders to read the _entry point_ configuration once, then the
ModuleGraph to get it back during the parsing phase. I couldn't re-use
the existing `getExtractMetadata()` facility since it's happening late
after the parsing.
- there's a glitch with `import { ServerRuntime } from '../../types'` in
`get-page-static-info.ts`
([here](https://github.com/vercel/next.js/pull/39539/files#diff-cb7ac6392c3dd707c5edab159c3144ec114eafea92dad5d98f4eedfc612174d2L12)).
I had to use `next/types` because it was failing during lint. Any clue
why?

### ☑️ Checklist

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
- [x] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`
2022-09-12 15:01:00 -07:00
Wyatt Johnson
c6ef857d57
Subresource Integrity for App Directory (#39729)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
In order to make sure your PR is handled as smoothly as possible we
request that you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

This serves to add support for [Subresource
Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)
hashes for scripts added from the new app directory. This also has
support for utilizing nonce values passed from request headers (expected
to be generated per request in middleware) in the bootstrapping scripts
via the `Content-Security-Policy` header as such:

```
Content-Security-Policy: script-src 'nonce-2726c7f26c'
```

Which results in the inline scripts having a new `nonce` attribute hash
added. These features combined support for setting an aggressive Content
Security Policy on scripts loaded.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: Steven <steven@ceriously.com>
2022-09-08 15:17:15 -07:00
Yusuke Hayashi
885defd442
Fix typo in error/middleware-upgrade-guide.md (#40176)
No Reponse Body -> No Response Body

<img width="330" alt="スクリーンショット 2022-09-02 16 40 04" src="https://user-images.githubusercontent.com/11132792/188084749-a1dd4ca6-b108-47ef-85d9-5f1602cb8cea.png">
2022-09-02 18:20:22 +00:00
Steven
4cc7f11da2
Update to stable: next/future/image, remotePatterns, unoptimized (#40142)
This PR updates a few features from experimental to stable status:

- `next/future/image` component
- `remotePatterns` configuration
- `unoptimized` configuration
2022-08-31 22:44:17 +00:00
Jerome Leclanche
a799d25cfa
Mention largePageDataBytes in warning docs (#39941)
Also fix the KB unit to `kB` (https://en.wikipedia.org/wiki/Kilobyte)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-08-25 10:52:32 -05:00
Sukka
5360440413
fix(#39609): warns about suspense and ssr (#39676)
Currently, `next/dynamic` will opt-in to `React.lazy` if `{ suspense: true }` is used. And React 18 will always resolve the `Suspense` boundary on the server-side, effectively ignoring the `ssr` option.

The PR fixes #39609 by showing a warning message when `{ suspense: true, ssr: false }` is detected. The error documentation and the corresponding test case has also been updated.

In the future, Next.js could implement a custom version of `React.lazy` that could suspense without executing the lazy-loaded component on the server-side.

cc @huozhi 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
2022-08-18 13:53:23 +00:00
M. Zakiyuddin Munziri
5f033ecb54
Fix docs path "Invalid getServerSideProps Return Value" (#39443)
# Description

The link to https://nextjs.org/docs/messages/invalid-getserversideprops-value does not exist, the correct link should be https://nextjs.org/docs/messages/invalid-getserversideprops-return-value

![image](https://user-images.githubusercontent.com/78015359/183795431-53210024-4938-4f50-9f0a-cb412383780e.png)

![image](https://user-images.githubusercontent.com/78015359/183795482-f17f31c2-cbaf-4652-ad77-5294fa8f104a.png)

# Solution

## From

Referring to the pattern, there is no `return`

![Screenshot from 2022-08-10 10-04-33](https://user-images.githubusercontent.com/78015359/183795393-ba244d6a-84b3-426c-b5ef-4e17d108902a.png)

## To

So I removed the word `return` to match the pattern

![image](https://user-images.githubusercontent.com/78015359/183796280-da6f10d1-c017-4075-942a-01d9ad234c27.png)


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-08-10 02:34:43 +00:00
Lee Robinson
5d93753bc3
API Routes Request Helpers docs. (#39407)
Renames Middleware to Request Helpers to be both more accurate and also less confusing since Middleware is a separate feature of Next.js now.
2022-08-09 02:02:14 +00:00
Balázs Orbán
b292bdc293
fix(ts): Middleware type tweaks (#38625)
* fix internal type

* allow `void` to be returned from Middleware

* mark deprecated APIs in JSDoc

* fix typo

* add missing error page

* remove unused import
2022-08-08 09:40:44 -05:00
Josh Goldberg
963585a4fc
fix: improve logging for _devPagesManifest.json loading failures (#38046)
## Bug

- [x] Fixes #38047
- ~[ ] Integration tests added: do you want them added for such an unusual edge case?~ [Comment below: skipping](https://github.com/vercel/next.js/pull/38046#issuecomment-1207444526)
- [x] Errors have helpful link attached, see `contributing.md`: ~I'll wait until this approach & error message are confirmed before thinking more deeply on how to explain the error~

Sending a draft PR as reference ahead of time. 🙂 

Doesn't resolve the root issue of why `_devPagesManifest.json` might fail to load. But does improve the log for when it happens. I'd suggest applying this same fix to `_devMiddlewareManifest.json` too.

<table>
<thead>
<tr>
<th></th>
<th>Before</th>
<th>After</th>
</tr>
</thead>
<tbody>
<tr>
<th>Console</th>
<td>
<pre>
<code>
Failed to fetch devPagesManifest TypeError: Failed to fetch
    at PageLoader.getPageList (page-loader.js?e87a:30:53)
    at _callee$ (router.js?8684:955:45)
    at ...
</code>
</pre>
<pre>
<code>
router.js?8684:1319 Uncaught (in promise) TypeError: Cannot \
read properties of undefined (reading 'includes')
    at resolveDynamicRoute (router.js?8684:1319:16)
    at _callee$
    at ...
</code>
</pre>
</td>
<td>
<pre>
<code>
Failed to fetch devPagesManifest: TypeError: Failed to fetch
    at PageLoader.getPageList (page-loader.js?e87a:30:53)
    at _callee$ (router.js?8684:955:45)
    at ...
</code>
</pre>
</td>
</tr>
<tr>
<th>Visual</th>
<td><img alt="Screenshot of a Next.js runtime error: TypeError: Cannot read properties of undefined (reading 'includes')" src="https://user-images.githubusercontent.com/3335181/175854728-8d5c2051-1229-4da7-8af1-fc95236befae.png" /></td>
<td><img alt="Screenshot of a Next.js runtime error: Error: Failed to fetch _devPagesManifest.json. Is something blocking that network request?" src="https://user-images.githubusercontent.com/3335181/175854774-07895846-1a1c-4bb8-bf57-fa696b8c6ba4.png" /></td>
</tr>
</tbody>
</table>
2022-08-07 19:36:03 +00:00
Sukka
02c78a5c15
fix(#38743): config.runtime support template literal (#38750)
## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes #38743.
Fixes: https://github.com/vercel/next.js/pull/38750

The PR adds basic `TemplateLiteral` support for static analysis.

The corresponding re-production of #38743 has also been implemented in e2e tests.
2022-07-21 19:56:52 +00:00
Lee Robinson
5d4ac47f7a
Improve response body in Middleware error page. (#38537)
* Improve respond body in Middleware error page.

* Apply suggestions from code review

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-07-18 20:20:23 -05:00
JJ Kasper
62f3f87891
Add next.config.js validation with ajv (#38498)
* Add next.config.js validation with ajv

* update manifest

* update lib type

* remove old tests

* update to pre-build validation code

* ensure validate output is ncced

* Apply suggestions from code review

Co-authored-by: Steven <steven@ceriously.com>

* Add example of typing next.config.js

Co-authored-by: Steven <steven@ceriously.com>
2022-07-13 13:31:55 -05:00
Damien Simonin Feugas
6e2c3821cf
feat: build edge functions with node.js modules and fail at runtime (#38234)
## What's in there?

The Edge runtime [does not support Node.js modules](https://edge-runtime.vercel.app/features/available-apis#unsupported-apis).
When building Next.js application, we currently fail the build when detecting node.js module imported from middleware.

This is an blocker for using code that is conditionally loading node.js modules (based on platform/env detection), as @cramforce reported.

This PR implements a new strategy where:
- we can build such middleware/Edge API route code **with a warning**
- we fail at run time, with graceful errors in dev (console & react-dev-overlay error)
- we fail at run time, with console errors in production

## How to test?

All cases are covered with integration tests.
To try them live, create a simple app with a page, a `middleware.js` file and a `pages/api/route.js`file.
Here are iconic examples:

### node.js modules
```js
// middleware.js
import { NextResponse } from 'next/server'
// static
import { basename } from 'path'

export default async function middleware() {
  // dynamic
  const { basename } = await import('path')
  basename()
  return NextResponse.next()
}

export const config = { matcher: '/' }
```
```js
// pags/api/route.js
// static
import { isAbsolute } from 'path'

export default async function handle() {
  // dynamic
  const { isAbsolute } = await import('path')
  return Response.json({ useNodeModule: isAbsolute('/test') })
}

export const config = { runtime: 'experimental-edge' }
```

Desired error (+ source code highlight in dev):

> The edge runtime does not support Node.js 'path' module
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime

Desired warning at build time:

> A Node.js module is loaded ('path' at line 2) which is not supported in the Edge Runtime.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime

- [x]  in dev middleware, static, shows desired error on stderr
- [x]  in dev route, static, shows desired error on stderr
- [x]  in dev middleware, dynamic, shows desired error on stderr
- [x]  in dev route, dynamic, shows desired error on stderr
- [x]  in dev middleware, static, shows desired error on react error overlay
- [x]  in dev route, static, shows desired error on react error overlay
- [x]  in dev middleware, dynamic, shows desired error on react error overlay
- [x]  in dev route, dynamic, shows desired error on react error overlay
- [x]  builds middleware successfully, shows build warning, shows desired error on stderr on call
- [x]  builds route successfully, shows build warning, shows desired error on stderr on call

### 3rd party modules not found

```js
// middleware.js
import { NextResponse } from 'next/server'
// static
import Unknown from 'unknown'

export default async function middleware() {
  // dynamic
  const Unknown = await import('unknown')
  new Unknown()
  return NextResponse.next()
}
```
export const config = { matcher: '/' }
```
```js
// pags/api/route.js
// static
import Unknown from 'unknown'

export default async function handle() {
  // dynamic
  const Unknown = await import('unknown')
  return Response.json({ use3rdPartyModule: Unknown() })
}

export const config = { runtime: 'experimental-edge' }
```

Desired error (+ source code highlight in dev):

> Module not found: Can't resolve 'does-not-exist'
Learn More: https://nextjs.org/docs/messages/module-not-found

- [x]  in dev middleware, static, shows desired error on stderr
- [x]  in dev route, static, shows desired error on stderr
- [x]  in dev middleware, dynamic, shows desired error on stderr
- [x]  in dev route, dynamic, shows desired error on stderr
- [x]  in dev middleware, static, shows desired error on react error overlay
- [x]  in dev route, static, shows desired error on react error overlay
- [x]  in dev middleware, dynamic, shows desired error on react error overlay
- [x]  in dev route, dynamic, shows desired error on react error overlay
- [x]  fails to build middleware, with desired error on stderr
- [x]  fails to build route, with desired error on stderr

### unused node.js modules
```js
// middleware.js
import { NextResponse } from 'next/server'

export default async function middleware() {
  if (process.exit) {
    const { basename } = await import('path')
    basename()
  }
  return NextResponse.next()
}
```
```js
// pags/api/route.js
export default async function handle() {
  if (process.exit) {
    const { basename } = await import('path')
    basename()
  }
  return Response.json({ useNodeModule: false })
}

export const config = { runtime: 'experimental-edge' }
```

Desired warning at build time:

> A Node.js module is loaded ('path' at line 2) which is not supported in the Edge Runtime.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime

- [x]  invoke middleware in dev with no error
- [x]  invoke route in dev with no error
- [x]  builds successfully, shows build warning, invoke middleware with no error
- [x]  builds successfully, shows build warning, invoke api-route with no error

## Notes to reviewers

The strategy to implement this feature is to leverages webpack [externals](https://webpack.js.org/configuration/externals/#externals) and run a global `__unsupported_module()` function when using a node.js module from edge function's code.
For the record, I tried using [webpack resolve.fallback](https://webpack.js.org/configuration/resolve/#resolvefallback) and [Webpack.IgnorePlugin](https://webpack.js.org/plugins/ignore-plugin/) but they do not allow throwing proper errors at runtime that would contain the loaded module name for reporting.

`__unsupported_module()` is defined in `EdgeRuntime`, and returns a proxy that's throw on use (whether it's property access, function call, new operator... synchronous & promise-based styles).

However there's an issue with error reporting: webpack does not includes the import lines in the generated sourcemaps, preventing from displaying useful errors.
I extended our middleware-plugin to supplement the sourcemaps (when analyzing edge function code, it saves which module is imported from which file, together with line/column/source)

The react-dev-overlay was adapted to look for this additional information when the caught error relates to modules, instead of looking at sourcemaps.

I removed the previous mechanism (built by @nkzawa ) which caught webpack errors at built time to change the displayed error message (files `next/build/index.js`, `next/build/utils.ts` and `wellknown-errors-plugin`)
2022-07-06 20:54:44 +00:00
Emerson Laurentino
6b8e499c7b
fix: middleware-upgrade-guide.md (#38246)
* Update middleware-upgrade-guide.md

* Update middleware-upgrade-guide.md
2022-07-01 22:24:34 -05:00
Michael Novotny
c83f94cf8b
Readds missing @next/next/no-assign-module-variable ESLint rule. (#38134)
Readds `@next/next/no-assign-module-variable` ESLint rule that was inadvertently removed in #34335 during the resolution of many merge conflicts.

This PR will get us back to a good / working state. I'll see if I can add a test to ensure all rule are accounted for in a separate PR.

Fixes #34335.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
2022-06-29 02:54:47 +00:00
Edward Thomson
dd1492d3a0
Update middleware upgrade guide w/ more rationale (#38074)
* Update middleware upgrade guide w/ more rationale

The API changes for GA are general improvements.

Our middleware upgrade guide is very helpful about describing the changes
that were made and how to react to them; include some additional "why" to
rationalize these changes.

* Update middleware-upgrade-guide.md

Avoid words like "simple" and "easy"

* Update middleware-upgrade-guide.md

Clarify the issues around "security" with responses - the previous verbiage made it sound like there was a security vulnerability; there was not.

* Apply suggestions from code review

Co-authored-by: Lee Robinson <me@leerob.io>

* Apply suggestions from code review

* Update errors/middleware-upgrade-guide.md

* Update errors/middleware-upgrade-guide.md

Co-authored-by: Balázs Orbán <info@balazsorban.com>

* Apply suggestions from code review

* Update canary -> latest

* Apply suggestions from code review

Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: Balázs Orbán <info@balazsorban.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-28 10:31:47 -05:00
JJ Kasper
8e161e1855
Middleware docs update (#38111)
* Initial outline added for middleware docs

* Feedback changes - not included new user agent yet

* edge api routes docs

* middleware page some cleanup

* moved api stuff to next server, added useragent

* waitUntil example added

* Updated env vars section

* Added cookies on the request object example

* fixed naming

* fixed import typo

* Amy feedback

* Update docs/advanced-features/middleware.md

Co-authored-by: Amy Burns <amy.burns@vercel.com>

* first commit

* more fixes

* re-structuring based on feedback

* changes to middleware.md

* updates to edge-api-routes

* code typo

* Update docs/advanced-features/middleware.md

Co-authored-by: JJ Kasper <jj@jjsweb.site>

* Update edge-api-routes.md

* Update Edge Runtime API docs

* Clarify API docs

* Update

* Update docs/advanced-features/middleware.md

* Update switchable runtime docs

* Update response

* experimental-edge

* Update docs/advanced-features/middleware.md

* Add example of forwarding headers and address comments

* Add proxying headers to middleware upgrade guide

* Apply suggestions from code review

Co-authored-by: Jeff Escalante <jescalan@users.noreply.github.com>

* fix middleware directory spec

* localization explanation

* update detecting runtime

* Update docs/advanced-features/react-18/switchable-runtime.md

Co-authored-by: Balázs Orbán <info@balazsorban.com>

* fix prettier issues

* table fix

* Update docs/advanced-features/react-18/switchable-runtime.md

Co-authored-by: Jeff Escalante <jescalan@users.noreply.github.com>

* Update docs/api-routes/edge-api-routes.md

Co-authored-by: Jeff Escalante <jescalan@users.noreply.github.com>

* lint-fix

Co-authored-by: molebox <hello@richardhaines.dev>
Co-authored-by: Amy Burns <amy.burns@vercel.com>
Co-authored-by: Ismael Rumzan <ismael.rumzan@gmail.com>
Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: Balázs Orbán <info@balazsorban.com>
Co-authored-by: Jeff Escalante <jescalan@users.noreply.github.com>
Co-authored-by: Ismael <ismael@vercel.com>
2022-06-28 10:22:48 -05:00
Dom Sip
a90349d519
Update middleware-upgrade-guide to be consistent (#38102)
Update middleware-upgrade-guide.md

Ensure that before and after examples are consistent
2022-06-28 14:48:57 +02:00
Javi Velasco
0f33c0cbf5
Rephrase middleware location (#38073)
It is possible that users that are defining `src/pages` in their project confuse the "project root" to locate middleware with the actual project root instead of `src/middleware` so this PR rephrases the migration guide to make explicit that the it should be located **next** to the `pages` folder.
2022-06-27 17:13:19 +00:00
Steven Tey
8019cf002a
Added ESLint upgrade instructions to middleware upgrade guide (#38008)
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-06-27 00:20:36 +00:00
M. Zakiyuddin Munziri
2833904b6f
Fix typo for rewrites/redirects to rewrite/redirect (#38016)
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-26 16:17:34 -05:00
Lee Robinson
5646e43f83
Fix typo for rewrites to rewrite. (#38000) 2022-06-24 15:45:02 -05:00
Balázs Orbán
a590c4e7f3
docs: return response in code example (#37985)
* Update middleware-upgrade-guide.md

* lint-fix

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-24 11:13:40 -05:00
Steven
e28d03c5a4
Add experimental next/future/image component (#37927)
This PR introduces a new experimental component, `next/future/image`, which is inspired by the existing experimental `layout="raw"`.

The difference is that much of the code has been deleted in order to reduce client-side code as well as reduce complexity:

- No `layout` prop
- No `loader` config (although `loader` prop works)
- No `IntersectionObserver`, use native `loading="lazy"` 
  - No `lazyBoundary`
  - No `lazyRoot`
- No `fill` (yet) so width & height are required
- No `objectFit` (use `style` instead)
- No `objectPosition` (use `style` instead)

This improves performance because native `loading="lazy"` doesn't need to wait for React Hydration and client-side JS.

In a future PR, we will modify `next/image` to remove `layout="raw"` since this new component supersedes it.

## Feature

- [x] Integration tests added
- [x] Documentation added
- [x] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`
2022-06-24 14:56:05 +00:00
Lee Robinson
b0533635ab
Update Middleware beta message. (#37934)
To help folks find the upgrade guide.
2022-06-23 01:11:31 +00:00
Lee Robinson
f3c1bcfc2f
Change Middleware error guide from .js to .ts. (#37858)
Feedback:

> Hello :D may it be that the types should be imported in a TS file, so I think the extension of the file should be updated here :) /middleware.js -> /middleware.ts (or no types)
2022-06-20 21:56:19 +00:00
Lee Robinson
c84c7c8c3e
Fix broken link for no-script-in-document-page error. (#37831)
Going to https://nextjs.org/docs/messages/no-script-in-document-page incorrectly redirects to https://nextjs.org/errors/no-script-in-document.
2022-06-19 20:26:52 +00:00
Lee Robinson
c480726da2
Update 4MB API Routes warning error guide. (#37779)
We added documentation for this, but didn't post it back to the error guide, which is linked when you'd hit this limit.

https://github.com/vercel/next.js/pull/34700
2022-06-16 21:59:54 +00:00
Steven
b7d057453d
Add images.unoptimized: true for easy next export (#37698)
In a previous PR (#19032), we added a hard error during `next export` if the default Image Optimization API is being used because it requires a server to optimized on demand. The error message offers several different solutions but it didn't consider that by the time someone runs `next export`, they are probably done writing their app.

So if `next export` is a hard requirement, the quickest path forward is to disable Image Optimization API. So this PR adds a new configuration option to `next.config.js`:

```js
module.exports = {
  images: {
    unoptimized: true
  }
}
```

### Update
Upon further discussion, we might want to avoid doing this just for images and instead introduce a top-level config to indicate export is coming and then handle errors or warn for [unsupported features](https://nextjs.org/docs/advanced-features/static-html-export#unsupported-features).

```
module.exports = {
  nextExport: true
}
```

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-06-16 20:20:17 +00:00
Robert Cunningham
87a3c0c5bd
fix grammar in failed-loading-swc (#37765)
just a little clearer, was confused previously
2022-06-16 14:38:42 -05:00
JJ Kasper
97395b4cd6
Add missing error manifest entry and fix lint (#37758)
* Add missing error manifest entry

* fix lint

* guid -> guide
2022-06-16 11:46:15 -05:00
Lee Robinson
eb55af1cfe
Add upgrade guide for Middleware. (#37382)
* Middleware upgrade guide

* Add a bit more

* Apply suggestions from code review

Co-authored-by: Rich Haines <hello@richardhaines.dev>
Co-authored-by: Edward Thomson <ethomson@github.com>
Co-authored-by: Dominik Ferber <dominik.ferber@gmail.com>

* Apply suggestions from code review

Co-authored-by: Rich Haines <hello@richardhaines.dev>

* Updates based on PR comments

* Add before / after

* Flip paragraphs

* Apply suggestions from code review

Co-authored-by: Rich Haines <hello@richardhaines.dev>

* Apply suggestions from code review

Co-authored-by: Kiko Beats <josefrancisco.verdu@gmail.com>

* Apply suggestions from code review

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Damien Simonin Feugas <damien.feugas@gmail.com>

* Update errors/middleware-upgrade-guide.md

Co-authored-by: Dom Sip <dominik.sipowicz@gmail.com>

* Update errors/middleware-upgrade-guide.md

Co-authored-by: Damien Simonin Feugas <damien.feugas@gmail.com>

* Update middleware-upgrade-guide.md

* Update errors/middleware-upgrade-guide.md

Co-authored-by: Rich Haines <hello@richardhaines.dev>

* Update errors/middleware-upgrade-guide.md

Co-authored-by: Rich Haines <hello@richardhaines.dev>

* Apply suggestions from code review

Co-authored-by: Rich Haines <hello@richardhaines.dev>

* Apply suggestions from code review

* Update errors/middleware-upgrade-guide.md

Co-authored-by: Rich Haines <hello@richardhaines.dev>
Co-authored-by: Edward Thomson <ethomson@github.com>
Co-authored-by: Dominik Ferber <dominik.ferber@gmail.com>
Co-authored-by: Kiko Beats <josefrancisco.verdu@gmail.com>
Co-authored-by: Damien Simonin Feugas <damien.feugas@gmail.com>
Co-authored-by: Dom Sip <dominik.sipowicz@gmail.com>
2022-06-16 09:29:18 -07:00
Seiya Nuta
0bf9233e14
[middleware] Warn dynamic WASM compilation (#37681)
In Middlewares, dynamic code execution is not allowed. Currently, we warn if eval / new Function are invoked in dev but don't warn another dynamic code execution in WebAssembly.

This PR adds warnings for `WebAssembly.compile` and `WebAssembly.instantiate` with a buffer parameter (note that `WebAssembly.instantiate` with a **module** parameter is legit) invocations. Note that other methods that compile WASM dynamically such as `WebAssembly.compileStreaming` are not exposed to users so we don't need to cover them.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-06-16 14:59:30 +00:00
Andrei Stefan
6b829d8bd6
fix(eslint): allow <img> in conjunction with <picture> (#37504) (#37570)
* fix(eslint): allow <img> in conjunction with <picture> (#37504)

* Apply suggestions from code review

* add space

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-15 21:13:52 -05:00
Michael Novotny
5211ac5cae
Adds consistency to ESLint rules. (#34335)
* Adds consistency to ESLint rules.

* Fixes lint errors.

* Fixes manifest.

* Adds missing title.

* Fixes copy / paste error.

Co-authored-by: Lee Robinson <me@leerob.io>

* Update errors/no-script-in-document.md

Co-authored-by: Lee Robinson <me@leerob.io>

* Update errors/no-sync-scripts.md

Co-authored-by: Lee Robinson <me@leerob.io>

* Updates a couple of rule descriptions.

* Adds redirects.

* Fixes unit tests.

* Removes duplicated section.

* Updates `no-before-interactive-script-outside-document` description.

* Fixes lint.

* Fixes integration tests.

* Adds description to `no-before-interactive-script-outside-document` documentation.

* Removes `link-passhref` from rules list.

* Updates remaining `pages/_middleware.js` references.

* Adds consistancy to messaging in new `no-styled-jsx-in-document` rule.

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-13 21:17:42 -05:00
Splatterxl
e0631d0ef1
Fix links in google-font-display error (#37661) 2022-06-13 10:17:36 -05:00
Houssein Djirdeh
2eb48e7837
[Script] Updates stale no-script-in-document-page error doc (#37568)
[Recent changes](https://github.com/vercel/next.js/pull/31936) to `next/script` introduced the capability to use `beforeInteractive` scripts in the custom document. This old error message is incorrect.
2022-06-13 03:04:15 +00:00
Joe Previte
6108f10799
feat(next export): add warning if using getInitialProps (#37642)
This PR builds on the work from @hattakdev (PR went stale: https://github.com/vercel/next.js/pull/14499) and adds a new `integration` test for the new warning.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

![image](https://user-images.githubusercontent.com/3806031/173214117-f5159f3a-778c-4177-894d-78e7bb0c80e7.png)

## To run locally
1. `pnpm build` 
2. `pnpm testonly test/integration/export-getInitialProps-warn/test/index.test.js`

Fixes #13946

## Notes

<details>
<summary>Click to toggle see</summary>

I know the `contributing.md` doc said to avoid adding new tests to `integration`. It also said new tests should be written in TypeScript.

I wasn't sure where to put the tests for this so I went with `integration`. I also didn't see many other tests written in TS in this part of the codebase so I stuck with `.js`. 

</details>

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-06-13 02:34:23 +00:00
Kiko Beats
76083210ed
Middleware: remove req.ua (#37512)
This PR moves the internal logic associated with `req.ua` into an explicit method the user should to call to have the same behavior.

**before**

```typescript
// middleware.ts
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
  const url = request.nextUrl
  const viewport = request.ua.device.type === 'mobile' ? 'mobile' : 'desktop'
  url.searchParams.set('viewport', viewport)
  return NextResponse.rewrites(url)
}
```

**after**

```typescript
// middleware.ts
import { NextRequest, NextResponse, userAgent } from 'next/server'
export function middleware(request: NextRequest) {
  const url = request.nextUrl
  const { device } = userAgent(request)
  const viewport = device.type === 'mobile' ? 'mobile' : 'desktop'
  url.searchParams.set('viewport', viewport)
  return NextResponse.rewrites(url)
}
```

This potentially will save the extra 17 kB related to the size of `ua-parser-js`
2022-06-09 11:10:21 +00:00
Kiko Beats
129052dc98
Edge Functions: deprecate access to request.page (#37349)
It uses `URLSearchParams` to have the same behavior.

closes https://github.com/vercel/next.js/issues/34521
2022-06-01 20:06:37 +00:00
Lee Robinson
1f47d0b34b
Update nested Middleware error reference (#37368) 2022-06-01 10:09:11 -05:00
Jiachi Liu
c984281f90
docs: fix usage in middleware errors (#37364)
## Documentation

- [x] Make sure the linting passes by running `pnpm lint`
2022-06-01 12:03:07 +00:00
Dilwoar Hussain
1cd34dd640
Use appropriate className attribute for JSX (#37168)
## What

This changes the` class` attribute with `className`

## Why

Class is a reserved word in JavaScript. The appropriate attribute to use is `className`.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-05-24 23:15:06 +00:00
Alexandru Tasica
e4bf1a515c
Docs: missing parenthesis on example (#37156)
Missing close parenthesis on docs example from page custom-document-image-import.md

## Bug

- [x] Related issues linked using `fixes #37155`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-05-24 15:01:56 +00:00
Sharath Challa
11ad65e445
Add eslint rule for not allowing styled-jsx in _document.js (#32678)
## Bug

- [ ] Related issues linked
fixes #32656



Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-05-23 02:32:13 +00:00
Hannes Bornö
318664562c
next-swc: Improve error message for nested styled jsx tags (#33805)
Before
![image](https://user-images.githubusercontent.com/25056922/151694166-a27a35b6-5fb4-4179-8c88-bd98ede35fea.png)

After
![image](https://user-images.githubusercontent.com/25056922/151758033-65576e5b-e2e8-435d-87b3-9e384b13936b.png)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-05-22 22:45:58 +00:00
Damien Simonin Feugas
bf089562c7
feat(middleware)!: forbids middleware response body (#36835)
_Hello Next.js team! First PR here, I hope I've followed the right practices._

### What's in there?

It has been decided to only support the following uses cases in Next.js' middleware:
- rewrite the URL (`x-middleware-rewrite` response header)
- redirect to another URL (`Location` response header)
- pass on to the next piece in the request pipeline (`x-middleware-next` response header)

1. during development, a warning on console tells developers when they are returning a response (either with `Response` or `NextResponse`).
2. at build time, this warning becomes an error.
3. at run time, returning a response body will trigger a 500 HTTP error with a JSON payload containing the detailed error.

All returned/thrown errors contain a link to the documentation.

This is a breaking feature compared to the _beta_ middleware implementation, and also removes `NextResponse.json()` which makes no sense any more.

### How to try it?
- runtime behavior: `HEADLESS=true yarn jest test/integration/middleware/core`
- build behavior : `yarn jest test/integration/middleware/build-errors`
- development behavior: `HEADLESS=true yarn jest test/development/middleware-warnings`

### Notes to reviewers

The limitation happens in next's web adapter. ~The initial implementation was to check `response.body` existence, but it turns out [`Response.redirect()`](https://github.com/vercel/next.js/blob/canary/packages/next/server/web/spec-compliant/response.ts#L42-L53) may set the response body (https://github.com/vercel/next.js/pull/31886). Hence why the proposed implementation specifically looks at response headers.~
`Response.redirect()` and `NextResponse.redirect()` do not need to include the final location in their body: it is handled by next server https://github.com/vercel/next.js/blob/canary/packages/next/server/next-server.ts#L1142

Because this is a breaking change, I had to adjust several tests cases, previously returning JSON/stream/text bodies. When relevant, these middlewares are returning data using response headers.

About DevEx: relying on AST analysis to detect forbidden use cases is not as good as running the code.
Such cases are easy to detect:
```js
new Response('a text value')
new Response(JSON.stringify({ /* whatever */ })
```
But these are false-positive cases:
```js
function returnNull() { return null }
new Response(returnNull())

function doesNothing() {}
new Response(doesNothing())
```
However, I see no good reasons to let users ship middleware such as the one above, hence why the build will fail, even if _technically speaking_, they are not setting the response body. 



## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2022-05-19 22:02:20 +00:00
Javi Velasco
f354f46b3f
Deprecate nested Middleware in favor of root middleware (#36772)
This PR deprecates declaring a middleware under `pages` in favour of the project root naming it after `middleware` instead of `_middleware`. This is in the context of having a simpler execution model for middleware and also ships some refactor work. There is a ton of a code to be simplified after this deprecation but I think it is best to do it progressively.

With this PR, when in development, we will **fail** whenever we find a nested middleware but we do **not** include it in the compiler so if the project is using it, it will no longer work. For production we will **fail** too so it will not be possible to build and deploy a deprecated middleware. The error points to a page that should also be reviewed as part of **documentation**.

Aside from the deprecation, this migrates all middleware tests to work with a single middleware. It also splits tests into multiple folders to make them easier to isolate and work with. Finally it ships some small code refactor and simplifications.
2022-05-19 15:46:21 +00:00
Jiachi Liu
ed4d009841
Drop the unstable web vital hook and remove exports of flush effects (#36912)
* remove the experimental web vital hook api
* remove the exported flush effects api and only error on development, keep only usage to styled-jsx

for web vital hook API: The usage is not widly adopted since the existing exported vital api could do the same work. In the future we'll deprecate the `_app.server` in favor of `_app` in server component pages. so that this api won't be required.

for flush effects api: other css-in-js libs are not using the same approach like styled-jsx which holding a style registry and could flush it during streaming. emotion-js and styled-components are still relying on `Document.getInitialProps` atm and we have supported it in latest canary
2022-05-14 21:20:24 +00:00
Hunter Tunnicliff
521f88e159
Move CI build cache instructions to Deployment docs (#35418)
This PR moves the instructions for configuring CI providers to preserve Next.js's build cache out of the _No Cache Detected_ error message and into ~the _Deployment_~ a new page within the _Advanced Features_ docs. 

This change is beneficial because it makes the CI configuration examples visible in the context of other helpful documentation for deploying Next.js.

👉 **Prior to this change, these CI configuration examples didn't show up in the Next.js docs.**

The relocated instructions are now linked on the _No Cache Detected_ error message page.

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`

Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: Steven <steven@ceriously.com>
2022-05-13 09:56:47 -04:00
Erik Brinkman
c7b2083f2e
Add experimental flag to force SWC transforms (#36789)
fixes #36763
fixes #36590

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
   - It hasn't been accepted for implementation, although that process isn't clear, and this is a pretty trivial fix.
- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
   - This is somewhat inherent in the error log
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2022-05-10 10:33:31 +00:00
Steven
da8d1984d2
Add experimental wildcard remotePatterns config for upstream images (#36245)
## Description 
This PR implements a new configuration object in `next.config.js` called `experimental.images.remotePatterns`.

This will eventually deprecate `images.domains` because it covers the same use cases and more by allowing wildcard pattern matching on `hostname` and `pathname` and also allows restricting `protocol` and `port`.

## Feature

- [x] Implements an existing feature request.
- [x] Related issues linked
- [x] Unit tests added
- [x] Integration tests added
- [x] Documentation added
- [x] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

## Related 

- Fixes #27925 
- Closes #18429 
- Closes #18632
- Closes #18730
- Closes #27345
2022-05-05 02:19:16 +00:00
LongYinan
2012f4e7c6
Update components in GitHub Actions (#36669)
`build-native` jobs tested on https://github.com/vercel/next.js/runs/6288266017
2022-05-04 18:51:25 +00:00
Houssein Djirdeh
fd2ba11763
Adds inline script functionality to next/script for worker and beforeInteractive strategies (#36364)
Adds inline script functionality to `next/script` for `worker` and `beforeInteractive` strategies. 

- fixes #36318 
- fixes #26343
- fixes #26591
- fixes #26343
- fixes #26240


Co-authored-by: Janicklas Ralph <6142074+janicklas-ralph@users.noreply.github.com>
2022-04-29 15:20:31 +00:00
JJ Kasper
4f5c2deb4c
Add note about lockfile causing swc to fail to load (#36484)
* Add note about lockfile causing swc to fail to load

* Apply suggestions from code review

Co-authored-by: Maia Teegarden <dev@padmaia.rocks>
2022-04-26 14:48:52 -05:00
Jiachi Liu
d5e767b20d
Add process env NEXT_RUNTIME (#36383)
* To help determine the current running environment is in server nodejs / edge runtime
* Remove usage of `process.browser`
2022-04-26 17:54:28 +00:00
Maedah Batool
60488e68e3
Fix broken data fetching links in docs (#33766)
* Fix broken links for Data Fetching

* Fix link data fetching doc

* Improve link file paths

* Update errors/large-page-data.md

Co-authored-by: Steven <steven@ceriously.com>

* Update links

* Fix linting

* Lint tests

* Lint tests

Co-authored-by: Steven <steven@ceriously.com>
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2022-04-22 14:03:07 +02:00
Janicklas Ralph
0441f816a6
Changes to the beforeInteractive strategy to make it work for streaming (#31936)
Changes to the beforeInteractive strategy to make it work for streaming

Splitting `beforeInteractive` into two strategies `beforeInteractive` at the _document level and `beforePageRender` for page level <Scripts>
2022-04-21 21:15:53 +00:00
Michael Ward
1be5f36123
Adds linting rule to avoid assignment to 'module' variable. Fixes #34859 (#35279) 2022-04-15 14:45:53 +02:00
Vlad Tansky
a11479bad9
docs: failed-loading-swc possible fix for Windows users (#36081)
Apparently, Microsoft Visual C++ Redistributable is required for Windows users. 

> The issue is because the package is failing due to a missing library on the computer. fs-search requires Microsoft Visual C++ 2015 Redistributable (x64).
> You can get the download here -> https://aka.ms/vs/17/release/vc_redist.x64.exe

from https://stackoverflow.com/questions/69859120/the-specified-module-could-not-be-found-d-next-js-firstapp-node-modules-n



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-04-12 18:21:11 +00:00
Samuel
cb565d69cf
Update: Add appropriate error message when for getServerSideProps invalid return value (#35887) 2022-04-06 10:25:27 -05:00
Samuel
739e6f0760
Docs: Add useful link to invalid-getstaticprops-value error message (#35915)
## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`

I would like to add useful link to `getStaticProps` invalid return value error message as currently there is no useful link being provided. I believe that this might improve the developer experience.
2022-04-05 18:59:58 +00:00
JJ Kasper
b1a7b88890
Add warning when importing "next" directly (#35884)
* Add warning when importing "next" directly

* Apply suggestions from code review

Co-authored-by: Balázs Orbán <info@balazsorban.com>

* Apply suggestions from code review

Co-authored-by: Steven <steven@ceriously.com>

* add example

Co-authored-by: Balázs Orbán <info@balazsorban.com>
Co-authored-by: Steven <steven@ceriously.com>
2022-04-05 10:57:45 -05:00
Jon Espen Kvisler
127bbc0ba6
Update dead link for getStaticPaths in docs (#35814) 2022-04-01 11:51:18 +02:00
Jiachi Liu
27d23f5b7b
docs: update styling support for streaming (#35375)
## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2022-03-26 20:19:21 +00:00
JJ Kasper
72478c5c80
Update next/link error when no children are provided (#35453)
* Update next/link error when no children are provided

* update manifest

* Apply suggestions from code review

Co-authored-by: Balázs Orbán <info@balazsorban.com>
2022-03-22 13:58:55 -05:00
Sacha
b579a35e83
[examples] Update styled-components to use SWC (#34770)
* rename example with-styled-components to with-styled-components-babel

* update with-styled-components references

* update docs on react hydratation erros for styled components

* clone with-styled-components-babel without babel config

* add styledComponents to compiler options in next.config

* replace babel dependency with Next.js SWC compiler

* upgrade example typescript version

example with-typescript-styled-components had a typescript dependency
lower than the recommended version. Upgraded from 4.2.4 to 4.5.5.

* Apply suggestions from code review

* update readme for with-styled-components-babel

remove Notes section since it describes a workaround which is no longer
required to work with `next/link`.

Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: Balázs Orbán <info@balazsorban.com>
2022-03-02 03:43:34 +01:00
JJ Kasper
35a1af4cf4
Add redirect for error doc rename (#34791) 2022-02-25 11:09:04 -08:00
Colin Rice
079b507327
Add API config to allow disabling response size warning (#34700)
Adds an API config option that disables warning a user when their API response body is over 4 megs. This has been added for users who'd like to stream larger amounts of data from their API acknowledging the drawbacks. This config mirrors the existing [`externalResolver` config](https://nextjs.org/docs/api-routes/api-middlewares#custom-config).

Closes: [#33162](https://github.com/vercel/next.js/issues/33162)

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-02-25 02:04:02 +00:00
mxgnus
7d98537f95
added styled-components & replay support in swc (#34647)
styled-components & relay is supported in swc since nextjs 12.1
2022-02-21 12:28:38 -06:00
Lionel
4ad1c5a2bc
Update wrong code snippet (#34520)
Following the [example](https://nextjs.org/docs/advanced-features/custom-document), avoiding the following error:
>  Identifier 'Document' has already been declared



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-02-18 10:03:27 +00:00
Gerald Monaco
944c734d04
Add unstable_useFlushEffects hook (#34117)
Implements https://github.com/vercel/next.js/issues/30997 with some minor tweaks to the design:

* The hook is moved to Client Components (e.g. `pages/_app` instead of `pages/_document`). This was a silly oversight in the original design: the hook needs to be called during server prerendering.

* `useFlushEffects` instead of `useFlushEffect` as there isn't a particularly safe way to implement the singular semantics as a Client Component hook given the current implementation of server rendering.

---

Fixes #30997
2022-02-18 00:18:28 +00:00
JJ Kasper
d4eea7593a
Fix snippet language sh -> bash (#34487) 2022-02-17 11:05:40 -06:00
Gal Schlezinger
f0f322c0d1
Remove deprecation for relative URL usage in middlewares (#34461)
* Remove deprecation for relative URL usage in middlewares

* fix tests

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-02-17 09:12:36 -06:00
Steven
8a55612c0d
Add image config for dangerouslyAllowSVG and contentSecurityPolicy (#34431)
## Feature

- [x] Integration tests added
- [x] Documentation added
- [x] Errors have helpful link attached, see `contributing.md`



Co-authored-by: Lee Robinson <9113740+leerob@users.noreply.github.com>
2022-02-16 19:28:22 +00:00
Balázs Orbán
d88793d973
feat: improve opening a new issue flow (#34434)
Ref: [Slack thread](https://vercel.slack.com/archives/CGU8HUTUH/p1645003443213449)

When opening a new issue, it is desired that the user has checked if the `canary` release not already have fixed their issue, since we do not backport fixes to previous Next.js versions.

- added a CLI warning when `next info` runs that looks like this:

![image](https://user-images.githubusercontent.com/18369201/154309275-01ccc979-01e5-4ccb-8a22-5deab64765a0.png)

This links to a message docs page with more information and some useful links.

- refactored our bug report templates to be more clear and removed the fields that are now unnecessary (since running `next info` is expected to run on releases that already have it)

- Made browser/deployment optional, as in most cases those fields are irrelevant. We still ask them, but mention that they are only needed if relevant.

- Asking for the exact browser version now

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-02-16 17:45:01 +00:00
Rich Haines
127f94dc13
Updated failed to load error page to include info about node versions (#34362)
This PR updates the [failed loading swc](https://nextjs.org/docs/messages/failed-loading-swc) error page with information about switching node version and installing deps again.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-02-15 15:44:10 +00:00
Ben Ilegbodu
41ecd5e6e2
docs: Add link to pageExtensions config in page-without-valid-component.md (#34285)
The [Page Without Valid React Component](https://nextjs.org/docs/messages/page-without-valid-component) explains some ways to fix the problem of Next finding a non-React Component. However, if someone is trying to use `.mdx` as an extension, or wants to include tests, stories or other non-page files in their `pages` folder there's nothing for them.

The [Custom Page Extensions](https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions) has all the info to help them out, but adding it to this error page should make it more discoverable.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2022-02-14 17:25:45 +00:00
Hassan El Mghari
ca8530ffc8
Changed all occurrences of etc to match (#34280)
The [correct](https://www.grammarly.com/blog/et-cetera-etc/) way to use et cetera is to put a period right after and a comma behind it if it's being used as a list. I updated the occurrences in the docs and examples that didn't match these rules.
2022-02-13 00:22:52 +00:00
Hassan El Mghari
26c40f3186
Add heading to invalid-api-status-body error (#34150) 2022-02-09 21:01:58 -06:00
Maia Teegarden
01ee7e0b22
Chore/stable swc compiler options (#34074)
Mark some previously experimental swc compiler options as stable under a new `compiler` option

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-02-10 01:54:28 +00:00
Tim Neutkens
c74e4f21ff
Add support for async fn / promise in next.config.js/.mjs (#33662)
- Add support for async function / promise export in next.config.js/.mjs
- Update docs

Adds support for https://twitter.com/timneutkens/status/1486075973204422665

But also the simpler version:

```js
module.exports = async () => {
  return {
    basePath: '/docs'
  }
}
```



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-02-07 08:48:35 +00:00
Chris Patterson
74a65332a4
Changing GitHub Actions cache documentation (#28228)
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-02-06 19:01:38 +00:00
Lakshya Thakur
8039c6b2b5
fix: Change url to nextUrl inside delete-query-params-in-middlewares.md (#33796)
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-02-06 09:21:28 -06:00
Krychaxp
3055a82ede
Add note about dns-prefetch as fallback (#30385)
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-02-06 02:53:26 +00:00
Kara
24fe279751
Warn in dev mode when stylesheets are added using next/head (#34004)
This commit adds a development mode warning in the console
if you try to include <link rel="stylesheet"> tags in
next/head, e.g.

```
<Head>
  <link ref="stylesheet" href="..." />
</Head>
```

The warning message explains that this pattern will not
work well with Suspense/streaming and recommends using a
custom Document component instead.

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-02-04 22:48:30 +00:00
Jiachi Liu
4812e22992
docs: use function for components in general (#33990) 2022-02-04 09:18:35 -06:00
Kara
61ea8efe42
Warn in dev mode when script tags are added with next/head (#33968)
This commit adds a development mode warning in the console
if you try to include <script> tags in next/head, e.g.

```
<Head>
  <script async src="..." />
</Head>
```

The warning message explains that this pattern will not
work well with Suspense/streaming and recommends using the
next/script component instead.

TODO in follow-up PR: add same warning for stylesheets, etc

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Integration tests added
- [x] Documentation added
- [x] Errors have helpful link attached, see `contributing.md`
2022-02-04 13:06:55 +00:00
Balázs Orbán
b7725133f8
docs: recommend .end instead of .send when no body is being sent (#33611)
Fixes #33536

I was thinking to make `body` optional here instead:

2c6bd1ed1b/packages/next/shared/lib/utils.ts (L262)

But that might result in more errors when not setting the status code correctly. So it's the easiest for us to just recommend `.end()` which `.send()` uses when there is no argument.

c71465d87a/packages/next/server/api-utils.ts (L269)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-02-03 02:41:28 +00:00
Rich Haines
138361208c
Fixed duplicate data fetching overview page + links (#33774)
This PR removes the duplicate overview link for data fetching, and fixes the corresponding links

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`


Co-authored-by: Lee Robinson <9113740+leerob@users.noreply.github.com>
Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-01-29 02:52:40 +00:00
Gal Schlezinger
7b2ea48745
Allow to delete URL search params in middleware rewrites (#33725)
This allows to have a more streamlined API for rewrites.
Since middlewares are code and not static configurations, we can allow people to
delete query params and not just overwrite them.

**⚠️ Warning ⚠️** this is a breaking change in query parameter behavior with middlewares, but the API will make more sense now. Compare the following:

```diff
  import {NextResponse} from 'next/server'
  export default function middleware({ nextUrl }) {
-   nextUrl.searchParams.set('ignored-query-param', '');
+   nextUrl.searchParams.delete('ignored-query-param'); // 🎉
    return NextResponse.rewrite(nextUrl);
  }
```

Since this is breaking change, we're adding a warning every time we find a query param that is missing, and eventually--the warning will be deleted.
I don't think we need to worry about older versions of Next.js as the current behavior is misleading: merging query parameters make sense for static rewrites, but not for middlewares where you have access to all the query parameters and can manipulate them freely.

Related:
* This is the opposite of #33454
* This supersedes #33574
2022-01-27 22:06:39 +00:00
Karl Horky
5c6c385100
Use relative path for example (#33565)
The paths starting with slashes in the ESLint config examples are absolute, which are almost never the correct configuration for a project. Using a relative path is a much more common configuration, and leads to much less debugging why these examples don't work.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-01-26 12:41:14 +00:00
Baoshuo Ren
72658c918a
fix(errors/no-cache): netlify-plugin-cache-nextjs has been deprecated (#33629) 2022-01-25 14:59:32 +01:00
Javi Velasco
e5dee17f77
Enforce absolute URLs in Edge Functions runtime (#33410)
We currently have inconsistencies when working with URLs in the Edge Functions runtime, this PR addresses them introducing a warning for inconsistent usage that will break in the future. Here is the reasoning.

### The Browser

When we are in a browser environment there is a fixed location stored at `globalThis.location`. Then, if one tries to build a request with a relative URL it will work using that location global hostname as _base_ to construct its URL. For example:

```typescript
// https://nextjs.org
new Request('/test').url; // https://nextjs.org/test
Response.redirect('/test').headers.get('Location'); // https://nextjs.org/test
```

However, if we attempt to run the same code from `about:blank` it would not work because the global to use as a base `String(globalThis.location)` is not a valid URL. Therefore a call to `Response.redirect('/test')` or `new Response('/test')` would fail.

### Edge Functions Runtime

In Next.js Edge Functions runtime the situation is slightly different from a browser. Say that we have a root middleware (`pages/_middleware`) that gets invoked for every page. In the middleware file we expose the handler function and also define a global variable that we mutate on every request:

```typescript
// pages/_middleware

let count = 0;

export function middleware(req: NextRequest) {
  console.log(req.url);
  count += 1;
}
```

Currently we cache the module scope in the runtime so subsequent invocations would hold the same globals and the module would not be evaluated again. This would make the counter to increment for each request that the middleware handles. It is for this reason that we **can't have a global location** that changes across different invocations. Each invocation of the same function uses the same global which also holds primitives like `URL` or `Request` so changing an hypothetical `globalThis.location` per request would affect concurrent requests being handled.

Then, it is not possible to use relative URLs in the same way the browser does because we don't have a global to rely on to use its host to compose a URL from a relative path.

### Why it works today

We are **not** validating what is provided to, for example, `NextResponse.rewrite()` nor `NextResponse.redirect()`. We simply create a `Response` instance that adds the corresponding header for the rewrite or the redirect. Then it is **the consumer** the one that composes the final destination based on the request. Theoretically you can pass any value and it would fail on redirect but won't validate the input.

Of course this is inconsistent because it doesn't make sense that `NextResponse.rewrite('/test')` works but `fetch(new NextRequest('/test'))` does not. Also we should validate what is provided. Finally, we want to be consistent with the way a browser behaves so `new Request('/test')` _should_ not work if there is no global location which we lack.

### What this PR does

We will have to deprecate the usage of relative URLs in the previously mentioned scenarios. In preparation for it, this PR adds a validation function in those places where it will break in the future, printing a warning with a link that points to a Next.js page with an explanation of the issue and ways to fix it.

Although middleware changes are not covered by semver, we will roll this for some time to make people aware that this change is coming. Then after a reasonable period of time we can remove the warning and make the code fail when using relative URLs in the previously exposed scenarios.
2022-01-19 15:10:25 +00:00
Mosaad
2518a5a4c7
docs: fix url (#33409) 2022-01-18 00:26:24 +01:00
Steven
7f6afa4a89
Update links in next export + next/image error message (#33317)
Minor changes to this error message to link to latest docs
2022-01-14 15:19:52 +00:00
Rich Haines
d291aa9134
Refactor data fetching API docs (#30615)
Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: Balázs Orbán <info@balazsorban.com>
2022-01-12 08:56:51 +01:00
JJ Kasper
57a87050e7
Add util for normalizing errors (#33159)
* JSON.stringify generic errors

* Add util for normalizing errors

* lint-fix

* Add better error for null case as well

Co-authored-by: Michael Ozeryansky <mozeryansky@users.noreply.github.com>
2022-01-11 14:40:03 -06:00
JJ Kasper
efabf81e23
Add util for generating new tests/error documents (#33001)
* Add util for generating new tests/error documents

* update compiled

* lint-fix

* apply suggestions

* update wording

* update compiled
2022-01-06 09:45:04 -06:00
Houssein Djirdeh
d72f5bd69d
[ESLint] Adds lint rule to flag usage of <head> (#32897)
## Bug

- [X] Related issues linked using `fixes #number`
- [X] Tests added
- [X] Errors have helpful link attached, see `contributing.md`

Fixes #30142
2021-12-31 05:09:44 +00:00