Commit graph

1216 commits

Author SHA1 Message Date
Jiachi Liu
311eea4c6a
Fix emotion-js transform for server components (#54284)
### What & Why

emotion-js has its own [jsx transform](https://emotion.sh/docs/typescript#emotionreact) which is being applied when `compiler.emotion` is enabled in `next.config.js`.

Thanks to emotion-js team that provided an emotion-js example setup with app router [here](https://github.com/emotion-js/emotion/issues/2928#issuecomment-1319792703), so that we can use it as test example with app router. Based on the setup, we create a test case working with emotion js but failed with error mentioned in #41994 that some client hooks appearing in server components. That is because the emotion-js jsx factory includes some client hooks.

### How

For server components, css-in-js is not recommended to apply so we disabled the transform before, the emotion jsx factory is a separate config that should also not be applied in server components. So in this case we still use react jsx factory instead of the emotion-js one for server components then it won't error. The test case can also be used as an example for basic emotion-js use case with app router.


Fixes #41994
Closes NEXT-1368
2023-08-20 03:14:16 +00:00
Jiachi Liu
85606472e4
Add test for using custom font in metadata image routes (#54274)
In the past few rounds of improving metadata image routes bundling, we have improved the bundling strategy and also updated [the usage tutorial of using custom fonts in og image routes](https://vercel.com/docs/functions/edge-functions/og-image-generation/og-image-examples) which should load the font in the image route handler.

Adding some tests to ensure custom fonts are working with metadata

Closes #48081
2023-08-19 14:29:18 +00:00
Shu Ding
0e78798f37
Fix renamed export of Server Actions (#54241)
This fixes the compilation of `export { action as renamed }` syntax. Previously it's compiled as `export var action = ...` and with this fix, it will be `export var renamed = ...`.

Closes #54229.
2023-08-18 20:41:32 +00:00
Jiachi Liu
c305bf6afb
Add default not found to loader tree of group routes root layer (#54228)
For group routes, unlike normal routes, the root layout is located in the "group"'s segment instead of root layer.
To make it also able to leverage the default not found error component as root not found component, we need to determine if we're in the root group segment in the loader tree, and add the not-found boundary for it.


If you compre the loader tree visually they will look like this:

Normal route tree structure
```
['',
 { children: [segment, {...}, {...}]},
 { layout: ..., 'not-found': ... }
]
```

Group routes
```
[''
 { children: ['(group)', {...}, { layout, 'not-found': ... }]}
 {}
]
```

Comparing to normal case, we go 1 layer down in the tree for group routes, then insert the not-found boundary there

Fixes #52255
Closes NEXT-1532
2023-08-18 16:56:55 +00:00
Jiachi Liu
e20c8c824c
Assign default not-found boundary if custom not-found is not present for root layer only (#54185)
Fixes #54174 

We should only add default not-found boundary to loader tree components for root layout. It was accidently added for children routes before
2023-08-17 18:27:27 +00:00
Jiwon Choi
043114773f
fix: cookies().has() breaks in app-route (#54112)
- Added `has` to ResponseCookies in [edge-runtime/cookies#533](https://github.com/vercel/edge-runtime/pull/533)
- Upgraded edge-runtime/cookies to 3.3.0 #54117 
- Added a test case


Fixes #54005 #54111
2023-08-17 01:06:07 +00:00
Jiachi Liu
594f3d1fc0
Fix root not-found page tree loader structure (#54080)
### What & Why

Previously when rendering the root `/_not-found` in production, we'll always override the parallel routes component children with not-found component, this will break the navigation in build mode from root 404 `/_not-found` path.

### How

The new solution is to change the root `/_not-found` rendering strategy. Previously the loader tree of `/_not-found` look like this

```js
['',
  {
    children: ['not-found', {}, {}]
  },
  { layout: ..., 'not-found': ...}
]
```

it's not a pretty valid tree, which could lead to problems during rendering.

New solution is to change the children to render a page, but the page content is `not-found.js` component. The new tree of root not-found will look like

```js
['',
  {
    children: ['__PAGE__', {}, {
      page: ... // same as root 'not-found'
    }]
  },
  { layout: ..., 'not-found': ...}
]
```

This change could fix the navigation on the root not-found page.

Fixes #52264
2023-08-16 15:10:08 +00:00
Zack Tanner
cb432eb42d
Fix scroll bailout logic when targeting fixed/sticky elements (#53873)
### What?
When navigating to a new page with fixed or sticky positioned element as the first element, we were bailing on scroll to top behavior, which often isn't expected.

### Why?
Currently, we decide to bail on scroll to top behavior on navigation if the content that is swapped into view is visible within the viewport. Since fixed/sticky positioned elements are often intended to be relative to the current viewport, it's most likely not the case that you'd want it to be considered in this heuristic. For example, if you were scrolled far down on a page, and you navigated to a page that makes use of a sticky header, you would not be scrolled to the top of the page because that sticky header is technically visible within the viewport. 

### How?
I've updated the previous implementation that was intended to skip targeting invisible elements to also skip over fixed or sticky elements. This should help by falling back to the next level of the layout tree to determine which element to scroll to.

I've deleted the `// TODO-APP` comments as I couldn't think of a scenario in which we'd need a global scrollTop handler -- if we've bailed on every element up the tree, it's likely the page wasn't scrollable.

Some additional considerations:
- Is the warning helpful or annoying?
- Is the parallel route trade-off an acceptable one? (ie, a parallel modal slot might not be considered in the content visibility check unless if it’s fixed positioned)

Closes NEXT-1393
Fixes #47475
2023-08-15 13:31:39 +00:00
Jiachi Liu
ec6d2c7825
Do not output pages 404 in tree view if app not-found is used (#54051)
### What?

Skip logging `/404` for pages routes in `next build` when app router root not-found is present

### Why?

When app router's root not-found is used it can cover all the not found cases, and for static rendering it can already replace the `404.html`. So in the tree view we don't need to log the pages `/404` when those cases are covered by app router.
2023-08-15 09:59:43 +00:00
Zack Tanner
e02397d264
fix: pages not visible in dev when named children (#54007)
`getEntryKey` had some logic to remove `children` if it was part of the entry (originally it was intended to fix an issue with parallel slots that were used in place of a page, but wasn't working as intended). However, this breaks pages that are named `children`.

Updating this again to implement what I think was the intended behavior in 4900fa21b0 which is to point to the correct entry when a parallel slot is used in place of a page component. 

- x-ref: #52362

Closes NEXT-1514
Fixes #53072
2023-08-15 03:50:37 +00:00
Ngô Đức Anh
a4b430e6f1
Better IPv6 support for next-server (#53131)
### What?
This PR makes it easier to use Next.js with IPv6 hostnames such as `::1` and `::`.

### How?
It does so by removing rewrites from `localhost` to `127.0.0.1` introduced in #52492. It also fixes the issue where Next.js tries to fetch something like `http://::1:3000` when `--hostname` is `::1` as it is not a valid URL (browsers' `URL` class throws an error when constructed with such hosts). It also fixes `NextURL` so that it doesn't accept `http://::1:3000` but refuse `http://[::1]:3000`. It also changes `next/src/server/lib/setup-server-worker.ts` so that it uses the server's `address` method to retrieve the host instead of our provided `opts.hostname`, ensuring that no matter what `opts.hostname` is we will always get the correct one.

### Note
I've verified that `next dev`, `next start` and `node .next/standalone/server.js` work with IPv6 hostnames (such as `::` and `::1`), IPv4 hostnames (such as `127.0.0.1`, `0.0.0.0`) and `localhost` - and with any of these hostnames fetching to `localhost` also works. Server Actions and middleware have no problems as well.

This also removes `.next/standalone/server.js`'s logging as we now use `start-server`'s logging to avoid duplicates. `start-server`'s logging has also been updated to report the actual hostname.
![image](https://github.com/vercel/next.js/assets/75556609/cefa5f23-ff09-4cef-a055-13eea7c11d89)
![image](https://github.com/vercel/next.js/assets/75556609/619e82ce-45d9-47b7-8644-f4ad083429db)
The above pictures also demonstrate using Server Actions with Next.js after this PR.
![image](https://github.com/vercel/next.js/assets/75556609/3d4166e9-f950-4390-bde9-af2547658148)

Fixes #53171
Fixes #49578
Closes NEXT-1510

Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
2023-08-14 07:23:24 +00:00
Jiachi Liu
4056994304
Recover not found errors from flight data to render with proper boundary (#53703)
### What?

We change the not-found rendering strategy to the origin one which recovers the not found error from the flight data, and hit the error boundary to display the closet not found component.

For parallel `@slot` we shouldn't pass down the not-found boundary, the boundary is only for `@children`.

### Why?

We're having a lot of not-found matching issues that manually searching for not found and layout won't be accurate as we have various scenarios like `(group)` routes, dynamic routes, etc.

### How?

Only render html with empty body so that the error can recover from flight and render the proper not-found component during hydration.

One change for metadata is that we need to get the "not-found" metadata in the initial render, so we'll catch the not-found error once there first and start render the "not-found" metadata and put it in the flight data. Then when it recovers it's still preserved.

Fixes #53694
2023-08-12 08:41:47 +00:00
Jiachi Liu
50ff54ee29
Use summary_large_image as twitter card if images present by default (#53919)
This is an improvement for twitter card, if there's image present we could use `summary_large_image` to make the twitter card look better instead of using `summary` by default, so that users could benefit from the large image card display on twitter
2023-08-11 17:52:45 +00:00
Jan Nicklas
9e1e9bf311
Fix/match resource (#53796)
### Bug Fix
fixes #53366



### Change

Add `query` and [`matchResource`](https://webpack.js.org/api/loaders/#inline-matchresource) to the module mapping. 



### Why

`mod.resource` and all other paths inside `packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts` are already using the query:

e127c51327/packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts (L506-L507)

e127c51327/packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts (L584-L585)



### Motivation

We are using a static extraction css-in-js solution which uses the [inline match resource feature](https://webpack.js.org/api/loaders/#inline-matchresource) (see more in #53366)

As a css-in-js file contains the TSX and the CSS code we have to split the file into two modules which share the same `resource.path`.  

This works well however there is a problem in the mapping inside `.next/server/app/page_client-reference-manifest.js`.
The `ssrModuleMapping` from `addSSRIDMapping` in 
e127c51327/packages/next/src/build/webpack/plugins/flight-manifest-plugin.ts (L270) links to the latest module (the css part) and not the the initial one (the js part).

Therefore the css module is rendered [react-server-dom-webpack](https://www.npmjs.com/package/react-server-dom-webpack/v/0.0.1?activeTab=versions) and breaks.

The SSR/SSG fails with the error `Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.`

Co-authored-by: Shu Ding <3676859+shuding@users.noreply.github.com>
2023-08-11 17:36:58 +00:00
Luud Janssen
5fe332186e
Add changeFrequency and priority attributes to sitemaps (#48484)
Closes #48479. 

Couldn't find the source for the Next.js beta docs, so I didn't add documentation.

Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-08-09 20:36:22 +00:00
Zack Tanner
712669f605
improve error message for conflicting parallel segments (#53803)
This is a follow-up to log both conflicting paths & a link to route group docs, which I believe is the only scenario someone could trigger this

- https://github.com/vercel/next.js/pull/53752
2023-08-09 17:21:24 +00:00
JJ Kasper
4b4533787b
Add warnings for static generation bail outs (#53761)
Currently using server actions on a page or using edge runtime causes
that page to bail out of ISR or static generation so this adds warnings
to make users aware of this.

x-ref: [slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1690816539472449)

---------

Co-authored-by: Zack Tanner <zacktanner@gmail.com>
2023-08-08 20:09:34 -07:00
Josh Story
79b7c1493b
App Router Preinitialize all required scripts except one for bootstrap (#53705)
Currently all scripts that are required for every page are loaded as
part of the bootstrap scripts API in React. Unfortunately this loads
them all as sync scripts and thus requires preloading which increases
their priority higher than they might otherwise be causing things like
images to load later than desired, blocking paint. We can improve this
by only using one script for bootstrapping and having the rest
pre-initialized. This only works because all of these scripts are
webpack runtime or chunks and can be loaded in any order asynchronously.

With this change we should see improvements in LCP and other metrics as
preloads for images are favored over loading scripts

Co-authored-by: Steven <steven@ceriously.com>
2023-08-08 17:28:17 -07:00
Zack Tanner
d58fd68f0a
fix parallel route tests & improve error for conflicting pages (#53752)
This fixes some tests that were disabled due to a missing `page` segment for the corresponding slots in `/parallel/nested`. 

While fixing, I also noticed if you accidentally create two pages that resolve to the same URL segment (which is fairly easy to do accidentally do with route groups), we were throwing an unhelpful error of "Cannot find module: '<snip>/page_client-reference-manifest.js'" when building (and fail silently in dev). For example, this scenario was throwing a manifest error:

```
app
  (groupa)
    page.tsx
  (groupb)
    page.tsx
```

This will now throw with a more helpful error when resolving parallel segments if the page segment was already resolved. This also re-enables the disabled tests.

Closes NEXT-1440
Fixes #53569 (by virtue of throwing a more helpful error)
2023-08-08 22:56:39 +00:00
Tim Neutkens
c5a8e0989e
Consolidate Server and Routing process into one process (#53523)
In the current version of Next.js there are 4 processes when running in
production:

- Server
- Routing
- Rendering Pages Router
- Rendering App Router

This setup was introduced in order to allow App Router and Pages Router
to use different versions of React (i.e. Server Actions currently
requires react@experimental to function). I wrote down more on why these
processes exist in this comment:
https://github.com/vercel/next.js/issues/49929#issuecomment-1637185156

This PR combines the Server and Routing process into one handler, as the
"Server" process was only proxying to the Routing process. In my testing
this caused about ~2x the amount of memory per request as the response
body was duplicated between the processes. This was especially visible
in the case of that memory leak in Node.js 18.16 as it grew memory usage
on both sides quickly.

In the process of going through these changes I found a couple of other
bugs like the propagation of values to the worker processes not being
awaited
([link](https://github.com/vercel/next.js/pull/53523/files#diff-0ef09f360141930bb03263b378d37d71ad9432ac851679aeabc577923536df84R54))
and the dot syntax for propagation was not functioning.

It also seemed there were a few cases where watchpack was used that
would cause many more files to be watched than expected, for now I've
removed those cases, specifically the "delete dir while running" and
instrument.js hmr (instrument.js is experimental). Those tests have been
skipped for now until we can revisit them to verfiy it

I've also cleaned up the types a bit while I was looking into these
changes.

### Improvement

⚠️ Important preface to this, measuring memory usage / peak usage is not
super reliable especially when JS gets garbage collected. These numbers
are just to show the rough percentage of less memory usage.

#### Baseline

Old:

```
next-server: 44.8MB
next-router-worker: 57.5MB
next-render-worker-app: 39,6MB
next-render-worker-pages: 39,1MB
```

New:

```
next-server: Removed
next-router-worker: 64.4MB
next-render-worker-app: 43.1MB (Note: no changes here, this shows what I meant by rough numbers)
next-render-worker-pages: 42.4MB (Note: no changes here, this shows what I meant by rough numbers)
```

Overall win: ~40MB (process is removed)

#### Peak usage

Old:

```
next-server: 118.6MB
next-router-worker: 223.7MB
next-render-worker-app: 32.8MB (I ran the test on a pages application in this case)
next-render-worker-pages: 101.1MB
```

New:

```
next-server: Removed
next-router-worker: 179.1MB
next-render-worker-app: 33.4MB
next-render-worker-pages: 117.5MB
```

Overall win: ~100MB (but it scales with requests so it was about ~50% of
next-router-worker constantly)

Related: #53523

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-08-08 16:06:32 +02:00
Zack Tanner
fef6f82aba
Add docs page for uncaught DynamicServerErrors (#53402)
When using imports from `next/headers` in a layout or page,
`StaticGenerationBailout` will throw an error to indicate Next.js should
fallback to dynamic rendering. However, when async context is lost, this
error is uncaught and leads to a confusing error message at build time.

This attempts to improve DX surrounding this error by linking out to a
page that explains when it might happen. I've also tweaked
`StaticGenerationBailout` to always throw a fully descriptive reason as
opposed to just `DynamicServerError: Dynamic server usage: cookies`

Closes NEXT-1181
Fixes #49373

---------

Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-08-08 12:49:53 +02:00
Jiachi Liu
9035f14dda
Fix not-found rendering in production with edge (#53687)
Edge runtime doesn't have `/404` entry points as not-found, if server hits 404 it's hitting `/_not-found` entry point of edge bundles unlike nodejs server rendering.

In app-render it needs the information that when `pathname` (or can call `pagePath`) is `/404` it can render the `not-found.js` components properly

Separate the not-found test suite into 2 parts, a basic case testing all urls with `not-found.js` boundary, and another conflict route case with `not-found.js` with `not-found/` route at the same time

Fixes #53652
Fixes #53210
2023-08-07 20:05:22 +00:00
Janicklas Ralph
033732a3e5
Adding GoogleMaps and Youtube embed components (#52909)
Adding GoogleMapsEmbed and YoutubeEmber components into `@next/third-parties`
Adding tests and README
Each component is tagged with `data-ntpc` attribute

cc: @kara @housseindjirdeh @huozhi @gnoff 




Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-08-07 19:38:13 +00:00
Tobias Koppers
25e6db4274
Turbopack: add edge app routes (#53387)
### What?

* adds middleware manifest and other missing items for edge app routes
* fixes react-server condition in edge context
* fixes node.js route context

---------

Co-authored-by: Alex Kirszenberg <alex.kirszenberg@vercel.com>
2023-08-07 13:00:06 +02:00
yudai yamamoto
b993afbf7c
Fix action failures due to state tree encoding (#53655)
fixes #53654

### Related PRs
- #51017
2023-08-07 10:02:48 +00:00
JJ Kasper
7bf3d77b5e
Revert "Implement new forking technique for vendored packages. (#51083)" (#53640)
This reverts commit e06880ea4c.

reverts: https://github.com/vercel/next.js/pull/51083

x-ref: [slack
thread](https://vercel.slack.com/archives/C04DUD7EB1B/p1691349686136519)
x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1691349579712979?thread_ts=1691217652.833079&cid=C03S8ED1DKM)
2023-08-06 13:00:12 -07:00
Zack Tanner
6beec2f6b1
Reduce flakiness of app-fetch-logging test (#53612)
Updates `app-fetch-logging` tests to not start/stop the server each run and instead slice the relevant log output. Also moves log parsing inside of `check` since logs get appended asynchronously.

ref: https://github.com/vercel/next.js/actions/runs/5767647854/job/15637691191#step:28:662
2023-08-05 16:12:21 +00:00
Josh Story
e06880ea4c
Implement new forking technique for vendored packages. (#51083)
## Vendoring

Updates all module resolvers (node, webpack, nft for entrypoints, and nft for next-server) to consider whether vendored packages are suitable for a given resolve request and resolves them in an import semantics preserving way.

### Problem

Prior to the proposed change, vendoring has been accomplished but aliasing module requests from one specifier to a different specifier. For instance if we are using the built-in react packages for a build/runtime we might replace `require('react')` with `require('next/dist/compiled/react')`.

However this aliasing introduces a subtle bug. The React package has an export map that considers the condition `react-server` and when you require/import `'react'` the conditions should be considered and the underlying implementation of react may differ from one environment to another. In particular if you are resolving with the `react-server` condition you will be resolving the `react.shared-subset.js` implementation of React. This aliasing however breaks these semantics because it turns a bare specifier resolution of `react` with path `'.'` into a resolution with bare specifier `next` with path `'/dist/compiled/react'`. Module resolvers consider export maps of the package being imported from but in the case of `next` there is no consideration for the condition `react-server` and this resolution ends up pulling in the `index.js` implementation inside the React package by doing a simple path resolution to that package folder.

To work around this bug there is a prevalence of encoding the "right" resolution into the import itself. We for instance directly alias `react` to `next/dist/compiled/react/react.shared-subset.js` in certain cases. Other times we directly specify the runtime variant for instance `react-server-dom-webpack/server.edge` rather than `react-server-dom-wegbpack/server`, bypassing the export map altogether by selecting the runtime specific variant. However some code is meant to run in more than one runtime, for instance anything that is part of the client bundle which executes on the server during SSR and in the browser. There are workaround like using `require` conditionally or `import(...)` dynamically but these all have consequences for bundling and treeshaking and they still require careful consideration of the environment you are running in and which variant needs to load.

The result is that there is a large amount of manual pinning of aliases and additional complexity in the code and an inability to trust the package to specify the right resolution potentially causing conflicts in future versions as packages are updated.

It should be noted that aliasing is not in and of itself problematic when we are trying to implement a sort of lightweight forking based on build or runtime conditions. We have good examples of this for instance with the `next/head` package which within App Router should export a noop function. The problem is when we are trying to vendor an entire package and have the package behave semantically the same as if you had installed it yourself via node_modules

### Solution

The fix is seemingly straight forward. We need to stop aliasing these module specifiers and instead customize the resolution process to resolve from a location that will contain the desired vendored packages. We can then start simplifying our imports to use top level package resources and generally and let import conditions control the process of providing the right variant in the right context.

It should be said that vendoring is conditional. Currently we only vendor react pacakges for App Router runtimes. The implementation needs to be able to conditionally determine where a package resolves based on whether we're in an App Router context vs a Page Router one.

Additionally the implementation needs to support alternate packages such as supporting the experimental channel for React when using features that require this version.

### Implementation

The first step is to put the vendored packages inside a node_modules folder. This is essential to the correct resolving of packages by most tools that implement module resolution. For packages that are meant to be vendored, meaning whole package substitution we move the from `next/(src|dist)/compiled/...` to `next/(src|dist)/vendored/node_modules`. The purpose of this move is to clarify that vendored packages operate with a different implementation. This initial PR moves the react dependencies for App Router and `client-only` and `server-only` packages into this folder. In the future we can decide which other precompiled dependencies are best implemented as vendored packages and move them over.

It should be noted that because of our use of `JestWorker` we can get warnings for duplicate package names so we modify the vendored pacakges for react adding either `-vendored` or `-experimental-vendored` depending on which release channel the package came from. While this will require us to alter the request string for a module specifier it will still be treating the react package as the bare specifier and thus use the export map as required.

#### module resolvers
The next thing we need to do is have all systems that do module resolution implement an custom module resolution step. There are five different resolvers that need to be considered

##### node runtime
Updated the require-hook to resolve from the vendored directory without rewriting the request string to alter which package is identified in the bare specifier. For react packages we only do this vendoring if the `process.env.__NEXT_PRIVATE_PREBUNDLED_REACT` envvar is set indicating the runtime is server App Router builds. If we need a single node runtime to be able to conditionally resolve to both vendored and non vendored versions we will need to combine this with aliasing and encode whether the request is for the vendored version in the request string. Our current architecture does not require this though so we will just rely on the envvar for now

##### webpack runtime
Removed all aliases configured for react packages. Rely on the node-runtime to properly alias external react dependencies. Add a resolver plugin `NextAppResolverPlugin` to preempt perform resolution from the context of the vendored directory when encountering a vendored eligible package.

##### turbopack runtime
updated the aliasing rules for react packages to resolve from the vendored directory when in an App Router context. This implementation is all essentially config b/c the capability of doing the resolve from any position (i.e. the vendored directory) already exists

##### nft entrypoints runtime
track chunks to trace for App Router separate from Pages Router. For the trace for App Router chunks use a custom resolve hook in nft which performs the resolution from the vendored directory when appropriate.

##### nft next-server runtime
The current implementation for next-server traces both node_modules and vendored version of packages so all versions are included. This is necessary because the next server can run in either context (App vs Page router) and may depend on any possible variants. We could in theory make two traces rather than a combined one but this will require additional downstream changes so for now it is the most conservative thing to do and is correct

Once we have the correct resolution semantics for all resolvers we can start to remove instances targeting our precompiled instances for instance making `import ... from "next/dist/compiled/react-server-dom-webpack/client"` and replacing with `import ... from "react-server-dom-webpack/client"`

We can also stop requiring runtime specific variants like `import ... from "react-server-dom-webpack/client.edge"` replacing it with the generic export `"react-server-dom-webpack/client"`

There are still two special case aliases related to react
1. In profiling mode (browser only) we rewrite `react-dom` to `react-dom/profiling` and `scheduler/tracing` to `scheduler/tracing-profiling`. This can be moved to using export maps and conditions once react publishses updates that implement this on the package side.
2. When resolving `react-dom` on the server we rewrite this to `react-dom/server-rendering-stub`. This is to avoid loading the entire react-dom client bundle on the server when most of it goes unused. In the next major react will update this top level export to only contain the parts that are usable in any runtime and this alias can be dropped entirely

There are two non-react packages currently be vendored that I have maintained but think we ought to discuss the validity of vendoring. The `client-only` and `server-only` packages are vendored so you can use them without having to remember to install them into your project. This is convenient but does perhaps become surprising if you don't realize what is happening. We should consider not doing this but we can make that decision in another discussion/PR.

#### Webpack Layers
One of the things our webpack config implements for App Router is layers which allow us to have separate instances of packages for the server components graph and the client (ssr) graph. The way we were managing layer selection was a but arbitrary so in addition to the other webpack changes the way you cause a file to always end up in a specific layer is to end it with `.serverlayer`, `.clientlayer` or `.sharedlayer`. These act as layer portals so something in the server layer can import `foo.clientlayer` and that module will in fact be bundled in the client layer.

#### Packaging Changes
Most package managers are fine with this resolution redirect however yarn berry (yarn 2+ with PnP) will not resolve packages that are not defined in a package.json as a dependency. This was not a problem with the prior strategy because it was never resolving these vendored packages it was always resolving the next package and then just pointing to a file within it that happened to be from react or a related package.

To get around this issue vendored packages are both committed in src and packed as a tgz file. Then in the next package.json we define these vendored packages as `optionalDependency` pointing to these tarballs. For yarn PnP these packed versions will get used and resolved rather than the locally commited src files. For other package managers the optional dependencies may or may not get installed but the resolution will still resolve to the checked in src files. This isn't a particularly satisfying implemenation and if pnpm were to be updated to have consistent behavior installing from tarballs we could actually move the vendoring entirely to dependencies and simplify our resolvers a fair bit. But this will require an upstream chagne in pnpm and would take time to propogate in the community since many use older versions

#### Upstream Changes

As part of this work I landed some other changes upstream that were necessary. One was to make our packing use `npm` to match our publishing step. This also allows us to pack `node_modules` folders which is normally not supported but is possible if you define the folder in the package.json's files property.

See: #52563

Additionally nft did not provide a way to use the internal resolver if you were going to use the resolve hook so that is now exposed

See: https://github.com/vercel/nft/pull/354

#### additional PR trivia
* When we prepare to make an isolated next install for integration tests we exclude node_modules by default so we have a special case to allow `/vendored/node_modules`

* The webpack module rules were refactored to be a little easier to reason about and while they do work as is it would be better for some of them to be wrapped in a `oneOf` rule however there is a bug in our css loader implementation that causes these oneOf rules to get deleted. We should fix this up in a followup to make the rules a little more robuts.


## Edits
* I removed `.sharedlayer` since this concept is leaky (not really related to the client/server boundary split) and it is getting refactored anyway soon into a precompiled runtime.
2023-08-04 23:47:10 +00:00
JJ Kasper
e4aecabc7d
Update ISR revalidateTag handling (#53595)
This ensures `revalidateTag()` doesn't cause an async/background revalidation unexpectedly and instead does a blocking revalidate so fresh data is shown right away when a path is using ISR. 

Test deployment with patch can be seen here: https://next-revalidation-test-47rqf8q5s-vtest314-ijjk-testing.vercel.app/

Fixes: https://github.com/vercel/next.js/issues/53187
2023-08-04 23:31:18 +00:00
Wyatt Johnson
1df2686bc9
Remove Base Path from usePathname output (#53582)
This removes the `basePath` from the output of `usePathname`. Previously this always resulted in hydration errors, this now strips the `basePath` when it's found/configured.

Now when you configure `basePath`, you don't have to factor it into your application logic and can instead rely on the values always returning the pathname without it.

Fixes #46562
2023-08-04 22:10:00 +00:00
Jiachi Liu
99372fbedf
Add test for catching metadata error in error boundaries (#53581) 2023-08-04 20:57:49 +02:00
Jiachi Liu
ab14895ada
Fix dynamic route not-found boundary matching (#53564)
### What & Why

The dynamic not-found boundary didn't work as expected as it was using the `pathname` to match how many levels of the segements should be matched. For dynamic routes this doesn't work, unlike normal page, the unmatched segment can also hit the not found boundary in the same level.

### How

Use `segment` of loader tree nodes to determine if not-found boundary searching is reached to the end instead of using `pathname`. 
> NOTE: For production `/_not-found` case since it's a valid page on production and still has the original tree, so we handle that as a special case to use the not found loader tree (with empty child routes) to render.

Fixes #53344
2023-08-04 16:07:58 +00:00
Jiachi Liu
cc4879d1dc
Move metadata error under error boundaries (#53551)
### What & Why

Using `notFound()` in `generateMetadata()` or in page will lead to unacught global not found error when you do navigation, this is because `head` cache is actually not inside the error boundary as designed to stay in the beginning of the content. But in this way we won't be able to catch the notFound error thrown from metadata API. So we created a new approach that can separate the error itself from metadata result, and throw as error under the error boundaries, and metadata itself will always be safe to render.

### How

We use a new promise that resolve the `error` thrown from metadata resolving, and let the render result always return successfully and always has a value, using default metadata when there's error thrown. Create two components, one rendering the metadata tags, and another `MetadataOutlet` to throw the error when the resolving is failed but rendered under error boundaries.



```jsx
const [MetadataTree, MetadataOutlet] = createMetadataComponents(/*...*/)
const ComponentTree = createComponentTree({ metadataOutlet: <MetadataOutlet /> })

renderRSC(
  <AppRourer head={<MetadataTree />}>
    <ComponentTree />
  </AppRourer>
)
```

`metadataOutlet` will stay next to page component in the layout hierarchy

discuessed with @gnoff 

Minor changes: 

* When there's rendering root layout layer, not found boundary should be `DefaultNotFound` if the custom one doesn't exisit


Fixes #53371
Fixes #53149
2023-08-04 15:32:36 +00:00
Zack Tanner
f8a4e0f3c3
fix: fetch deduping in dev (#53549)
This attempts to fix an issue where fetch requests were not being
deduped on the first request to the page (but subsequent requests were
properly deduped).

This seems to be caused by the async context from
`staticGenerationStore` being lost by the time the patched fetch is
called, and so it was falling back to a regular fetch and skipping the
cache. This attempts to fix that by falling back to
`fetch.__nextGetStaticStore()`.

[slack
x-ref](https://vercel.slack.com/archives/C03KAR5DCKC/p1691007445597619)
2023-08-03 15:10:55 -07:00
Shu Ding
9f24840032
Fix resource being preloaded multiple times during development (#53525)
Closes #49607. Since `ReactDOM.preload`s might be called multiple times during the rendering process, we need to ensure the timestamp queries are stable across the request so Flight can properly deduplicate them.
2023-08-03 15:15:44 +00:00
Shu Ding
0ecde6bd32
Add test for client router state invalidation caused by cookie mutations (#53494)
Closes #53261. Closes NEXT-1478.
2023-08-03 11:02:19 +00:00
Zack Tanner
59c767b258
Allow next/headers in middleware & draftMode in edge runtime (#53465)
## What
Using methods from `next/headers` in middleware would throw a `requestAsyncStorage` invariant. Additionally, using `draftMode()` in middleware/an edge runtime is not possible

## Why
We do not expose `requestAsyncStorage` to the middleware adapter. Also, the prerender manifest wasn't available to the `EdgeRouteModuleWrapper` & middleware adapter, so it wasn't possible to enable/disable it.

## How
This makes `requestAsyncStorage` available for middleware, and makes the preview mode data available from build to the edge runtime/middleware. 

Fixes #52557
2023-08-02 20:22:35 +00:00
Andrew Gadzik
9bde7dcc0f
Add warning logs for incorrect page exports (#53449)
<!-- 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 #53448

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-08-01 13:59:20 -07:00
Tim Neutkens
a721a749d8
router: apply server actions in a similar way to router.refresh() (#53373)
## What?

I was investigating reports of server actions with `revalidatePath` /
`revalidateTag` not invalidating the client-side router cache. Managed
to reproduce the issue here:
https://github.com/timneutkens/server-actions-test (requires Vercel KV
to run).

While looking at the reducer I noticed a few things that seemed off:

- setTimeout to trigger another `dispatch` on the same reducer with the
fetched data. This means that it would not be part of the same React
Transition.
- redirects weren't applying the data that comes back from the server
(that allows for single hop navigation)
- prefetchCache was invalidated, but the router cache which is used for
back/forward navigation was not invalidated, causing back/forward to not
get the new data.
- all changes in the action-reducer mutate. The part that shouldn't
mutate was part of that setTimeout dispatch.

This PR aims to solve all of these by reworking the actions reducer to
be handled similarly to `router.refresh()`. Since `router.refresh()` was
already modeled to be similar to `revalidatePath('/')`.

The flow is now more like the other reducers:
- Fetch data
- Wait for the data to come back
- Apply the data to the cache and keep it in a mutable variable
- Return the new cache and otherwise values like the url

In particular the actions reducer handles a few extra specifics:
- Resolving the server action promise, so that the value is passed to
the application code waiting for the result.
- Applying redirects from `redirect()` calls.
- Invalidating the router cache

## Followup changes

- Currently when calling `revalidatePath('/dashboard')` the entire
router cache is invalidated instead of only `/dashboard` and further
down, this is not in scope for this PR but still has to be added.
- `notFound()`, I'm not sure how this is supposed to work exactly, it
doesn't really make sense to me to allow it in server actions.

Kudos @feedthejim for helping investigate for this PR 😌 

<!-- 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-07-31 21:52:31 +02:00
Jiachi Liu
de873d02b3
Prefer svg icon instead favicon if possible (#53343)
### What?

Change the `favicon.ico` metadata `sizes` property from always `"any"`
to using the actual size possible

### Why?

When chrome/firefox browsers search for icon it needs the proper
metadata to determine which one to use, giving `favicon.ico` sizes with
`"any"` will prevent it loading the `icon.svg` as default icon when
available.

Changing to set proper size of `favicon.ico` (as the `favicon.ico` sizes
could be 16x16, 32x32, etc.) fixes the issue.

It works (loading svg icon) for chrome and firefox:

Firefox
<img width="505" alt="image"
src="https://github.com/vercel/next.js/assets/4800338/6873e595-479d-4d9e-ae5c-39e5f938fcf5">

Chrome
<img width="460" alt="image"
src="https://github.com/vercel/next.js/assets/4800338/03bbe4c7-ef76-4f34-a611-337b0d4b97a3">

It loads favicon.ico for Safari:
Using the `test/e2e/app-dir/metadata` app it shows the favicon.ico for
Safari
<img width="1000" alt="image"
src="https://github.com/vercel/next.js/assets/4800338/92cc8714-ea5e-432d-8144-2a4a42ee4ce2">

Can't have it as an e2e test as I tested it always loads the
`favicon.ico` in headless mode which can't determine the behaviors

Fixes #52002

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-07-31 07:32:39 -07:00
Jimmy Lai
f2c5eb840f
actions: fix revalidate after redirect (#53368)
This PR reverts a change that removed the `content-length` header filtering from the req made by the actions when redirecting. This change made some tests flaky and presumably also broke server actions in subtle ways.

There's still one other bug when redirecting after revalidating that will happen in you revalidate a page that was already rendered before where we will still show stale content. @timneutkens is fixing that one.

NEXT-1483
2023-07-31 10:08:52 +00:00
JJ Kasper
4a926efb83
Update flakey app-action and prerender-prefetch tests (#53340)
x-ref:
https://github.com/vercel/next.js/actions/runs/5699334417/job/15448149485#step:27:600
x-ref:
https://github.com/vercel/next.js/actions/runs/5702064037/job/15453403529?pr=53340
2023-07-30 12:51:24 -07:00
Justin Ridgewell
3906374740
Try to fix flakey socket hang up failures in stream cancel tests (#53318)
### What?

I think the flakiness from the new stream cancel tests is due to a uncaught error thrown from the priming. If it is during the priming, then we need to also explicitly check if we restart the test and reset the `streamable`.

### Why?

Flakey tests suck.

### How?

- Adds a `on('error', reject)` to catch the socket error and associate it with the test
- Explicitly checks for the `?write=` param to see if we're priming or getting results
2023-07-28 22:59:24 +00:00
Zack Tanner
afa9f96515
add a "skip" cache type to verbose logging mode (#53275)
When verbose logging is enabled and a cache MISS is logged due to either `revalidate: 0`,`cache: no-store` or `cache-control: no-store`, this logs a slightly different message, ie:

```
- GET / 200 in 804ms
   ├─── GET <url> in 205ms (cache: SKIP, reason: cache: no-cache)
   ├────── GET <url> 200 in 0ms (cache: HIT)
   ├────── GET <url> 200 in 373ms (cache: SKIP, reason: revalidate: 0)
   └────── GET <url> 200 in 111ms (cache: SKIP, reason: cache-control: no-cache (hard refresh))
 ```
 
 Closes NEXT-1412
2023-07-28 20:01:31 +00:00
Jiachi Liu
c5308eb4a8
Respect fetching logging config and Polish logs format (#52973)
Respect to the logging option which was introduced in #49250 that only logs when `experimental.logging` set to `"verbose"`

Polish the logging format a bit that make it more compact and lit
![image](https://github.com/vercel/next.js/assets/4800338/7b41424b-1215-415b-84c9-4619512ba0b4)


Related NEXT-1357
2023-07-27 09:02:42 +00:00
Zack Tanner
c73fdfd87c
Fix file tracing issues for not-found pages (#53231)
This fixes the `.nft.json` output for not-found pages to correctly grab the client reference manifest. We were checking for `entryPoint.name.startsWith('app/')` but then asserting on `entryPoint.name === '/_not-found'` when determining the manifest path.

This also fixes the build output to mark pages with `λ` if the corresponding page has dynamic data. This revealed a separate issue that de-opts all pages into dynamic rendering if the root `NotFound` page bails, tracked in [NEXT-1474](https://linear.app/vercel/issue/NEXT-1474/if-the-root-notfound-page-opts-into-dynamic-rendering-all-pages-do-too)

[slack x-ref](https://vercel.slack.com/archives/C03S8ED1DKM/p1683763412272429)
2023-07-27 00:08:53 +00:00
Zack Tanner
22ca85946e
Wrap incremental cache in an IPC server (#53030)
This uses an IPC server (if available) for incremental cache methods to help prevent race conditions when reading/writing from cache and also to dedupe requests in cases where multiple cache reads are in flight. This cuts down on data fetching across the different build-time workers.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-07-26 23:19:46 +00:00
Justin Ridgewell
31d2b720d9
Reimplement stream cancellation (#52281)
### What?

This reimplements our stream cancellation code for a few more cases:
1. Adds support in all stream-returning APIs
2. Fixes cancellation detection in node 16
3. Implements out-of-band detection, so can cancel in the middle of a
read

It also (finally) adds tests for all the cases I'm aware of.

### Why?

To allow disconnecting from an AI service when a client disconnects. $$$

### How?

1. Reuses a single pipe function in all paths to push data from the
dev's `ReadableStream` into our `ServerResponse`
2. Uses `ServerResponse` to detect disconnect, instead of the
`IncomingMessage` (request)
    - The `close` event fire once all incoming body data is read
- The request `abort` event will not fire after the incoming body data
has been fully read
3. Using `on('close')` on the writable destination allows us to detect
close
- Checking for `res.destroyed` in the body of the loop meant we had to
wait for the `await stream.read()` to complete before we could possibly
cancel the stream

- - -

#52157 (and #51594) had an issue with Node 16, because I was using
`res.closed` to detect when the server response was closed by the client
disconnecting. But, `closed` wasn't
[added](https://github.com/nodejs/node/pull/45672) until
[v18.13.0](https://nodejs.org/en/blog/release/v18.13.0#:~:text=%5Bcbd710bbf4%5D%20%2D%20http%3A%20make%20OutgoingMessage%20more%20streamlike%20(Robert%20Nagy)%20%2345672).
This fixes it by using `res.destroyed`.

Reverts #52277
Relands #52157
Fixes #52809

---------
2023-07-26 12:57:34 -07:00
JJ Kasper
1c5c0ba3ff
Temporarily skip flakey action revalidate (#53134)
These have been flaking for quite a while so skips them more
consistently until they are investigated.

x-ref:
https://github.com/vercel/next.js/actions/runs/5650048471/job/15305721826#step:27:590
2023-07-24 15:37:30 -07:00
Jiachi Liu
d8f4fa8946
Fix not found hangs the build with overridden node env (#53106)
### Why

In #52985 the not found solution introduces `NODE_ENV` to determine if it his the not found boundary and should render the not found, as in the next build mode, we have `/_not-found` as a special route which has a empty parallel route, but in next dev mode so far it his the `parallel-default-route`. This could dependend on the `NODE_ENV` passing to next server but not necessarily.

### What

Fixes #53082
Fixes #53083 

### How

When server actions `not-found` hits, now we create a new loader tree based on the previous one, including `layout` and other components but not the children parallel routes

For production case, to make the rendering independent from the `NODE_ENV`, we're using original pathname to check if it's `/_not-found` to determine if it's production build 404 page

To support replace the loader tree of action, did a little refactor that passing down the loader tree from top level to `bodyResult`. Then we can change the loader tree itself before rendering, in short, we tweak it from original tree to one for not-found case, so server actions could render it properly
2023-07-24 21:00:53 +00:00
akfm
1494283a74
fix: Add Next-Url to http vary in consideration of intercept routes. (#52746)
### Why

We calculate the “next url” depending on the router state and the previous router state so that when you navigate to a route, the proxy matches with that header and returns you the intercepted route if matching

### What 
- Fixes #52745




Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-07-22 21:38:23 +00:00
JJ Kasper
1398de9977
Revert "Revert "Separate routing code from render servers (#52492)"" (#53029)
Reverts vercel/next.js#53016
2023-07-21 14:02:52 -07:00
JJ Kasper
ac62406ca0
Revert "Separate routing code from render servers (#52492)" (#53016)
Temporarily reverts these changes to allow patch release first. 

Reverts: https://github.com/vercel/next.js/pull/52149
Reverts: https://github.com/vercel/next.js/pull/52492
2023-07-21 10:52:19 -07:00
Jiachi Liu
1fefb4a8d2
Reland "Refine the not-found rendering process for app router" (#52985)
Reland #52790
Reverts vercel/next.js#52977

was failed due to failed job
[vercel/next.js/actions/runs/5616458194/job/15220295829](https://github.com/vercel/next.js/actions/runs/5616458194/job/15220295829)

Should be fine to resolve with
https://github.com/vercel/next.js/pull/52979 now

Fixes #52718
Fixes #52739

---------

Co-authored-by: Alex Kirszenberg <alex.kirszenberg@vercel.com>
Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2023-07-21 10:09:30 -07:00
JJ Kasper
f57eecde5e
Separate routing code from render servers (#52492)
This breaks out routing handling from `next-server`, `next-dev-server`,
and `base-server` so that these are only handling the "render" work and
eventually these will be deleted completely in favor of the bundling
work being done.

The `router` process and separate `render` processes are still
maintained here although will be investigated further in follow-up to
see if we can reduce the need for these.

We are also changing the `require-cache` IPC to a single call instead of
call per entry to reduce overhead and also de-dupes handling for
starting the server between the standalone server and normal server.

To maintain support for existing turbopack route resolving this
implements the new route resolving in place of the existing
`route-resolver` until the new nextturbo API is fully landed.

After these initial changes we should continue to eliminate non-render
related code from `next-server`, `base-server`, and `next-dev-server`.
2023-07-20 22:13:42 -07:00
JJ Kasper
d7335b75d1
Revert "Refine the not-found rendering process for app router" (#52977)
Reverts vercel/next.js#52790

Reverting temporarily as this breaks turbopack's not found handling due
to the app tree being generated there not having the necessary parallel
routes in the `_not-found` entry x-ref:
0df8aac935/packages/next-swc/crates/next-core/src/app_structure.rs (L677-L681)

x-ref:
https://github.com/vercel/next.js/actions/runs/5616458194/job/15220295829
2023-07-20 20:39:05 -07:00
Jimmy Lai
55eebefbab
app-router: prefetching tweaks (#52949)
This PR tries to address some feedback around prefetching, like in #49607, where they were some warnings because we were over prefetching.

The tweaks in this PR:
- if there are no loading boundary below, we don't prefetch the full page anymore. I made that change a while ago but I think it wasn't the original intent from @sebmarkbage. Fixing that now. So now, we will prefetch the page content until the nearest loading boundary, only if there is any.
- there's now a queue for limiting the number of concurrent prefetches. This is to not ruin the bandwidth for complex pages. Thanks @alvarlagerlof for that one.
- also, if the prefetch is in the queue when navigating, it will get bumped.
- bonus: fixes a bug where we were wrongly stripping headers in dev for static pages

Test plan:
<img width="976" alt="CleanShot 2023-07-20 at 17 53 43@2x" src="https://github.com/vercel/next.js/assets/11064311/2ea34419-c002-4aea-94df-57576e3ecb2e">
In the screenshot you can see that:
- only 5 requests at a time are authorised
- when I clicked on 15, it got prioritised in the queue (did not cancel the rest)
- the prefetch only included the content until the loading boundary




Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-07-20 21:46:38 +00:00
Jiachi Liu
cb24c555a6
Refine the not-found rendering process for app router (#52790)
### What

This PR changes the flow of not-found rendering process. 

### Why

`not-found.js` was rendered in two ways before:
* 1 is SSR rendering the not-found as 404
* 2 is triggering the error on RSC rendering then the error will be
preserved in inline flight data, on the client it will recover the error
and trigger the proper error boundary.

The solution has been through a jounery:
No top-level not found boundary -> introduce metadata API -> then we
create a top level root not found boundary -> then we delete it due to
duplicated rendering of root layout -> now this

So the solution before this PR is still having a root not found boundary
wrapped in the `AppRouter`, it's being used in a lot of places including
HMR. As we discovered it's doing duplicated rendering of root layout,
then we removed it and it started failing with rendering `not-found` but
missing root layout. In this PR we redesign the process.

### How

Now the rendering architecture looks like:

* For normal root not-found and certain level of not-found boundary
they're still covered by `LayoutRouter`
* For other error renderings including not-found
* Fully remove the top level not-found boundary, when it renders with
404 error it goes to render the fallback page
* During rendering the fallback page it will check if it should just
renders a 404 error page or render nothing and let the error from inline
flight data to trigger the error boundary

pseudo code
```
try {
  render AppRouter > PageComponent
} catch (err) {
  create ErrorComponent by determine err
  render AppRouter > ErrorComponent
}
```

In this way if the error is thrown from top-level like the page itself
or even from metadata, we can still catch them and render the proper
error page based on the error type.

The problematic is the HMR: introduces a new development mode meta tag
`<meta name="next-error">` to indicate it's 404 so that we don't do
refresh. This reverts the change brougt in #51637 as it will also has
the duplicated rendering problem for root layout if it's included in the
top level not found boundary.

Also fixes the root layout missing issue:

Fixes #52718
Fixes #52739

---------

Co-authored-by: Shu Ding <g@shud.in>
2023-07-20 14:12:06 -07:00
Shu Ding
a96a9b0791
Fix client reference manifest for interception routes (#52961)
We have the logic to group the client compiler's entry names to make sure we generate one single manifest file for the page. This is complicated and requires a special step to "group" the entry names because a page can depend on a bunch of files from everywhere.

And currently, the normalization of "entryName → groupName" doesn't cover interception routes' conventions (`(.)`, `(..)` and `(...)`). This PR fixes that.

Closes #52862, closes #52681, closes #52958.
2023-07-20 20:06:44 +00:00
Zack Tanner
4994a428ae
improve error DX on pages with RSC build errors (#52843)
### What?
- Visiting a page in the app router without a proper component export doesn't show the dev overlay, but logs errors to the console
- When it does show the error overlay (e.g. during an HMR event), the error message was sharing the module code itself rather than the component path, making it hard to debug

### Why?
`createComponentTree` can throw these errors before the AppRouter tree is mounted, leaving the errors uncaught by the dev overlay.

### How?
This wraps the server root in the `ReactDevOverlay` when in dev mode with a minimal "HMR" for when the server component is edited (to reload the page).

Closes NEXT-308
2023-07-20 00:32:57 +00:00
Wyatt Johnson
3e821ef189
Support basePath with edge runtime for Custom App Routes (#52910)
Fixes a bug where the edge runtime didn't support `basePath` with Custom App Routes.

- Fixes #49661
2023-07-19 23:54:34 +00:00
Janicklas Ralph
009b6a19d6
Setting up third-parties package (#51194)
This PR Introduces `next/third-parties`

- `@next/third-parties` is a new package in the Next.js monorepo. 
- The package contains collection of components that can be used to
efficiently load third party libraries into your Next.js application.
- This package uses [Third Party
Capital](https://github.com/GoogleChromeLabs/third-party-capital/) under
the hood to fetch the best loading recommendations.
- This PR mainly sets up the package to run a script at build time to
auto generate the components from [Third Party
Capital](https://github.com/GoogleChromeLabs/third-party-capital/)
config.

Packages are grouped by company, for eg:

`import { GoogleMapsEmbed } from '@next/third-parties/google'`


cc: @housseindjirdeh @kara 
<!-- 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?

### Why?

### How?

Closes NEXT-
Fixes #

-->

---------
2023-07-18 10:52:39 -07:00
Facundo Giuliani
a26bac9604
Update default moduleResolution in tsconfig.json from node to bundler (#51957)
This updates our `moduleResolution` to `bundler` as this matches our heuristics much more closely so is more accurate. This shouldn't be a breaking change is it should be compatible with our previous default. 

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-07-18 15:11:09 +00:00
Shu Ding
7e9627a390
Fix tracking of ContextModule (#52795)
When doing dynamic imports like `import(variable)`, Webpack _tries_ to statically analyze it and creates a regex like context module for it (which includes all possible modules). This `ContextModule` doesn't have a resource path so we need to use the identifier to track it.

Tested with @alexkirsz's repro here https://github.com/vercel/next.js/issues/50243#issuecomment-1628675346 and confirmed that it fixes the problem.

Closes #50243.
2023-07-17 17:41:04 +00:00
JJ Kasper
dc6c22c991
Fix runtime edge not-found handling (#52754)
This ensures we properly detect `not-found` as `runtime = 'edge'` and treat it as such instead of attempting to render it like normal. 

x-ref: https://github.com/vercel/next.js/issues/52648
2023-07-16 22:21:29 +00:00
Jiachi Liu
a28ae633e4
Support metadata exports for server components not-found (#52678)
### What?

Support metadata exports for `not-found.js` conventions

### Why?

We want to define metadata such as title or description basic properties for error pages, including 404 and 500 which referrs to `error.js` and `not-found.js` convention. See more requests in #45620 

Did some research around metadata support for not-found and error convention. It's possible to support in `not-found.js` when it's server components as currently metadata is only available but for `error.js` it has to be client components for now so it's hard to support it for now as it's a boundary.

### How?

We determine the convention if we're going to render is page or `not-found` boundary then we traverse the loader tree based on the convention type. One special case is for redirection the temporary metadata is not generated yet, we leave it as default now.

Fixes #52636
2023-07-14 22:33:47 +00:00
Jiachi Liu
d10f105b74
Fix icons metadata missing from layout (#52673)
### What

This was an issue introduced in #52416 when we check the images and icons property while merging with static file conventions. Where we should check if the properties are present but we only checked if they value is falsy. So when you're using `icons` with array value it breaks.

Now we properly check all the possible case of `metadata.icons` value, so then decide if we are using the static file convention or the icons property of defined metadata.

Also add few more tests cases to cover icons resolving.
2023-07-14 12:04:31 +00:00
Shu Ding
b957f52be3
Fix per-entry client reference manifest for grouped and named segments (#52664)
References for Client Components need to be aggregated to the page entry level, and emitted as files in the correct directory for the SSR server to read from. For normal routes (e.g. `app/foo/bar/page`), we can go through and collect all entries (layout, loading, error, ...) from the current and parent segments, to aggregate all necessary client references.

However, for routes with special conventions like `app/(group)/@named/foo/bar/page`, it needs to be normalized (remove the named slot and group segments) so it can be grouped together with `app/(group)/@named2/foo/bar/loading`.
2023-07-14 01:18:37 +00:00
Jiachi Liu
79227ee74a
Catch layout error in global-error (#52654)
When there's a runtime error showing in root layout (server components), it should be able to catch by `global-error`.

For server components, we caught it and gonna render the error fallback components (either not-found or error page), and the response status is `200`, and since we'll display error dev overlay in developmenet mode so we only render `global-error` for production.

So that you can catch more errors with `global-error` and maybe do potential error tracking on client side.

Follow up of #52573
Closes NEXT-1442

minor refactor: move `appUsingSizeAdjust` into `Metadata` component so that we can just tune the flag as prop
2023-07-14 00:43:40 +00:00
Shu Ding
88084e6b7a
Fix bundle path normalization for /index routes (#52650)
We have some special bundle path handling logic for `/index` routes in
`normalizePagePath`, which is missing in the new
`AppBundlePathNormalizer`. This already broke `/index/page.js` in dev in
the past, and now become noticeable in prod as well because of the
manifest change.


b98469c86b/packages/next/src/shared/lib/page-path/normalize-page-path.ts (L5-L14)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-07-13 17:36:43 +02:00
Jiachi Liu
9313c51bc4
Ensure root layout only render once per request (#52589)
Introduce a new way to search for `not-found` component that based on
the request pathname and current loader tree of that route. And we
search the proper not-found in the finall catch closure of app
rendering, so that we don't have to pass down the root layout to
app-router to create the extra error boundary.

This ensures the root layout doesn't have duplicated rendering for
normal requests

Fixes NEXT-1220
Fixes #49115
2023-07-13 17:34:31 +02:00
Jiachi Liu
76eec86a6b
Set sizes prop to any for svg icons (#52609)
For `link` tag's `sizes` property, the property is defined as:

> This attribute defines the sizes of the icons for visual media contained in the resource. It must be present only if the [rel](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#rel) contains a value of icon or a non-standard type such as Apple's apple-touch-icon. It may have the following values:
`any`, meaning that the icon can be scaled to any size as it is in a vector format, like image/svg+xml.

x-ref: https://html.spec.whatwg.org/#attr-link-sizes
x-ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link (`sizes` property definition section) 

Closes #52002
Closes NEXT-133
2023-07-13 00:33:27 +00:00
Wyatt Johnson
bb0fecc68f
Move App Pages rendering into bundle (#52290)
Transitions the App Pages renderer into the entrypoint bundle.

- Adjusts the static path detection to handle route module's with App Pages
- Fixes bug in font manifest loading on Edge
2023-07-12 18:28:06 +00:00
Shu Ding
b7e2627422
Avoid loading Next.js config again in render workers (#52587)
This PR ensures that both Webpack and the config won't be initiated in render workers. This is great for performance but also avoids potential issues (e.g. Next.js plugin with side effects). Instead, we pass a serialized config from the router worker to the render workers.

Closes #52366.
2023-07-12 11:21:05 +00:00
Jiachi Liu
b9760b2910
Support global-error for ssr fallback (#52573)
Previously `global-error` only caught the error on client side, this PR adds the support for catching the errors thrown during client components SSR or server components RSC rendering.

Closes #46572
Closes #50119
Closes #50723
2023-07-12 01:54:07 +00:00
Florentin / 珞辰
0648a109eb
add version to function config manifest (#52507)
Adds `version` to function config manifest as suggested in https://github.com/vercel/vercel/pull/10069#discussion_r1255021336
2023-07-10 20:28:59 +00:00
Shu Ding
0fe6e850fe
Fix tracking of client reference manifest (#52505)
The problem was introduced in #52450, that the client reference manifest isn't being tracked and included in the function.

Verified that this fixes the issue.
2023-07-10 14:27:08 +00:00
Tim Neutkens
73e2979cb8
Ensure useParams return array for catch-all routes (#52494)
## What?

Ensures `useParams` matches `params` passed to the page, also matches the docs.

Fixes #50856
Fixes NEXT-1419
2023-07-10 12:04:44 +00:00
Shu Ding
990c58c5ef
Split the client reference manifest file to be generated per-entry (#52450)
This PR changes client manifest generation process. Instead of one big
manifest file that contains client references for the entire app, we're
now generating one manifest file per entry which only covers client
components that can be reached in the module graph.
2023-07-10 09:48:03 +02:00
Jiachi Liu
85033e3add
Override file based images with social images property (#52416)
Metadata API should provide a way to override the filebased metadata
images. As usually for child routes, if there's new social images or
icons are provided, the ones from parent routes should be overridden /
skipped.

The `metadata` object export or `generateMetadata` should be able to do
that. Sometimes users still add other og info (besides images) to
metadata export (both object and `generateMetadata`).
I think we should check if they really have returned images property,
then decide to override.

- For the same level of routes:
- If there's no `openGraph.images` in the returned value, merge with
file based images
- If there's any `openGraph.images` in the returned value, ignore file
based ones

- For child level of routes:
Always override the parent level, ignoring parent level file based
images unless they use `generateMetadata` to merge from
`resolvingParentMetadata` value, then the parent level's file based ones
will present there


Closes NEXT-1418
2023-07-10 09:09:01 +02:00
Jiachi Liu
632a582807
Fix metadata layer webpack rule for server-only (#52403)
After we separating the metadata routes to a separate layer, we didn't apply the webpack alias rules properly to it as it's should still be treated as pure "server" side

This PR fixes the aliasing for that new metadata layer and make it working properly with "server-only"

Fixes #52390
2023-07-09 18:23:51 +00:00
Wyatt Johnson
f45a7fce9a
Temporarily revert change to pages render (#52407)
This reverts the change to the pages render until a more substantial refactor can ensure that using the custom `app.render` method will attach a match to the request metadata.

- Fixes #52384
2023-07-07 17:58:36 +00:00
Zack Tanner
412992ad6e
fix: prevent infinite dev refresh on nested parallel routes (#52362)
### What?
HMR causes infinite reloads for parallel routes when the corresponding page component is nested

### Why?
In 4900fa21b078fd1ec1adc5d570fcfb560be8aeb6, code was added to remove `/@children` from the page path (if present) but in 59b36349eb86427ac7b679ac62fa6628c9fc4886, `normalizeParallelKey` removes the @ prefix from children, so this doesn't seem to be catching the scenario it was intended to prevent

### How?
This updates the existing replace logic to consider `/children/page` rather than `/@children/page` -- it doesn't seem like `/@children` is a valid scenario given the `normalizeParallelKey` behavior

Fixes #52342 and addresses the concerns in https://github.com/vercel/next.js/pull/52061#issuecomment-1619145129
2023-07-07 07:41:21 +00:00
Steven
27217146cf
fix: metadatabase warning message (#52363)
This PR is a small grammar change to the warning message since "fallbacks to" is not grammatically correct.
2023-07-07 00:08:10 +00:00
Sebastian Markbåge
b095e9e980
Test Progressive Enhancement of Server Actions (#52062)
Adds a regression test for testing progressive enhancement of Server
Actions. Both when passed from a Server Component and when imported into
a Client Component.

#51723 landed a bit too early which broke this but it'll be fixed again
once React is upgraded.

Co-authored-by: Shu Ding <g@shud.in>
2023-07-06 18:21:59 +02:00
Jimmy Lai
2743d0c037
msic: disable otel esm test (#52325)
x-ref:
https://github.com/vercel/next.js/actions/runs/5475379084/jobs/9971770362?pr=52320

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-07-06 15:56:53 +02:00
Jiachi Liu
927fc9e220
skip hot reload sync event for applying hmr updates (#52270)
### Issue

x-ref:
https://github.com/vercel/next.js/actions/runs/5469070005/jobs/9957658991?pr=52282#step:27:526
metadata tests are failing as flaky recently, then after digging into
it, noticed it as a hmr issue.

**Steps to repro with metadata test app**

The later (after the 1st one) visited page with client components
imports sometimes throw an full reload refresh warning
> Fast Refresh will perform a full reload when you edit a file that's
imported by modules
outside of the React rendering tree.

Turns out there's some unexpected hmr events when `"sync"` event is
triggered, and client tries to apply the updates but then failed. This
PR excludes the triggering by `"sync"` for RSC pages updates. `'sync'`
event with errors/warnings will still be handled and then `'built'`
event will be handled with hot reload updates

Possibly related to #40184
2023-07-05 17:06:11 -07:00
JJ Kasper
5046ee13aa
Revert "Fix stream cancellation in RenderResult.pipe() and sendResponse()" (#52277)
The test here seems to be failing after merge 

x-ref:
https://github.com/vercel/next.js/actions/runs/5467319221/jobs/9953529745
x-ref:
https://github.com/vercel/next.js/actions/runs/5467152519/jobs/9953132439

Reverts vercel/next.js#52157
2023-07-05 11:12:39 -07:00
Justin Ridgewell
ff1f75a873
Fix stream cancellation in RenderResult.pipe() and sendResponse() (#52157)
### What?

I've found 2 more spots that didn't properly cancel the streaming response when the client disconnects. This fixes `RenderResult.pipe()` (called during dynamic render results) and `sendResponse()` (used during Route Handlers using `nodejs` runtime).

It also (finally) adds tests for all the cases I'm aware of.

### Why?

To allow disconnecting from an AI service when a client disconnects. $$$

### How?

Just checks for `response.closed`, which will be closed when the client's connection disconnects.
2023-07-05 17:15:48 +00:00
Shu Ding
8aa9a52c36
Fix tree shaking for image generation module (#51950)
### Issue

When the og module is a shared module being imported in both page and metadata image routes, it will be shared in the module graph. Especially in the edge runtime, since the `default` export is being used in the metadata image routes, then it can't be easily tree-shaked out.

### Solution

Separate the image route to a separate layer which won't share modules with the page, so that image route is always bundling separately and the `@vercel/og` module only stays inside in that layer, when import image metadata named exports (size / alt / etc..) it can be still tree shaked.

Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-07-04 18:19:08 +00:00
Zack Tanner
0b87ba29c4
fix: infinite dev reloads when parallel route is treated a page entry (#52061)
### What?
When there's a parallel route adjacent to a tree that has no page
component, it's treated as an invalid entry in `handleAppPing` during
dev HMR, which causes an infinite refresh cycle

### Why?
In #51413, an update was made to `next-app-loader` to support layout
files in parallel routes. Part of this change updated the parallel
segment matching logic to mark the parallel page entry as `[
'@parallel', [ 'page$' ] ]` rather than `[ '@parallel', 'page$' ]`.

This resulted in `handleAppPing` looking for the corresponding page
entry at `client@app@/@parallel/page$/page` (note the `PAGE_SEGMENT`
marker) rather than `client@app@/@parallel/page`, causing the path to be
marked invalid on HMR pings, and triggering an endless fastRefresh.

### How?
A simple patch to fix this would fix this is to update `getEntryKey` to
replace any `PAGE_SEGMENT`'s that leak into the entry which I did in
59a972f53339cf6e444e3bf5be45bf115a24c31a.

The other option that's currently implemented here is to only insert
PAGE_SEGMENT as an array in the scenario where there isn't a page
adjacent to the parallel segment. This is to ensure that the
`parallelKey` is `children` rather than the `@parallel` slot when in
[`createSubtreePropsFromSegmentPath`](59a972f533/packages/next/src/build/webpack/loaders/next-app-loader.ts (L298)).
This seems to not cause any regressions with the issue being fixed in
51413, and also solves this case, but I'm just not 100% sure if this
might break another scenario that I'm not thinking of.

Closes NEXT-1337
Fixes #51951
2023-07-04 12:37:00 +02:00
Tim Neutkens
5d06b79e92
Support scroll: false for Link component for app router (#51869)
### What

Support `scroll={false}` for Link component in app router. This can be
used when you don't need to scroll back to top again when route url
changes. For instance hash query changes, if you want to keep the
scrolling as it is, you can use this option.

### How

Handling the `scroll` option in navigation reducer on client side.  

Fixes #50105
Fixes NEXT-1377

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-07-04 10:25:25 +02:00
Shu Ding
3a87f0005e
Change the Server Actions feature flag to be validated at compile time (#52147)
Currently we are validating the `experimental.serverActions` flag when creating the actual entries for Server Actions, this causes two problems. One is that syntax errors caught at compilation time are still shown, even if you don't have this flag enabled. Another problem is we still traverse the client graph to collect these action modules even if the flag isn't enabled.

This PR moves that check to be happening at compilation time, which addresses the two above but also brings the extra benefit of showing the exact span and module trace that errors:

<img width="974" alt="CleanShot 2023-07-03 at 20 26 34@2x" src="https://github.com/vercel/next.js/assets/3676859/1676b1f6-e205-4963-bce4-5b515a698e9c">
2023-07-03 20:29:57 +00:00
Jiachi Liu
10605a15c1
Renable flaky tests disabled before (#51680)
As we moved to new CI runner in `#50436`, try to re-enable few flaky tests we disabled before
2023-07-03 09:29:28 +00:00
Shu Ding
8eb9730607
Avoid unnecessary resolveExternal calls (#52053) 2023-07-03 10:34:46 +02:00
JJ Kasper
934a7da4d7
Ensure non-implicit unstable_cache tags are propagated (#52058)
This ensures we correctly pass up the non-implicit tags for
`unstable_cache` so that `revalidateTag` can be triggered for ISR paths
properly.

x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1687900021175649)
2023-06-30 15:27:16 -07:00
Wyatt Johnson
efe00e941b
Enable Pages Route Module Rendering for Edge (#51894)
This adapts the new route module rendering to support edge as well.

- Added a new `routeModule` export to the Edge SSR Loader
- Updated some tests to validate page state

Fixes NEXT-1327
2023-06-30 18:42:58 +00:00
Jiachi Liu
4bfc1eaf54
Revert "Optimize inlined Flight data array format" (#52039)
Reverts vercel/next.js#52028

revert temporarily and will re-land in next round release
2023-06-30 16:12:31 +02:00
Shu Ding
0123a9d5c9
Optimize inlined Flight data array format (#52028)
When looking at [some sites](https://rsc-llm-on-the-edge.vercel.app/) with a large amount of chunks streamed, I noticed that the inlined Flight data array can be optimized quite a lot. Currently we do:

```js
self.__next_f.push([1,"d5:[\"4\",[\"$\",\"$a\",null,..."])
```

1. The `self.` isn't needed (except for the initial bootstrap tag) as React itself has `<script>$RC("B:f","S:f")</script>` too.
2. After the bootstrap script tag, all items are an array with `[1, flight_data]` and `flight_data` is always a string. We can just push only these strings.
3. We use `JSON.stringify(flight_payload)` to inline the payload where the payload itself is a string with a lot of double quotes (`"`), this results in a huge amount of backslashes (`\`). Here we can instead replace it to use a pair of single quotes on the outside and un-escape the double quotes inside.

Here's a side-by-side comparison of a small page:

<img width="1710" alt="CleanShot 2023-06-30 at 11 41 02@2x" src="https://github.com/vercel/next.js/assets/3676859/398356ec-91d5-435c-892d-16fb996029e8">

For a real production page I saw the HTML payload reduced by 11,031 bytes, a 3% improvement.

Note that all the tests are not considering gzip here, so the actual traffic impact will be smaller.
2023-06-30 13:27:38 +00:00
Jiachi Liu
54a963b666
Update displayed error message for rsc case (#52004)
We show the "Application error: a client-side exception has occurred (see the browser console for more information)" error incorrectly when a server-side error occurs (a digest is present) when we should be showing an error saying it is in fact a server side error and should check the logs there. This change will make the error message more accurate for users to look up

Fixes NEXT-1263
2023-06-30 11:31:19 +00:00