Commit graph

9721 commits

Author SHA1 Message Date
Jimmy Lai
30da48fcd2
server: enable minification by default (#54960)
This PR will enable minifying the *server* part of the user code by default when running `next build`.

## Explanation

Next.js compiles two versions of your code: the client version for the app that runs in the browser, and the server for the code that will run on the server. Whilst the client code has always been minified and optimised, we haven't done so historically for the server side.

## How does this impact me?

There are several consequences to this change:
- minified code makes error stacks less readable. To fix that, you can use source maps, which you can enable with `experimental.serverSourceMaps`. This is not enabled by default as this is an expensive operation.
- however the server code will be optimised for size so as a result, cold boots should improve

## opting out

you can opt out via specifying `experimental.serverMinification: false` in `next.config.js`
2023-09-06 10:24:01 +00:00
Shu Ding
d30c94b816
Adjust optimizePackageImports (#55040)
Added a few popular libs (all tested locally), removed some that don't have barrel entries.
2023-09-06 10:14:42 +00:00
Jiachi Liu
f2f1c251bf
Fix duplicated dynamic metadata routes in dev mode (#55026)
In dev mode, we're using a catch-all route for dynamic metadata routes, e.g. page path `/twitter-image` would become `/twitter-image/[[...metadata_id...]]/route` as a dynamic custom app route.

But we're missing to convert it in filesystem scanning for routing purpose, adding the metadata related normalizing logic for app page to align with other places.
2023-09-06 00:21:06 +00:00
vercel-release-bot
cdfb9de498 v13.4.20-canary.18 2023-09-05 22:49:40 +00:00
Shu Ding
a9b5174e68
Experimental server optimization (#54925)
Server React API optimization of `useEffect`, `useLayoutEffect` and `useState` under a `experimental.optimizePackageImports` flag.
2023-09-05 22:22:24 +00:00
Shu Ding
b65c823a64
Remove react-hot-toast from the optimizePackageImports list (#55029)
@MaxLeiter ran into an issue with this, need to investigate.
2023-09-05 15:05:20 -07:00
vercel-release-bot
1b844600ce v13.4.20-canary.17 2023-09-05 18:20:51 +00:00
Jimmy Lai
9f247d9f98
perf: use split chunks for the node server (#54988)
This PR introduces a change in the Next.js server that should improve memory usage nicely during dev.

## How

While investigating the repro cases in #54708, @timneutkens and I noticed that the high memory usage often involved `googleapis`. Digging a bit more, I also saw that in dev, the bundle generated for a page using `googleapis` was around 80MB and that requiring it in Node.js increased memory by 160MB 🥲 and that requiring another page that also used this also increased the memory by 160MB.

The problem is that Next.js, on new navigations and hot reloads, might need to load/reload all the code required for the page *all the time*. This issue is also exacerbated by the fact that there's a nasty Node.js bug that makes it impossible to clean that memory up, leading to memory bloat and overall a pretty bad DX.

So if we can't clean this up, what can we do about it?

The change I'm introducing in this PR is that I'm changing Next.js in order to split the code you're using from `node_modules` from the code you've written in different chunks. The idea is that the heavy code you're loading from `node_modules` is only gonna be loaded once per session this time.

This should make a navigation/page reload only load the user bundle now. On my simple test case, the cost of navigation went from ~200MB to ~40MB.

A few notes on the implementation:
- The chunks for the `node_modules` are split at the module level, this is to ensure that each of the node_modules dependencies is split in the most memory efficient manner. If it was a big monolithic chunk, it would potentially be reloaded again and again whenever reloaded, leading to leakage.
- I'm guessing we could do the same for the Edge server
- the first load for a page will still be fairly heavy memory wise, there's probably something else we can do there 
- there's also an issue with the webpack require cache being flushed, whereas it could be reused

## comparisons

### navigating a page

before
<img width="284" alt="CleanShot 2023-09-04 at 21 00 46@2x" src="https://github.com/vercel/next.js/assets/11064311/44e37df8-4414-4ca1-b6bf-fb0fb11751ea">


after
<img width="392" alt="CleanShot 2023-09-04 at 20 58 53@2x" src="https://github.com/vercel/next.js/assets/11064311/46226123-a73a-4132-a99d-fb812e59df46">
2023-09-05 15:54:44 +00:00
Tim Neutkens
bf3c3ce245
Client-side HMR message types (#55009)
Continuation of the previous PRs that introduce HMR event types for the server-side code, this leverages those types client-side too.
2023-09-05 15:30:35 +00:00
Florentin / 珞辰
ffd504c43f
[functions-config-manifest] use correct extra config for pages router (#54786)
When using page routes we need to pass the contents of the `export const config` to the `extraConfig` populating the functions config manifest rather then relying on named exports like in app router.
2023-09-05 14:13:37 +00:00
Kiko Beats
7288c866eb
upgrade edge-runtime (#55005)
Fixing `Headers#getSetCookie` method
2023-09-05 13:29:17 +00:00
Damien Simonin Feugas
39a66d5a08
chore: restore options to opt-in for server-side transpilation (#55010)
### 🧐 What's in there?

As discussed with @huozhi, it's restoring code from #52393 which I forgot to restore in #54891.
It is unlikely to make a difference, but we'll set globalWindow based on the desired test environment. This will not make any difference for most people.

### 🧪 How to test?

Once you've rebuilt your local version of Next, `pnpm testheadless jest/rsc` 

Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-09-05 12:55:10 +00:00
Balázs Orbán
3a62a30649
chore: bump undici (#55007)
This bumps `undici` to the latest version to match the one used in newer Node.js versions', potentially fixing compatibility issues.

I also cleaned up some related `global as any` usage.
2023-09-05 12:29:51 +00:00
Jiachi Liu
e117c000e4
Redesign nextjs logging (#54713)
The current logging styles has been existed for a while, this PR gives a fresh impression for the logging output from Next.js.
We want to achieve few new goals that makes the output clean, modernized, sweet 🍫 .

Few goals are addressed with this redesign:

## Refresh Impression & Simplification

The new design of logging is much more information centralized and streamlined.

* Given a `ready` message at the begining when compilers are bootstrapped.
* Only show `compiled` event with green check mark indicating succesful compilation, this will merge the unclear `compiling` event which shows `(client and server)` before, now tell you the route compilation info in one line.

hello world app

### `next dev`

#### After vs Before


<img src="https://github.com/vercel/next.js/assets/4800338/9649b340-8241-4756-a2b3-a989f0b74003" height="120"> 
<img src="https://github.com/vercel/next.js/assets/4800338/ee181263-3dd4-40d0-9ffc-819a56b45900" height="120">  

 


 

### `next build`

#### After vs Before


<img src="https://github.com/vercel/next.js/assets/4800338/5db9829a-9ffc-49f0-b030-93ee92f5c248" width="360"> 
<img src="https://github.com/vercel/next.js/assets/4800338/b9527b83-27c8-4426-9c0d-c0d4072b7d58" width="360">





### error status

#### After vs Before

<img src="https://github.com/vercel/next.js/assets/4800338/00455226-ace7-468b-8d90-0d36bf038489" height="120"> 
<img src="https://github.com/vercel/next.js/assets/4800338/1be8c451-d3f0-465c-9ef7-6b0dde7cff85" height="120"> 



## Streamlization

If you have customized envs and experiments Next.js will give the brief in the early summary about your network information, env vars, and enabled experimental features

<img src="https://github.com/vercel/next.js/assets/4800338/ca1a7409-1532-46cb-850f-687e61e587b2" width="400">


## Polish

### fetching logging structure 

#### After vs Before
<img src="https://github.com/vercel/next.js/assets/4800338/97526397-dffe-4736-88ed-e5cbe5e945bd" width="400">
<img src="https://github.com/vercel/next.js/assets/4800338/ab77c907-5ab5-48bb-8347-6146d2e60932" width="400">


### Dedupe Duplicates

The logging is moved from `@next/env` to `next` itself, `@next/env` will only notify the invoker that the env is reloaded. Then the duplicated logs for the env reloading cases can be avoid.

#### After vs Before
<img src="https://github.com/vercel/next.js/assets/4800338/04799295-e739-4035-87aa-61cec962fc39" width="400">
<img src="https://github.com/vercel/next.js/assets/4800338/e29020c9-0031-4bf3-a21b-8b64633f43a2" width="400"> 


### Different indicators

Use unicode text icons for different situation: 
* passed -> check mark
* warning -> warning
* error -> red cross
* loading -> circle

<img src="https://github.com/vercel/next.js/assets/4800338/715c34bd-298f-4990-a5d7-e12e455ead44" width="400">



Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
2023-09-05 11:40:00 +00:00
Tim Neutkens
37a6e213a2
Add turbopack-connected HMR event (#54976)
Additional HMR actions that weren't using the types yet.
2023-09-04 15:26:57 +00:00
Tim Neutkens
f313235428
Remove pong HMR event as it is not used (#54965)
While investigating the HMR event types I noticed `pong` is not being used in Pages Router nor in App Router.

This PR removes the code that sends it as it's essentially dead code.
2023-09-04 13:27:47 +00:00
vercel-release-bot
7a1924ed6d v13.4.20-canary.16 2023-09-04 11:58:51 +00:00
Damien Simonin Feugas
3338dd0b31
fix(next-swc): skips client/server only checks when running with Jest to unblock testing (#54891)
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-09-04 13:53:41 +02:00
Tim Neutkens
91922c44eb
Add serverError action to list of HMR events (#54964)
Adds `serverError` to the list of HMR actions, missed this in the earlier PRs as it wasn't using `hotReloader.send`.

Also ensures on-demand-entry-handler reuses the hotReloader.send method.
2023-09-04 09:40:40 +00:00
Tim Neutkens
986f648baa
Unify serverError hmr event (#54962)
Ensures only one name is used for the event, makes it easier to search for, going to replace this with the constant like the other events in a follow-up PR but this can be landed as-is.
2023-09-04 09:01:22 +00:00
Jiachi Liu
9abf8863cc
docs: fix document format for __next_private_export_map__ (#54952)
Fix the document format of `__next_private_export_map__`

follow up: #54695
2023-09-03 20:53:30 +00:00
Zack Tanner
c013e98fa4
fix: server actions firing twice after navigation (#54948)
The original logic here was introduced to unblock client side navigations if a server action was in flight, however this introduced a bug where subsequent actions would fetch twice after navigation. 

This was happening because the promise handling was in the wrong spot: previously this would potentially cause both the `then` callback to fire while simultaneously the action reducer would handle the result. Moving it to where we're first checking if there's a pending navigation will more reliably indicate if the action was resolved after we discarded it in the reducer.

Closes NEXT-1589
Fixes #54746
2023-09-03 20:31:48 +00:00
Jiachi Liu
6bc23cd2bd
Fix group routes custom root not-found (#54931)
For group routes, as their custom layout show up at the 1st level of the loader tree, so previously we tried to add the default not-found to that layer, but if they have custom root not-found, we should use that as the root not-found componeny, instead of using default not-found component to form the 404 page

Closes NEXT-1588

Fixes #51075
Fixes #54892
2023-09-03 20:15:24 +00:00
Shu Ding
ef6140723a
Ensure that barrel files behind wildcards are transformed into shortpath (#54951)
This fix ensures the case that if you `import { foo } from './index'`,
where:

index.js:

```js
export * from './subpkg'
```

subpkg.js:

```js

const unrelatedExpressions = ...

export { foo } from './foo'
export { bar } from './bar'
```

Previously we'll transform the proxy to the second module to `export {
foo } from './subpkg'`. With this fix it will be correctly optimized as
`export { foo } from './foo'` which is a shorter path.
2023-09-03 13:02:14 -07:00
Tim Neutkens
7ab02c5e1e
Fix typo in hot-reloader (#54944)
Noticed this small typo from hrm -> hmr.
2023-09-03 13:37:17 +00:00
Tim Neutkens
0f602f4943
Add types for hot reloader send method (#54932)
First part adding additional types for the various events that are sent through the websocket to the browser.

Will add additional details / comments on the types after landing these changes in the browser-side code too.
2023-09-03 00:07:32 +00:00
Justin Ridgewell
2b9ffb009c
Update turbopack (#54909)
Minor update that includes changes necessary to get HMR errors working.

Closes WEB-1494
2023-09-02 10:45:49 +00:00
Will Binns-Smith
125605c307
Debug tracing: time devserver startup (#54537)
This:

- Profiles dev server startup by wrapping most logic in a debug tracing Span
- Sets tracing globals for this case earlier, as they would otherwise be unavailable
- Allows Turbopack traces to be uploaded, as while currently incomplete, they are accurate


Closes WEB-1440

Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-09-02 00:34:37 +00:00
Wyatt Johnson
c3d23c6db6
Simplify getPagePaths logic (#54908)
Simplified some logic around `getPagePaths` as well as another normalization function. This instead utilized some flat arrays using push instead of relying on a separate `flatten` method (allowing us to remove it). This also reduced the number of logic checks for this generation.
2023-09-01 23:42:34 +00:00
Tim Neutkens
dab8f1f0d9
Remove leftover parameter on findPagesDir (#54905)
Cleans up more leftover App Router enabled conditions.




Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
2023-09-01 22:46:05 +00:00
Will Binns-Smith
03c3c004a1
Turbopack Experimental: Remove use of SourceMapContentSource (#54862)
Depends on https://github.com/vercel/turbo/pull/5852

This removes references to the `source_maps` module from Turbopack as it no longer exists in https://github.com/vercel/turbo/pull/5852

Test Plan:
- Opened a Next.js app with `--turbo-experimental`
- Change a file, verify the page is updated with HMR
- Confirm the source map for the updated file is correctly loaded in DevTools


Closes WEB-1483

Co-authored-by: Justin Ridgewell <112982+jridgewell@users.noreply.github.com>
2023-09-01 22:01:54 +00:00
Shu Ding
d94248418a
Add @tremor/react to optimizePackageImports (#54900)
Tested locally.
2023-09-01 19:40:32 +00:00
Zack Tanner
ef33018a3d
fix next/headers when used in src/middleware (#54889)
This updates the check to see if a request is made in middleware to be a bit less brittle so it doesn't break if middleware is located in `/src/middleware` (`params.page` would be `/src/middleware` rather than `/middleware` in that case)

fixes https://github.com/vercel/next.js/discussions/54853
2023-09-01 18:56:51 +00:00
Will Binns-Smith
7e7914c7f8
Turbopack: remove turbo_tasks::unit() from integration tests (#54901)
Closes WEB-1491
2023-09-01 18:06:39 +00:00
Will Binns-Smith
e76cf06f6a
Turbopack: Remove use of turbo_tasks::unit() (#54898)
This is deprecated -- replace it with `Default::default()` as suggested by the deprecation message.


Closes WEB-1490
2023-09-01 17:47:32 +00:00
Shu Ding
46d3a7c104
Remove mutation of renderOpts (#54888)
It's a bad pattern to do things like `(renderOpts as any).fetchMetrics = staticGenerationStore.fetchMetrics` and receive the updated value in the outside. This PR updates it to use `RenderResults`'s metadata instead.

In the future we should use `Reaedonly<RenderOpts>` and ban `as any` <picture data-single-emoji=":lolsob:" title=":lolsob:"><img class="emoji" width="20" height="auto" src="https://emoji.slack-edge.com/T0CAQ00TU/lolsob/2e754996aebc14fa.png" alt=":lolsob:" align="absmiddle"></picture>
2023-09-01 16:26:55 +00:00
Jiachi Liu
28ef2473af
Fix performance measures crashing the app (#54858)
There're various of cases would cause the `beforeRender` performance mark is not existed in the performance entries, learned from the issues description. We have to check if that mark is existed.

This PR also refactors all the mark names and measurement names into constants so that we won't easily mistype them


Fixes #20743
Fixes #40903
Fixes #47560

Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
2023-09-01 14:51:38 +00:00
Justin Ridgewell
5b5dc4f1f3
Turbopack: Trigger Error Overlay in next-api (#54865)
### What?

This hooks up Turbopack's `TurbopackResult` issues into Next's HMR
messages.

### Why?

We need to deliver good error messages to devs so that they can quickly
fix any issues.

### How?

This builds up a debounced HMR queue (necessary because of the Turbopack
architecture delivering multiple messages via different async
iterators). If any HRM message contains an issue, we'll send down a
`built` message containing the issues and delay sending the real
messages. Once everything has been resolved, we'll resume sending down
HMR messages.

<img width="990" alt="Screenshot 2023-08-31 at 9 01 20 PM"
src="https://github.com/vercel/next.js/assets/112982/d5421779-2415-4a60-8d95-94f0e1147d6d">

Closes WEB-1484

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2023-09-01 16:05:22 +02:00
Donny/강동윤
a5c180388a
Update swc_core to v0.82.11 (#54653)
### What?

Update swc crates to
44de87f481

### Why?

To use `import with` and etc...

### How?

Closes WEB-1460
Fixes #

---

Turbopack counterpart: https://github.com/vercel/turbo/pull/5820

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2023-09-01 15:35:04 +02:00
Will Binns-Smith
58f1d238c4
Turbopack: Log build progress in development like webpack does (#54806)
This adds logging to indicate build progress while using Turbopack in the same format that webpack uses. It uses the same store object and sets state to log messages like so:

```
- wait compiling /page (client and server)...
- event compiled client and server successfully in 1366 ms
```

Given:

- An app router page `/`, `compiling /page` is logged
- An app router page `/bar`, `compiling /bar/page` is logged
- An app router route `/api/bar`, `compiling /api/bar/route` is logged
- A page router page `/foo`, `compiling /foo` is logged
- A page router api route `/api/foo`, `compiling /api/foo` is logged


Closes WEB-1476
2023-09-01 00:18:24 +00:00
vercel-release-bot
d172e7b72c v13.4.20-canary.15 2023-08-31 22:23:23 +00:00
JJ Kasper
2eef775472
Update tag handling for app cache (#53321)
Optimizes how we handle cache tags for soft tags (auto-added by Next.js)
and normal tags (added manually) and adds differentiating between
`revalidatePath('/blog/first')` and page/layout.

Soft tags are not stored across cache entry and instead auto sent along
when checking cache entries. This allows us to prevent storing
exponential amounts of tags across cache entries while still having the
relationship between them so that single path revalidation can work
properly.

x-ref: [slack
thread](https://vercel.slack.com/archives/C042LHPJ1NX/p1690586837903309)
2023-08-31 15:17:40 -07:00
Will Binns-Smith
1fc4b22dd3
Don't send tracing spans if performance.measure doesn't return measurements (#54808)
Fixes #54389.

Old versions of Safari appear to return `undefined` from `performance.measure()` instead of `PerformanceMeasure` values.


Closes WEB-1477
2023-08-31 19:19:42 +00:00
Leah
cc77579589
fix(turbopack): edge api entry path should not have /route suffix (#54851)
### Description

Lookup is done by the page name, which does not include the `route` suffix

Closes WEB-1481
2023-08-31 18:36:59 +00:00
vercel-release-bot
e1cc97f3e4 v13.4.20-canary.14 2023-08-31 17:24:11 +00:00
Jiachi Liu
1be02e36fe
Update @vercel/og to latest (#54849)
Upgrade `@vercel/og` to `0.5.12` and satori to the corresponding version
2023-08-31 17:17:07 +00:00
Dima Voytenko
826ef8c715
Allow any number of onFetch handlers for a single test (#54846)
Recent usability testing indicated that it's convenient to register multiple `onFetch` handlers and iterate them until one of them response a result.
2023-08-31 15:58:53 +00:00
Justin Ridgewell
9d7f54c11b
Fix the build manifest path for / (#54744)
### What?

Fixes a bug introduced in #54668, where the `/` path would map to the build manifest `.next/server/page/app-build-manifest.json` in App router instead of the correct `.next/server/index/page/app-build-manifest.json`
It also addresses an issue where `not-found` manifests would map to `.next/server/not-found/app-build-manifest.json` instead of the correct `.next/server/not-found/page/app-build-manifest.json`.

### Why?

The `&& type === 'pages'` mean we only rewrote `/` to `/index` in Pages router, where it needs to apply to both App and Pages. Likewise, special-casing `not-found` led to it not finding the manifest.

### How?

Duplicate the logic from d3a107991c/packages/next-swc/crates/next-core/src/util.rs (L65-L76)

Closes WEB-1471


Co-authored-by: Will Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
2023-08-31 03:14:25 +00:00
vercel-release-bot
fe191f8e54 v13.4.20-canary.13 2023-08-31 01:53:14 +00:00
Justin Ridgewell
8b4bb031ba
Fixes for Turbopack HMR (#54790)
### What?

There were a few issues with the initial implementation of next-api HMR:
1. We incorrectly errored out when we received a Next.js WebSocket message
2. We didn't handle Next's `span-end` message, leading to another error
3. We listened to the `htmlEndpoint` change events instead of the `dataEndpoint`/`rscEndpoint`, leading to us detecting client-side changes and causing full page reloads

### Why?

HMR is the life-blood of development

### How?

Small fixes to our Turbopack reimplementation of the server-side HRM handlers

Closes WEB-1475
2023-08-30 21:14:46 +00:00