Commit graph

7781 commits

Author SHA1 Message Date
Ngô Đức Anh
c897933b6e
fix(typedRoutes): added missing anchor props to LinkRestProps (#51977)
### What?
This PR fixes `next/link`'s `<Link />` missing many `<a />` props when
`experimental.typedRoutes` is enabled.

### How?
It does that by changing `AnchorHTMLAttributes<HTMLAnchorElement>` in
LinkRestProps to
`DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>,
HTMLAnchorElement>`, which contains all `<a />` props.

Fixes #51907

---------

Co-authored-by: Shu Ding <g@shud.in>
2023-06-29 14:24:44 +02:00
vercel-release-bot
68075376f0 v13.4.8-canary.9 2023-06-29 11:27:00 +00:00
Shu Ding
922a45839a
Optimize next-app-loader file resolution (#51924)
This PR changes `fs.stat` calls (to check if a file exists or not) to be
aggregated as `fs.readdir` calls and cached during the entire loader
pass. This is because we have a huge amount of file to check, but they
are always in a small amount of directories.

For example, a route that's 5-directory deep (`/a/b/c/d/page.js`) can
have up to 4,000 special segments and metadata files to check. However
they're always under these 5 directories. So instead of 4,000 `fs.stat`
calls let's just do 5 `fs.readdir` calls.

Didn't measure it carefully but a quick local test shows a 20~30%
improvement.

<details>
<summary>Another idea for future improvements</summary>
Currently, we create a MissingModuleDependency for any combination of
possible metadata filename. But in reality, we only need to track them
incrementally by index. For example if `icon1.js` is missing, it's kinda
waste to still track `icon2.js`~`icon9.js` because `icon1.js` will be
added first. This change should at least reduce the number of file
watchers by 80% and the initial compilation time by 0.5~1s, depending on
the actual route.
</details>

---------

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2023-06-29 13:21:32 +02:00
Tim Neutkens
b236670f97
Fix entrypoints.get and entrypoints.stream from turbopack (#50733)
## What?

Both `entrypoints.get` and `entrypoints.stream` threw an error because it was using the wrong tasks implementation. @sokra helped fix it.
2023-06-29 09:40:02 +00:00
Olivier Tassinari
60d27380f7
fix modularizeImports with @mui/material (#51953)
Fixes #51872. We were exploring in
https://github.com/mui/material-ui/pull/35457 the option to move the
`modularizeImports` config to our Next.js examples to fix
https://github.com/mui/material-ui/issues/35450 however, we never got to
complete the work.

We are not yet in a position where we can apply modularizeImports to
`@mui/material`. We still have folders to create to make it work.

Maybe we should close https://github.com/mui/material-ui/pull/35457?

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-06-28 20:26:04 -07:00
Steven
946c9c5c46
Add unstable_getImgProps export from next/image (#51205)
### Description 

This PR refactors the Image component so that the core logic can be consolidated into a single function.

This allows usage outside of `<Image>`, such as:

1. Working with [`background-image`](https://developer.mozilla.org/en-US/docs/Web/CSS/background-image) or [`image-set`](https://developer.mozilla.org/en-US/docs/Web/CSS/image/image-set)
2. Working with canvas [`context.drawImage()`](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images) or simply `new Image()`
3. Working with [`<picture>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture) media queries to implement Art Direction or Light/Dark Mode images

### Example

```tsx
import { unstable_getImgProps as getImgProps } from 'next/image'

export default function Page() {
  const common = { alt: 'Hero', width: 800, height: 400 }
  const { props: { srcSet: dark } } = getImgProps({ ...common, src: '/dark.png' })
  const { props: { srcSet: light, ...rest } } = getImgProps({ ...common, src: '/light.png' })

  return (<picture>
  <source media="(prefers-color-scheme: dark)" srcSet={dark} />
  <source media="(prefers-color-scheme: light)" srcSet={light} />
  <img {...rest} />
</picture>)
}
```

### Related

fix NEXT-257

Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-06-29 01:35:08 +00:00
Mrxbox98
bd1fc78bd8
Add cpu-features to server external packages (#51946)
<!-- 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(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

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

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

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


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
### What?
Adds [cpu-features](https://www.npmjs.com/package/cpu-features) to
external server packages.

### Why?
To prevent developers from having to add ``cpu-features`` to their next
config.
2023-06-28 17:52:04 -07:00
Jiachi Liu
3fb41561c4
Support react profiling option for app dir client components (#51947)
`reactProductionProfiling` was a next config working for pages before mainly for profiling react, this PR also enables it for pages.

### Chanegs

* Add react profiling entry and related alias
* Fix `reactProductionProfiling` is missing in next config type and schema

Closes #51131
2023-06-29 00:00:11 +00:00
Josh Goldberg ✨
0bd9b4bbdf
fix(next): improve error for using <Html> outside of document (#45056)
## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added -> this is a pretty niche edge case, do
you want me to?
- [x] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

Fixes #45024

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-06-28 15:40:35 -07:00
Jacob Fletcher
d206c68ad7
adds payload to server-external-packages.json (#51933)
### What?
Adds `payload` to the external package list

### Why?

To prevent developers using
[Payload](https://github.com/payloadcms/payload) modules within Next.js
from having to add this in their next config.
2023-06-28 13:03:08 -07:00
Justin Ridgewell
525ecf4158
Ensure edge runtime Pages API/App Route handlers AbortSignal aborts on client disconnect (#51727)
### What?

This ensures the `request.signal` `AbortSignal` that every Pages API and App Route handler receives aborts when the client disconnects.

### Why?

Now that we [support cancelling](https://github.com/vercel/next.js/pull/51594) responses mid-stream, it's important that we can communicate that abort to developer code. Eg, for AI endpoints that have an streaming `fetch` connection to the some 3p AI service, it's important that they're able to abort that stream when the client's browser disconnects.

### How?

Just need to listen for `error` events on Node's `IncomingMessage` request object, and forward that to an `AbortController`. After that, propagate the signal through the server to web-style handlers.
2023-06-28 16:50:45 +00:00
Sukka
e7a692537c
Reduce the client bundle size of App Router (#51806)
After migrating a Next.js app from Pages Router to App Router and using as many RSC as possible, I notice that the client js bundle size actually increases by 5%. It turns out that Next.js has introduced a lot of code to the client bundle.

<img width="1354" alt="image" src="https://github.com/vercel/next.js/assets/40715044/c7216fee-818b-4593-917e-bf0d2a00967a">

The PR is an attempt to reduce the client bundle size.
2023-06-28 13:32:10 +00:00
vercel-release-bot
e33b87d894 v13.4.8-canary.8 2023-06-28 09:04:46 +00:00
Shu Ding
ba252d8765
Simplify module context invalidation (#51905)
It _might_ be an overhead to send over a large amount of data over
`postMessage` just for diff. HMR and module compilation is way less
frequent than evaluation in the sandbox, so maybe we should always
invalidate the cache eagerly without any comparison and defer the
workload to `evaluateInContext`.

Some observations:

<img width="662"
src="https://github.com/vercel/next.js/assets/3676859/12136002-8857-4e10-ba9a-bf420d098dc8">

<img width="662" alt="CleanShot 2023-06-28 at 08 26 23@2x"
src="https://github.com/vercel/next.js/assets/3676859/db054948-a0ea-4d98-bf4f-aa125e0eb1b5">

![CleanShot 2023-06-28 at 08 02
24@2x](https://github.com/vercel/next.js/assets/3676859/b674beb1-d53c-4a1d-a9bf-ca3672e089c8)
2023-06-28 11:00:39 +02:00
Jimmy Lai
5b883bb5d1
performance: enable minification for the server bundles (#51831)
This PR makes the server code be minified.

## Why

This will improve performance of the server code because of the lesser
size, lesser time to parse and lesser code via tree sharking.

Cons: this should lead to increased build times, so a
`disableServerMinification` config was added


<!-- 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(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

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

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

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


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

link NEXT-1319
2023-06-28 09:08:17 +02:00
vercel-release-bot
2c856cb256 v13.4.8-canary.7 2023-06-27 23:04:22 +00:00
Max Leiter
25629bb880
node-polyfill-web-streams: require from stream/web instead of stream (#51906)
Follow-up to #51901
2023-06-27 15:09:45 -07:00
Max Leiter
a3d3016de8
next/node-polyfill-web-streams: fix web stream polyfill for Node v16 (#51901)
### What?
Slack thread:
https://vercel.slack.com/archives/C050WU03V3N/p1687889719318819

In Node 16, the ReadableStream is not on the global but instead under
the `stream` package. We should polyfill the global with that
implementation, if available

### Why?
Fixes passing ReadableStream from the Node.js runtime to the vercel/ai
SDK.
2023-06-27 12:46:23 -07:00
OJ Kwon
73c905dc53
feat(next-dev): add a new experimental flag (#51895)
### What?

WEB-1239. This is mainly for the code paths to not to cause regressions to existing turbopack.
2023-06-27 18:34:04 +00:00
Jiachi Liu
a04a34d3b7
Support relative url for openGraph.url and itunes.appArgument (#51877)
To enable the ability of leveraging current `pathname` and configured `metadataBase` for other canonical like urls, we support those urls with auto resolving with `pathename` and `metadataBase` like canonical url, then you could just configure relative paths like below

```js
openGraph: {
    url: './abc', // will be resolved based on pathname and metadata base
},
itunes: {
   appId: 'my-app-id'
   appArgument: './native-app'
}
```


Fixes #51829
Closes NEXT-1302
2023-06-27 17:01:49 +00:00
Jiachi Liu
a54d08bd50
Fix windows fill metadata dynamic path (#51885)
Fixes #51583
Fixes NEXT-1315
2023-06-27 16:16:24 +00:00
Leah
252c5a6908
make stdout blocking on macos (#51883)
### What?

Node.js sets stdout to non-blocking by default, rust expects it to be blocking

so when logging a lot of data, it can happen that rust receives an error while writing causing it to panic, the error is just that the resource is temporarily unavailable, but rust doesn't handle this case

See https://github.com/napi-rs/napi-rs/issues/1630
2023-06-27 14:55:27 +00:00
Shu Ding
7dfa56c714
Fix missing request body in DELETE and OPTIONS Route Handlers (#51874)
It looks like the `content-length` header can't be ignored for the request forwarded to the render worker. See https://github.com/nodejs/node/issues/27880.

Closes #48096, closes #49310. Fix NEXT-1194
fix NEXT-1114
2023-06-27 11:44:01 +00:00
vercel-release-bot
363b2368b3 v13.4.8-canary.6 2023-06-27 11:04:35 +00:00
Tim Neutkens
1bc2a5009c
Fix white screen when navigating to pages in certain cases (#51866)
## What?

In the first implementation of App Router a year ago I added `return
null` for this case which is incorrect as we have to suspend rendering
when doing a navigation.
The logic in `app-router.tsx` already handles that automatically. The
logic in layout-router is no longer needed as the flightData will be
applied automatically in the reducer.

## How?

Removed the logic when the fetch returns a path to mpaNavigate to.

<!-- 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(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

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

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

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


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
2023-06-27 12:31:40 +02:00
Shu Ding
69cbe4cbbf
Optimize client entry creation (#51849)
Removed an unnecessary dependency mapping from the plugin.

Here's a quick visualization of this change. We have to traversal the module graph twice. The first iteration goes through all server modules and creates the client entries. And the second iteration collects info from all client modules.

Before this PR, the second iteration starts from server entries so it traversals both layers. With this PR, the second iteration will start from client entries only:

<img width="870" alt="CleanShot 2023-06-27 at 10 01 08@2x" src="https://github.com/vercel/next.js/assets/3676859/f0b7a0c6-0ade-483a-8645-48e2a8a6c9d0">

This is a ~100ms improvement for HMR of complicate apps.
2023-06-27 09:13:30 +00:00
Zack Tanner
aec75c2f06
fix: build stats should properly report root page size in appDir (#51854)
### What?
`next build` is incorrectly reporting file size stats for the root path
when using the app dir

### Why?
In #50768, the denormalization logic was removed for appDir routes, but
`pagesManifest` has the root page keyed by `/` while the actual path
will be `/index`.

### How?
This re-adds a simpler denormalize function that just replaces `/index`
with `/`.

semi-related: #42819 

Fixes #51692

link NEXT-1306

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-06-26 18:57:50 -07:00
Jiachi Liu
d06bc5e286
perf: only require nextjs-require-cache-hot-reloader related API in dev mode (#51834)
`nextjs-require-cache-hot-reloader` is only required in dev mode for plugin, we can skip these require in production mode

![image](https://github.com/vercel/next.js/assets/4800338/889f98db-7029-4aea-9d12-0e03547be606)
2023-06-27 00:27:35 +00:00
Shu Ding
300507baf0
Enable compression for Webpack's cache during dev (#51851)
This is already the default for production build, but we are also enabling it for development. It has small perf impact as gzip takes time, but it significantly improves the disk usage and time for reading and writing to the filesystem. This is especially the case when working on a large codebase and we are transpiling and bundling more modules now.
2023-06-26 23:16:43 +00:00
Steven
8b60fbc718
fix: webpack target should match node engines (#51852)
The webpack `target` was outdated so I updated to match `engines`. See related https://github.com/vercel/next.js/pull/48941
2023-06-26 22:15:44 +00:00
Shu Ding
f1240b8efd
Refactor next-font-manifest-plugin (#51835)
## Background

Currently we're track all imported CSS modules by an entry, in the
client manifest:

```js
// client manifest
{
  entryCSSFiles: {
    'app/entry_name': {
      modules: ['app/foo.css', ...]
    }
  }
}
```

And then, in the font manifest we track all emitted assets (fonts) by
each CSS module:

```js
// font manifest
{
  app: {
    'app/foo.css': [
      'static/font/abc.woff', ...
    ]
  }
}
```

These two fields are only used together by `get-preloadable-fonts.tsx`,
so it can know which font files need to be preloaded for this entry.

(Although previously we use `.modules` for something else, but it's gone
now)

## Changes

Since we only need the font assets per entry, it's unnecessary to track
these in the module level and then join them together. This PR removes
the `modules` field from the client manifest, and changes the font
manifest to directly keep the entry—font mapping.

This gets rid of one module traversal from the client manifest plugin.
2023-06-26 13:43:37 -07:00
vercel-release-bot
57ab2818b9 v13.4.8-canary.5 2023-06-26 16:18:48 +00:00
Jimmy Lai
6238f8a5c0
performance: don't compile on hover on dev (#51830)
An annoying issue that slows down compilation times in dev for Next is
that we might trigger compilation of a page via hover on app.

We do this because we want the same experience in production and dev and
the prefetching is important for instantaneous loading states.

The alternative in this PR is that we don't prefetch at all anymore in
dev but instead, when we handle navigation, we can force a prefetch
navigation.

The slight compromise with this approach is that you can't test
prefetching anymore in dev.


<!-- 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(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

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

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

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


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

link NEXT-1317
2023-06-26 18:12:59 +02:00
Shu Ding
4b1a0165d3
Simplify Flight manifest plugin (#51589)
Instead of traversing the entire client module graph twice (!), this PR
changes it to only traverse the client **entry** modules only. Because
of the way we create client entries, all client modules' _boundaries_
can be retrieved via all outgoing connections of all chunks' entry
modules, filtered by `next-flight-client-entry-loader`.

This brings down the time complexity from `2 * num_client_modules` to
`num_client_entry_modules`.

Closes #51240.

Additional notes from @timneutkens 

- Removed `module.unsafeCache` as it seemed to be leaking modules across
multiple compilations, this should help with memory usage
- Changed entry-loader to have consistent module import map (sort it) to
ensure cache key is consistent

---------

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2023-06-26 18:11:08 +02:00
vercel-release-bot
c8f3897fe2 v13.4.8-canary.4 2023-06-26 15:27:18 +00:00
Shu Ding
5b9dd4c47e
Add WATCHPACK_WATCHER_LIMIT=20 (#51826)
Limit the number of Watchpack's watchers to 20 for the routing worker which runs Watchpack. By default this value is 2000 on macOS and 10000 on Windows, which can hold too much resource.
2023-06-26 14:17:44 +00:00
vercel-release-bot
913b365d85 v13.4.8-canary.3 2023-06-26 12:07:28 +00:00
Shu Ding
0dd0ef226c
Fix tree-shaking for metadata image functions on the Edge runtime (#51762)
This PR fixes tree-shaking for the metadata image generation module
(e.g. `opengraph-image.js` and other conventions) when the page has
`runtime = 'edge'`.

## Details

The first step of this fix is to change this from the loader:

```js
import * as exported from "./opengraph-image.js"
```

to be necessary fields only (so the `default` export can potentially be
removed):

```js
import { alt, size } from "./opengraph-image.js"
```

To know which fields are exported, we need to load the module first via
Webpack loader's `loadModule` API and check its
`HarmonyExportSpecifierDependency` dependencies.

This is the first step to make it tree-shakable. Since we have
`./opengraph-image.js` used in another entry, the actual image API route
`opengraph-image/route.js`:

```js
import * as image from "./opengraph-image.js"
```

Webpack still treats both as the same module and generates one chunk for
it. We want to "fork" it into two modules. The technique here is to add
a noop resource query and make it:

```js
import { alt, size } from "./opengraph-image.js?__next_metadata_image_meta__"
```

So it won't be shared in the chunk (as it's a different request), and
can be concatenated inline.

However that's not enough, the inlined result will still have all
imports from our `opengraph-image.js`, including `import { ImageResponse
} from 'next/server'`. Because we can't simply add `"sideEffects":
false` in Next.js' package.json, we need a way to mark this import as
side-effect free. I went through
https://github.com/webpack/webpack/blob/main/lib/optimize/SideEffectsFlagPlugin.js
and used the same method to mark that module with
`module.factoryMeta.sideEffectFree = true`.

With all these added, the page bundle will no longer contain the
`ImageResponse` instance.

## Result

The difference is quite amazing, for the new added test (an empty Edge
runtime page with an opengrah image file) here're the before/after
metrics for the `page.js` server bundle:

Edge bundle size: 892kB → 500kB.
Build time: 26.792s → 8.830s.
2023-06-26 13:44:29 +02:00
Cowboy Ho
70aad4a2b5
Fix NODE_OPTIONS='--inspect' not running expected (#51467)
## Fixing a bug

Fixes #50489
Fixes #48767
Fixes #45697

## What?
When running `NODE_OPTIONS='--inspect' next dev`
Then go to `http://localhost:3000/`
Will display error message `WebSockets request was expected` like the following screenshot
![image](https://github.com/vercel/next.js/assets/14261588/d2f3e520-7cce-40db-be69-99980804cc51)

Also the debug port for app and page still not follow by user input
When `NODE_OPTIONS='--inspect=8000' next dev` the app debug port still `54151`
![image](https://github.com/vercel/next.js/assets/14261588/e3d25c0e-9d00-4767-94d6-d954776912b2)


## Why?
#50248 added a function `getFreePort()` and it used on debug port and HTTP server port
So conflict happen between debug and HTTP port
Then show up error `WebSockets request was expected`

Here are some references about this error:
https://stackoverflow.com/questions/49766500/websockets-request-was-expected-error-when-using-inspect-brk-option


## How?
1. `getFreePort()` should only use on HTTP server
2. Added `getDebugPort()` for read the port from user input
3. Assign port to each worker
4. Add accurate info log for each debug port, e.g.: `proxy`,`router`,`app`,`page`

When `NODE_OPTIONS='--inspect' next dev` 
![image](https://github.com/vercel/next.js/assets/14261588/2260fdff-13fe-435a-9812-984315c0ea2e)

When `NODE_OPTIONS='--inspect=8000' next dev` 
![image](https://github.com/vercel/next.js/assets/14261588/de6e2f2d-9216-481f-b6db-7c5132e97b6f)

Also fix VSCode debugger
It is worth noting that
We can't hit the breakpoint on the first execution
because the file does not exist there yet or was not compiled
In most cases, the breakpoint can only be triggered normally during the second execution
![image](https://github.com/vercel/next.js/assets/14261588/dccded50-46c5-4b6d-beb7-ff0e5324327a)


Closes NEXT-1179
Closes NEXT-517



Co-authored-by: Shu Ding <3676859+shuding@users.noreply.github.com>
2023-06-25 22:31:09 +00:00
Shu Ding
cb7be1912f
Remove unncessary source matching from loader (#51775)
The same logic is handled by SWC already, and extracted here:

dd937bde09/packages/next/src/build/analysis/get-page-static-info.ts (L68)
2023-06-25 21:58:11 +00:00
Shu Ding
1ec61f76a0
Remove execSync (#51785)
Currently we have an `execSync` in the Telemetry class constructor which
runs at startup even if it might not be needed. This PR changes it to be
non-blocking and makes it lazily loaded.

Before:

<img width="907" alt="CleanShot 2023-06-25 at 16 33 57@2x"
src="https://github.com/vercel/next.js/assets/3676859/b3b1acb6-9968-4a00-9da9-b4b7fa859be3">

Note that all 3 processes (main, app renderer, pages renderer) all have
this.

After:

<img width="956" alt="CleanShot 2023-06-25 at 16 36 52@2x"
src="https://github.com/vercel/next.js/assets/3676859/7ef1973b-bdb2-485d-9336-6f67e13bccff">
2023-06-25 14:06:58 -07:00
Zack Tanner
1b804c0529
fix: interception rewrites should support catch-all segments (#51787)
### What?
Interception route rewrites are not properly parsing catch-all segments,
which leads to "missing parameter name" errors when passed to
`pathToRegexp`.

### Why?
The existing `toPathToRegexpPath` function ignores `...` and keeps it as
part of the regexp path. This means `pathToRegexp` will attempt to
handle `/foo/bar/:...baz` and `/foo/bar/:[...baz]` rather than
`/foo/bar/:baz*`

### How?
The regex used for matching the path was updated to support the dynamic
optional segment, and then we special case catch-all segments

Fixes #51784

---------
2023-06-25 14:02:10 -07:00
Shu Ding
869aeb40ce
Fix HMR for missing dependencies in next-app-loader (#51778)
As noted in the comments, it's necessary to always add optional dependencies as missing dependencies even if they exist because they might be deleted and re-added.

Closes #51763.
2023-06-25 19:26:09 +00:00
Shu Ding
70692d80b4
Rename serverActionsSizeLimit as serverActionsBodySizeLimit and add docs (#51755)
Close the circle after #51104. The name "size limit" can be confusing as it could also mean the size of the Server Action function itself, so this PR changes it to `serverActionsBodySizeLimit` and makes sure it's well documented.
2023-06-24 16:16:09 +00:00
Islam Sharabash
96d540c768
Allow matching against user-agent in rewrites that match headers (#48271)
This adds support for matching against the user-agent header when doing rewrites client side.
Currently a rewrite like this will fail because the user-agent header is not provided, and so next.js tries to prevent infinite rewrites:
```
{
  source: '/:path*',
  destination: 'https://example.com/:path*',
  missing: [
    {
      type: 'header',
      key: 'User-Agent',
      value: '.*(?:Electron).*'
    },
  ],
}
```

Adding in the user-agent header allows this rewrite to pass.
2023-06-24 05:32:11 +00:00
vercel-release-bot
74a5d2068a v13.4.8-canary.2 2023-06-24 02:20:39 +00:00
Jiachi Liu
e19e9e3ca3
Add critical next config value to github info (#51715)
Adding `nextConfig.output` to next-info output so that we could determine if users're using standalone mode if the reproduction is not clear or missing sometimes

Example output

```
    Relevant Packages:
      next: 13.4.8-canary.1
      eslint-config-next: 13.4.8-canary.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3
    Next.js Config:
      output: standalone
```
2023-06-24 00:07:07 +00:00
Zack Tanner
c98d2b927d
fix: handle 404 errors in HotReload client (#51637)
### What?
In dev mode, routing to a 404 page will result in an infinite redirect
cycle. This PR is an attempt to bring back similar functionality
currently implemented in the [pages
router.](https://github.com/vercel/next.js/blob/canary/packages/next/src/client/dev/on-demand-entries-client.ts#L41-L59)

### Why?
When the `pong` event (emitted from `onHMR`) occurs on a page that
doesn't exist, it will result in frequent calls to
`router.fastRefresh()`. Because the server response is an error, it'll
trigger a mpa navigation
[here](61ab4aadec/packages/next/src/client/components/router-reducer/fetch-server-response.ts (L115-L117)),
which is treated as an external route
[here](61ab4aadec/packages/next/src/client/components/router-reducer/reducers/fast-refresh-reducer.ts (L45-L53))
(and thus the window is reloaded)

In the case where you're on the page and it goes away, since we don't
have access to `__NEXT_DATA__`, I opted to use the dispatcher which will
trigger the parent `NotFoundBoundary`. This part felt a little awkward
-- I'm open to suggestions on a better way to handle this.

Closes NEXT-1299
Fixes #50585

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-06-23 16:30:48 -07:00
최지원
a9870df101
feat: add body parser limit for server actions (#51104)
<!-- 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(s) that you're making:

## For Contributors

### Improving Documentation

- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

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

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

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



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?
Add configuration options to modify the `bodyParser` size as it used to
be available in Page Router.
### Why?
Server Actions "Error: Body exceeded 1mb limit" cannot be resolved since
the body-parser limit size is hardcoded to `1mb`.
### How?

Closes NEXT-
Fixes #

-->

### What?

Add configuration options to modify the `bodyParser` size as it used to
be available in Page Router for APIs.

```js
export const config = {
  api: {
    responseLimit: false | '8mb' 
  },
}
```

Reference: [API Routes Response Size
Limit](https://nextjs.org/docs/messages/api-routes-response-size-limit)

### Why?

Server Actions "Error: Body exceeded 1mb limit" cannot be resolved since
the body-parser limit size is hardcoded to `1mb`.

```js
// /packages/next/src/server/app-render/action-handler.ts

// ...
const actionData = (await parseBody(req, '1mb')) || ''
// ...
```

Reference:
https://github.com/vercel/next.js/blob/canary/packages/next/src/server/app-render/action-handler.ts#L355
### How?

- Added option "serverActionsLimit" as `SizeLimit` type to
`config-shared.ts`
- Modified `action-handler.ts` to validate `serverActions` &
`serverActionsLimit` options in nextConfig
- Added conditional `serverActionsLimit` value and `1mb` if falsy

**Working on testing**

Fixes #49891 | #51097 | #51099

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Shu Ding <g@shud.in>
2023-06-23 16:24:18 -07:00
JJ Kasper
916e21f913
Update manual basePath with trailingSlash (#51726)
With the manualBasePath config we still need to normalize the trailing
slash per that config.

x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1687543336964799?thread_ts=1687487533.019499&cid=C03S8ED1DKM)
2023-06-23 15:36:16 -07:00