Commit graph

1476 commits

Author SHA1 Message Date
Jiachi Liu
87e0b4495a
Fix mixed module swc compilation for app router (#58967) 2023-12-01 15:23:43 +01:00
Jiachi Liu
778fb87131
Support generating multi-meta tahs for metadata api other prop (#59106) 2023-11-30 16:44:49 +01:00
Jiachi Liu
d37b507629
Fix next internal is missing in flight manifest (#59085) 2023-11-30 16:36:14 +01:00
Zack Tanner
d605ef6101
fix interception routes with rewrites (#59094)
### What?
When using interception routes & rewrites, on first interception the
router will properly handle the request. But when using the back button
and attempting another interception, it won't work

### Why?
Intercepting routes rely on the accuracy of `nextUrl` -- but when
`ACTION_RESTORE` is dispatched (in the `popstate` event), `nextUrl` is
restored from `url.pathname` rather than the flight router state.

### How?
This uses the `extractPathFromFlightRouterState` util which will
properly handle setting `nextUrl`. This util is also used when creating
the initial router state.

Closes NEXT-1747
Fixes #56072
2023-11-30 08:57:42 +01:00
Zack Tanner
8395059d33
verify action id before parsing body (#58977)
### What?
When handling a server action, in the non-progressive enhanced case,
React will attempt to parse the request body before verifying if a valid
server action is received. This results in an "Error: Connection Closed"
error being thrown, rather than ignoring the action and failing more
gracefully

### Why?
To support progressive enhancement with form actions, the `actionId`
value is added as a hidden input in the form, so the action ID from the
header shouldn't be verified until determining that we've reached the
non-PE case. ([React
ref](https://github.com/facebook/react/pull/26774)). However, in
https://github.com/vercel/next.js/pull/49187, support was added for a
URL encoded form (which is not currently used, as indicated on the PR).

Despite it not being used for server actions, it's currently possible to
trigger this codepath, ie by calling redirect in an action handler with
a 307/308 status code with some data in the URL. This would result in a
500 error.

### How?
React should not attempt to parse the URL encoded form data until after
we've verified the server action header for the non-PE case.

x-ref NEXT-1733
[Slack
context](https://vercel.slack.com/archives/C03S8ED1DKM/p1700674895218399?thread_ts=1700060786.749079&cid=C03S8ED1DKM)
2023-11-29 19:55:00 +00:00
Zack Tanner
77f8889b7c
use 303 status code for redirects in fetch actions (#59017)
### What?
A `redirect` that occurs during a fetch action will get a status code of
200, while the redirection logic is handled client-side.

### Why?
In this scenario, the redirect is handled by the client router, so no
`Location` is set on the action response. However for debugging /
logging purposes, it'd be useful to still return the same status code
used in other cases (see #58885)

### How?
Rather than selectively setting the status to 303 in the non-fetch
action case, this always applies it.

Closes NEXT-1745
2023-11-29 11:45:06 -08:00
Zack Tanner
e8c0273677
add full PPR e2e tests (#59025)
This copies over the E2E tests that were added to `vercel/vercel` in
https://github.com/vercel/vercel/pull/10808 and
https://github.com/vercel/vercel/pull/10823, with some minor adjustments
to reflect the differences in cache behavior between start & deploy.
2023-11-29 11:34:01 -08:00
Tobias Koppers
6b651b0b12
add support for instrumentation (#59070)
<!-- 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 #

-->


Closes PACK-2057
2023-11-29 20:01:38 +01:00
Zack Tanner
eafaba39cb
update status codes for redirect and permanentRedirect in action handlers (#58885)
### What?
Calling `redirect` or `permanentRedirect` with a route handler used by a server action will result in that POST request following the redirect. This could result in unexpected behavior, such as re-submitting an action (in the case where the redirected URL makes use of the same server action).

### Why?
By spec, 307 and 308 status codes will attempt to reuse the original request method & body on the redirected URL.

### How?
In all cases when calling a `redirect` handler inside of an action, we'll return a `303 See Other` response which is a typical status code when redirecting to a success / confirmation page as a result of a POST/PUT.

The other option would be to use 301 / 302 status codes, but since we're already doing a 303 status code [here](https://github.com/vercel/next.js/blob/canary/packages/next/src/server/app-render/action-handler.ts#L603), this aligns the behavior for the route handler case. 

Closes NEXT-1733
See also: https://github.com/vercel/next.js/issues/51592#issuecomment-1810212676
[Slack x-ref](https://vercel.slack.com/archives/C03S8ED1DKM/p1700060786749079)
2023-11-29 08:35:50 +00:00
Zack Tanner
809164d776
Enable PPR tests for test suites (#59030)
Cherry-picks #58708 without the dependency on https://github.com/vercel/next.js/pull/58779

Co-authored-by: Wyatt Johnson <633002+wyattjoh@users.noreply.github.com>
2023-11-29 03:22:45 +00:00
Zack Tanner
298bbe5489
fix async action queue behavior (#59038)
### What?
When the router action queue receives a bunch of async actions in quick succession, some of those requests are dropped, and as a result, anything observing pending transitions will be stuck in a pending state.

### Why?
When adding items to the action queue, the intended behavior is for new actions to be added to the end of the action queue, to be picked up by `runRemainingActions` once the in-flight action is processed. However, new actions are erroneously overwriting pending actions in the queue rather than appending them, as `actionQueue.last` might have a pending action attached to it. 

### How?
This moves the assignment of `actionQueue.last` to always be in `dispatchAction`, rather than the function that processes the action, so that we always have a single spot where `last` is assigned and to prevent it from erroneously omitted/overwritten. 

Fixes #59011
2023-11-28 22:54:04 +00:00
Zack Tanner
cd66493749
dedupe pending revalidation requests (#58990)
### What?
We currently dedupe fetch requests, but if those fetch requests contain a `revalidate` time, when that window is expired all of those fetches will be invoked without deduping.

### Why?
We track revalidations on the `staticGenerationStore` but we don't have a way to dedupe them, as it's currently just an array. When the (patched) fetch is invoked and catches a stale entry, it'll push each fetch onto the `pendingRevalidates` array which will later be invoked via `Promise.all`. 

### How?
This updates the shape of `pendingRevalidates` to be a map, that way we can reliably dedupe if we see a key that is already pending revalidation. 

Closes NEXT-1744
[slack x-ref](https://vercel.slack.com/archives/C03S8ED1DKM/p1700836529460289)
2023-11-28 14:38:59 +00:00
Shu Ding
c85caae8d6
Fix encoding in encryption of Server Actions (#59000)
Utils `stringToUint8Array` and `arrayBufferToString` assume that the values are just arbitrary fixed width data. However that doesn't work when we do unicode concatenation (`actionId + arg`) which requires Text encoder/decoder to be used.

Closes #58463, closes #58579. In general any complex unicode characters will cause the same issue, for example emojis.
2023-11-28 10:20:47 +00:00
Tim Neutkens
7bdb61c28d
Fix bugs with baseUrl and mdxRs (#58968)
## What?

Was investigating an issue with Turbopack and MDX, in the process found
a few bugs:

- When you have a `tsconfig.json` or `jsconfig.json` the `baseUrl: '.'`
is used by default which causes the top-level directories to be
available as e.g. `design-system` (without a prefix).
- This is not how TypeScript's default setting for `baseUrl` works.
While it should resolve `paths` relative to `.` when none is specified
it does not do additional resolving for the top level directories/files.
- When `"baseUrl": "."` is added to `tsconfig.js` explicitly it handles
the top level directories.
- `modularizeImports` and other SWC transforms weren't applied to `.mdx`
files when `experimental.mdxRs` is enabled, which caused compilation to
fail.
- `modularIzeImports` and other SWC transforms are not applied to `.mdx`
files when using Turbopack.
	- @kwonoj is investigating this, will be handled in a follow-up PR.

## How?

- Added a test suite for `modularizeImports` with MDX tests
- Removed the condition that disables swcLoader in webpack when using
mdxRs
- Changed the check for `tsconfig.json` / `jsconfig.json` baseUrl to
include if it was implicitly or explicitly set, disabled the module
resolving when it is implicitly set


<!-- 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 #

-->

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2023-11-28 10:50:14 +01:00
Jiachi Liu
46bcd11326
Fix nested fetch logging indentation (#58955)
The indentation of nested logging requests are not correct

### After 

<img height="200" src="https://github.com/vercel/next.js/assets/4800338/4e38cc5e-147c-4f62-81a7-b2a6d84729e0">

### Before
<img height="200" src="https://github.com/vercel/next.js/assets/4800338/5ac8f609-33a9-42b3-9421-291314ab94ba">

reported by @shuding
2023-11-27 15:53:18 +00:00
Tim Neutkens
f514684bbc
Enable .mjs extension config in Turbopack (#58825)
Enable `.mjs` resolving in Turbopack to match the current webpack config.

Added an additional tests for this.
2023-11-23 14:17:36 +00:00
Jiachi Liu
31809e42f5
Polish unsupported metadata warning with doc link (#58750)
* Add docs link to the warning, add codemod link to viewport docs section
* Only log the warning once after the metadata resolving process, this will avoid multiple duplicated logs showing for one page.

Closes NEXT-1723
2023-11-23 05:42:20 +00:00
Zack Tanner
0f642dc340
fix rewrites to edge routes (#58797)
### What?
Rewrites to an edge route currently throw an invariant rather than properly serving up the page that is rewritten to.

### Why?
The `NextRequest` object that is provided to the edge route handler contains pathname information only for the "origin" request (e.g., when visiting `/one/example` which rewrites to `/two/example`, the pathname is still `/one/example`. This hits an invariant since the route matcher is unable to find `/one/example` since it does not exist.

### How?
This updates the module wrapper to grab the pathname from the route definition rather than the request object. For dynamic segments, we extract them from `request.nextUrl` since we know that even if `nextUrl` is referencing the origin path, the parameters it has are relevant for the rewrite. 

This adds the `getUtils` utility that's also used in base-server to handle the URL normalization, which also provides an interface for normalizing dynamic params from a `ParsedUrlQuery`.

Closes NEXT-1724
Fixes #48295
2023-11-23 05:25:00 +00:00
Zack Tanner
d9531c13c4
Revert "fix: fetch() behavior when "dynamic" is "force-dynamic" (#58735)
Reverts vercel/next.js#58484

The correct way to address this is to use the `fetchCache` segment: https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#fetchcache

There are valid use-cases to want to force a dynamic response but still need fetch caching.

Additionally, this PR updates the docs, since they're incorrect about behavior.

[slack x-ref](https://vercel.slack.com/archives/C042LHPJ1NX/p1691623048477119?thread_ts=1691613915.257239&cid=C042LHPJ1NX)
2023-11-22 18:49:20 +00:00
Leah
bb3d5cf4c3
fix(turbopack): e2e/404-page-router test (#58146) 2023-11-22 19:30:26 +01:00
Jimmy Lai
8baf2e0b79
route handlers: make req.cookies opt you into dynamic (#58769)
This PR fixes an issue where users would try to access `req.cookies` from a route handler and be unable to read from it.

This issue was caused by `req.cookies` not opting you into dynamic behaviour, unlike `cookies()` from `next/headers`. This fixes it.
2023-11-22 14:29:01 +00:00
Zack Tanner
3043feef44
disable static prefetching behavior for dynamic segments (#58609)
### What?
When a layout segment forces dynamic rendering (such as with
`force-dynamic` or `revalidate: 0`), navigating to sub-pages of that
layout will attempt to re-render the layout, also resulting in side
effects re-running. This means if your layout relies on a data fetch and
you render the result of that data in the layout, it will unexpectedly
change when navigating between sub-paths, as described in #57326.

As a separate issue (but caused by the same underlying mechanism), when
using `searchParams` on a dynamic page, changes to those search params
will be erroneously ignored when navigating, as described in #57075

### Why?
As a performance optimization we generate static prefetch files for
dynamic segments ([original
PR](https://github.com/vercel/next.js/pull/54403)). This makes it so
that when prefetching is turned on, the prefetch can be served quickly
from the edge without needing to invoke unnecessarily. We're able to
eagerly serve things that can be safely prefetched. This is nice for
cases where a path has a `loading.js` that we can eagerly render while
waiting for the dynamic data to be loaded.

This causes a problem with layouts that opt into dynamic rendering: when
the page loads and a prefetch is kicked off for the sub-page, it'll load
the static prefetch, which won't be generated with the same router state
as the dynamically rendered page. This causes a mismatch between the two
trees, and when navigating within the same segment, a refetch will be
added to the router because it thinks that it's navigating to a new
layout.

This also causes issues for dynamic pages that use `searchParams`. The
static prefetch will be generated without any knowledge of search
params, and when the prefetch occurs, we still match to the prefetch
generated without search params. This will make the router think that no
change occurs, and the UI will not update to reflect the change.

### How?
There's ongoing work by @acdlite to refactor the client router.
Hopefully it will be easier to re-land this once that work is finished.
For now, I've reverted the behavior as it doesn't seem to be worth the
bugs it currently causes. I've also added tests so that when we do
re-land this behavior, we can catch these subtleties.

Fixes #57326
Fixes #57075

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-11-21 16:55:44 -08:00
mugi-uno
ba3ef55f5c
fix: fetch() behavior when "dynamic" is "force-dynamic (#58484)
### What?

I changed the behavior of fetch() when 'force-dynamic' is specified in the `dynamic` of Route Segment Config to be similar to when 'force-no-store' is specified in `fetchCache`.

### Why?

The document (https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic) contains an explanation that when 'force-dynamic' is specified for `dynamic`, it will behave equivalently to the following:

> Setting the segment config to export const fetchCache = 'force-no-store'

I tried to correct it because it was not actually behaving this way.

### How?

When determining if `fetchCache` is 'force-no-store', I have modified the code to also check the `dynamic` setting.

Fixes #47033


Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
2023-11-17 00:01:31 +00:00
Tim Neutkens
97ba91097d
Support passing a relative string to pushState/replaceState (#58438)
Follow-up to #58335.

Fixes
https://github.com/vercel/next.js/discussions/48110#discussioncomment-7565394

As reported in the discussion passing a origin-relative string didn't
work as `new URL` will throw an error. This ensures the `origin`
parameter is provided to `new URL`.
Added tests for the string behavior.

<!-- 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 #

-->

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Zack Tanner <zacktanner@gmail.com>
2023-11-16 22:11:14 +01:00
Zack Tanner
3cd9264abc
tweak cache + revalidate fetch warning (#58505)
### What?
When using a `Request` object with fetch, we'll log a warning indicating that using the `cache` property in addition to `revalidate` is unsupported.

### Why?
`Request` sets some defaults on the request init, such as `cache: "default"`. This makes the warning confusing and there's no way to avoid it aside from switching the resource argument to be a URL string instead.

### How?
This keeps existing behavior but omits the log in the case where a request object is used and no explicit cache overrides are specified.

Fixes #58109
2023-11-16 15:57:26 +00:00
Jiachi Liu
d6d6d56133
Remove client only dynamic chunks from edge bundle (#56761)
### Issue

In the client components world, when you're using `next/dynamic` with `ssr: false` to split chunks in pages of edge runtime, you could get the dynamic imported module still bundled in the server bundle for edge runtime. This could easily hit the bundle limit on edge runtime if you're loading a large size of non-SSR module.

This is caused by the whole chunk is still being included when we're creating the client entry. Since the client entry is imported eagerily, webpack will bundle all the modules under it, unless it's explicitly marked not being included.

### Fix

For client components, SSR rendering layer of bundle, non-SSR `next/dynamic` calls, we're transform the result of `dynamic()` call from to conditional import the dynamic loaded module.

From
```js
dynamic(() => import(...))
```
To
```js
dynamic(() => {
  require.resolveWeak(...)
}, { ssr: false })
```

This will only be applied to SSR layer bundle client components non-SSR `next/dynamic` calls and only when webpack is bundling since turbopack doesn't need this. In this way, the server side will be stripped but it can still enter the module graph since we need to traverse if there's SA in client modules with using webpack API `require.resolveWeak`. And for client side bundle will still include the actual module.

Close NEXT-1703
2023-11-16 15:10:28 +00:00
Tom Milewski
c26c7713b5
fix: set x-forwarded-host based on request (#58500)
Co-authored-by: @BRKalow <bryce@clerk.dev>

### What?

A number of our customers have been experiencing issues stemming from an
`x-forwarded-host` header that doesn't match the `host` header.

### Why?

[This PR](https://github.com/vercel/next.js/pull/57815) removes
functionality which sets `x-forwarded-host` to `req.headers['host']` and
relies solely on the server's hostname and port.

This can be seen locally when visiting the app via a localhost
subdomain.

The `x-forwarded-host` header will remain as `localhost:${port}` while
the actual requested host will contain the subdomain.

### Related 

- https://github.com/vercel/next.js/pull/57815#issuecomment-1808496790

---------

Co-authored-by: BRKalow <bryce@clerk.dev>
Co-authored-by: Zack Tanner <zacktanner@gmail.com>
2023-11-16 12:00:41 +01:00
Zack Tanner
24b2ff16ab
remove optimistic navigation behavior when prefetch is false (#58413)
### What?
When navigating between pages (via `prefetch: false`) within a dynamic
segment, the shared layout is re-rendered. This can cause unexpected
behavior like layout data changing when navigating between child
segments.

### Why?
When prefetch is false, we're currently opting into an "optimistic
navigation" codepath, which will optimistically render layout-routers up
to the point where data is missing, while kicking off data fetches. It
attempts to determine where refetching needs to happen by traversing the
router cache nodes and checking where data is missing. However, it
locates these cache nodes by using "segments" obtained by
[deconstructing the
URL](https://github.com/vercel/next.js/blob/fix/optimistic-bailout/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts#L142),
which won't accurately contain dynamic segment data. For ex, `/en` which
corresponds with `/app/[lang]/page.tsx` will have a cache node key of
`lang|en|d`, not `en`. Similarly, the optimistic tree that gets
constructed will also be incorrect, since it uses the URL segment.

### How?
My initial fix was to match the dynamic segment against the segment
constructed by the URL. But after discussion with @sebmarkbage and the
team, it seems more correct to remove the optimistic case all together
as there's no guarantee that the url will actually match to that
segment.

Fixes #50670

---------

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-11-16 11:58:17 +01:00
Zack Tanner
8d4f4fcd38
revalidate APIs should make route handlers dynamic (#58466)
### What?
Using `revalidateTag` or `revalidatePath` in a route handler will not
currently opt the handler into dynamic behavior. This means that if you
use these APIs and don't opt into dynamic behavior by some other means,
the revalidation call won't do anything as the route handler will be
served statically.

### Why?
During static generation, we do not currently indicate that usage of
these APIs should opt into dynamic usage.

### How?
This updates `revalidateTag` to throw a `DynamicUsageError` (similar to
our other scenarios, such as search params bailout, headers/cookies, or
fetch + revalidate/no-store)

Closes NEXT-1712
2023-11-16 11:57:55 +01:00
Will Binns-Smith
5f8be1f0d3
Turbopack: Use structured styled text in issue descriptions (#58156)
Requires vercel/turbo#6388

This uses the structure implemented in vercel/turbo#6388 to support formatted text when reporting. Right now only the `Line` and basic `String` cases are handled.
2023-11-15 22:33:14 +00:00
Will Binns-Smith
9ab8828f72
Apply next/font to foreign code (#58461)
This applies next/font to third party code like that in `node_modules`


Closes PACK-1964
2023-11-15 18:52:09 +00:00
François Best
e4158f6cec
fix: Don't reset shallow URL updates on prefetch (#58297)
## Description

Between 14.0.2-canary.6 and 14.0.2-canary.7, a change was introduced in vercel/next.js#56497 that turned the Redux store state into a Promise, rather than a synchronous state update.

This caused the `sync` function -- used to send state updates to the Redux Devtools -- to be recreated on every dispatch, which in turn, by referential instability, caused the `HistoryUpdater` component to re-render and trigger a `history.replaceState` with no particular change, but with the internal `canonicalUrl`.

When an app does a soft/shallow navigation by calling history methods directly (currently the only way to do shallow search params updates in the app router), these changes would have been overwritten by any prefetch (eg: hovering or mounting a Link), which is usually a no-op for the navigation state.

This PR changes the `sync` function to take the state as an argument rather than as a closure. The whole app router state is also unwrapped only once, and fed to the HistoryUpdater. Changes to its contents made by reducers will cause the HistoryUpdater effect to re-run, triggering history updates and a call to the sync function.

## Context

I maintain [`next-usequerystate`](https://github.com/47ng/next-usequerystate), which is used in the Vercel dashboard, and which is impacted by this change (see 
[#388](https://github.com/47ng/next-usequerystate/issues/388)).

## History

@timneutkens introduced the `sync` function and the whole Redux devtools reducer in vercel/next.js#39866, with the note:

> a new hook useReducerWithReduxDevtools has been added, we'll probably want to put this behind a compile-time flag when the new router is marked stable but until then it's useful to have it enabled by default (only 
when you have Redux Devtools installed ofcourse). 

If a different direction is needed to keep sending `RENDER_SYNC` actions to Redux devtools, I'll be happy to rework this PR to move the `sync` function into the action queue.

## Changes

- [x] Added e2e test. Requires a `start` mode as prefetch links are disabled in development. Test was verified to fail from next@>=12.0.2-canary.7 without the fix.


Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
2023-11-14 19:29:18 +00:00
Aki Matsumoto
4fe968bb71
Fix: expected "catch all routes" are not matched in “parallel routes" (#58368)
<!-- 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?



-->
This PR fixes a bug where the expected catch-all route would not match
if there were multiple "catch-all routes" under the "parallel route”.

The page on the non-parallel routes path matches the catch all routes.
that exist on the more detailed path.
However, under parallel routes, it matches the most parent page of all
the pages with catch all routes.

For example, if there are files like below:
```
app/@sidebar/[...catchall]/page.tsx
app/@sidebar/dashboard/[...catchall]/page.tsx
```
When accessing `/foo`, it should match
`app/@sidebar/[...catchall]/page.tsx`, and this is working correctly.
However, when accessing `/dashboard/foo`, it should match
`app/@sidebar/dashboard/[...catchall]/page.tsx`, but
`app/@sidebar/[...catchall]/page.tsx` is being matched instead.

## Repository to reproduce
https://github.com/nonoakij/fix-parallel-routes-with-catch-all

## Related PR
https://github.com/vercel/next.js/pull/58215

---------

Co-authored-by: Jimmy Lai <laijimmy0@gmail.com>
2023-11-13 23:26:45 +01:00
Tim Neutkens
797fecb0c6
Add experimental support for history.pushState and history.replaceState (#58335)
## What?

This PR introduces support for manually calling `history.pushState` and `history.replaceState`.

It's currently under an experimental flag:

```js
/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  experimental: {
    windowHistorySupport: true,
  },
}

module.exports = nextConfig
```

Going forward I'll refer to `history.pushState` as `replaceState` is interchangable.
 
When the flag is enabled you're able to call the web platform `history.pushState` in the usual way:

```js
const data = {
	foo: 'bar'
}
const url = '/my-new-url?search=tim'
window.history.pushState(data, '', url)
```

Let's start by explaining what would happen without the flag:

When a new history entry is pushed outside of the Next.js router any back navigation to that history entry will cause a browser reload as it can no longer be used by Next.js as the required metadata for the router is missing. In practice this makes it so that pushState/replaceState is not feasible to be used. Any pathname / searchParams added can't be observed by `usePathname` / `useSearchParams` either.

With the flag enabled the pushState/replaceState calls are instrumented and is synced into the Next.js router. This way the Next.js router's internal metadata is preserved, making back navigations apply still, and pathname / searchParams is synced as well, making sure that you can observe it using `usePathname` and `useSearchParams`.

## How?

- Added a new experimental flag `windowHistorySupport`
- Instruments `history.pushState` and `history.replaceState`
	- Triggers the same action as popstate (ACTION_RESTORE) to sync the provided url (if provided) into the Next.js router
	- Copies the Next.js values kept in history.state so that they are not lost
	- Calls the original pushState/replaceState

~~Something to figure out is how we handle additional pushes/replaces in Next.js as that should override the history state that was previously set.~~
Went with this after discussing with @sebmarkbage:
- When you open a page it preserves the custom history state
	- This is to solve this case: when you manually `window.history.pushState` / `window.history.replaceState` and then do an mpa navigation (i.e. `<a>` or `window.location.href`) and the navigate backwards the custom history state is preserved
- When you navigate back and forward (popstate) it preserves the custom history state
- When you navigate client-side (i.e. `router.push()` / `<Link>`) the custom history state is not preserved
2023-11-13 13:32:08 +00:00
Jimmy Lai
4024b25e8c
parallel routes: fix catch all route support (#58215)
This PR fixes a bug where parallel routes would not apply appropriately on navigation when used within slots.

The following scenarios:
```
/foo
   /bar
   /@slot
     /[...catchAll]
```

or 

```
/foo
  /[...catchAll]
  /@slot
     /bar
```

will now function correctly when accessing /foo/bar, and Next.js will render both /bar and the catchall slots.

The issue was that the tree constructed by `next-app-loader` for a given path, /foo/bar in the example, would not include the paths for the catch-all files at build time. The routing was done 1-1 when compiling files, where a path would only match one file, with parallel routes, a path could hit a defined path but also a catch all route at the same time in a different slot.

The fix consists of adding another normalisation layer that will look for all catch-all in `appPaths` and iterate over the other paths and add the relevant information when needed.

The tricky part was making sure that we only included the relevant paths to the loader: we don't want to overwrite a slot with a catch all if there's already a more specific subpath in that slot, i.e. if there's /foo/@slot/bar/page.js, no need to inject /foo/@slot/bar/[...catchAll]. 

One thing that is not supported right now is optional catch all routes, will add later.

fixes #48719
fixes #49662
2023-11-09 10:11:06 +00:00
Zack Tanner
bcef6d6ab6
fix ppr test flake (#58232)
Wraps this test in a `check` since the experimental log seems to occasionally come in late ([x-ref](https://github.com/vercel/next.js/actions/runs/6805954994/job/18506429726#step:27:410))
2023-11-09 02:13:21 +00:00
Zack Tanner
db275073d4
pass postpone through staticGenerationStore (#58229)
Instead of requiring React in `maybePostpone` (which is susceptible to referencing the wrong version of React during build time, such as in the case where the static worker patches fetch within an app-route, which doesn't have an experimental runtime), this provides the postpone API to the `staticGenerationStore`. That way we know the API is available in render which is when we'd expect to postpone.
2023-11-09 00:59:05 +00:00
Sebastian Markbåge
2f68e62d30
Reword PPR caught bail out to avoid "postpone" terminology (#58223)
The "postpone" terminology is internal to React and can be used for more
things than just this. It's also a mechanism we may or may not rely on.

---------

Co-authored-by: Zack Tanner <zacktanner@gmail.com>
2023-11-08 17:08:24 -05:00
Sebastian Markbåge
d68bbd7af7
Add helpful context to postpone reason if it's caught and logged or escapes (#58222)
Other ways such as on the client.
2023-11-08 16:16:40 -05:00
Jimmy Lai
536d2dbc44
interception routes: fix interception for dynamic routes (#58198)
This PR fixes the bug in which interception routes of the form `(.)[param]` would not intercept navigations.

The bug happened because we would not create a dynamic route matcher for the intercepted route, so we would never match the correct dynamic route when hitting the server, falling back to the base one. 

The fix consists of fixing the logic that checks for a dynamic route so that it checks the correct path when handling an interception route.

There's probably a better fix here, advice welcome

fixes #52533
2023-11-08 18:57:57 +00:00
Zack Tanner
fe0d947a66
ensure experimental warning shows up when using PPR (#58206)
Fixes a bug where when `config.experimental.ppr` was set, we didn't show
the proper warning indicator that the feature is experimental.
2023-11-08 17:20:02 +01:00
Shu Ding
24a617c24f
Change allowed forwarded hosts to be allowed origins for Server Actions (#58023)
The allowlist should be origin domains that are allowed to send the
requests, not the list of forwarded hosts (i.e. reverse proxies).
2023-11-08 11:20:32 +01:00
Zack Tanner
9aefe525bf
fix PPR build output logs (#58149)
Fixes two build output logs:

- A page that uses `generateStaticParams` & postpones should be marked as partially prerendered
- Assume pages that contain dynamic segments will postpone when PPR is enabled even though it won't be determined until request time
2023-11-07 22:09:55 +00:00
Zack Tanner
6e1eda72e1
ppr: fix fetch postpone not having correct React version (#58107)
When `patchFetch` is applied at during `buildAppStaticPaths` ([ref](030021234b/packages/next/src/build/utils.ts (L1253-L1256))), it's done so without any context of the bundled React runtime.

Since `patchFetch` will potentially postpone during static generation with PPR, it needs to be using the correct React version, otherwise `unstable_postpone` won't be available e.g. in the case of a page utilizing `generateStaticParams`.

This moves `patchFetch` into the bundled runtime so that it has the correct version of React when invoked.
2023-11-06 23:51:39 +00:00
Tim Neutkens
bafa8d4948
Remove installCommand on streaming-ssr test (#58037)
Removes installCommand as it doesn't have to be different than the
default command. Also converted to using `createNextDescribe`.

<!-- 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-11-05 13:30:12 +01:00
Tim Neutkens
520fbc884d
Remove throw for unknown messages in hot-reloader-client (#57353)
Ensures extra messages are ignored instead of throwing an error.
2023-11-03 01:06:53 +00:00
Jiachi Liu
9af75d4b2d
Fix client chunk loading encoding for dynamic route (#57960)
We had added encoding the client component assets loaded from RSC manifest that we need to encode them to make sure when they're loaded on server and sent to client, the client will receive the encoded one. But the override of the webpack chunk loading method could be loaded later than react related chunks, that when client component is loaded first (e.g. `next/script`) and it triggers react loaded ealier than the overriding. Then the chunk could be encoded incorrectly.

Discussed with @gnoff and put this out as the 1st step solution to ensure the order. in the future we can try to get rid of the encoding by providing safer url

Fixes #57829
2023-11-02 23:09:25 +00:00
Zack Tanner
5da8ad7baf
move static worker IPC server behind experimental flag (#57943)
A shared incremental cache IPC server was introduced for build-time static workers as an optimization to dedupe fetch requests, however this can cause fetch-related flakiness to builds under certain conditions (e.g., large payloads).

This moves the optimization behind an experimental flag to unblock those running into IPC-related build time errors while we work on an alternative solution for the underlying issue

Fixes #53695
2023-11-02 20:26:55 +00:00
Zack Tanner
85abc48c90
suspend in render, not in reducers (#56497)
This removes our current convention of throwing promises in reducers in
favor of returning promises that can be consumed by `use` instead. This
will help unblock some future improvements (batching, PPR)

Reducers that would typically throw a promise now return their promise.
This gets maintained by a mutable queue (initialized in hydrate) to
ensure actions are processed in-order. The queue is also responsible for
mutating state and passing it as an input to subsequent actions.

This PR does not modify reducer behavior to keep changes minimal, but
there's more cleanup that we can do in a follow-up PR to remove things
that previously assumed reducers would be replayed.

(I recommend reviewing with whitespace turned off)

---------

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2023-11-02 11:53:50 +01:00
Zack Tanner
7b524fa2d3
ppr: fail static generation if postponed & missing postpone data (#57786)
When postpone is caught by user code, this will cause PPR not to properly prerender the static parts and thus we need to fail the build. This also adds some messaging about how to fix the error.

Prior to this change, catching code that would normally trigger `postpone ` would silently fail, but the build outputs would be incorrect as there's no postpone data available. 

Relands #57477 with additional tests & fixes
2023-11-01 20:28:13 +00:00
Jiachi Liu
6b6bfcd387
Polish logging segments of page route (#57834)
Reduce the confusiong of the the logging of pages, make it easier to understand

* Removing the trailing `/page`, `/route` suffix 
* Removing the internal segment like `[[...__metadata__]]` 



#### After

```
 ○ Compiling / ...
 ○ Compiling /dynamic/[slug]/icon ...
```
#### Before
```
 ○ Compiling /page ...
 ○ Compiling /(group)/dynamic/[slug]/icon-ahg52g/[[...__metadata_id__]]/route ...
```

Closes NEXT-1701
2023-11-01 16:34:52 +00:00
Shu Ding
937254edf5
Add serverActions.allowedForwardedHosts option (#57529)
This new option specifies a list of host names that are considered safe, to accept as Server Action requests if they're different from the initial request origin. It can be very helpful when the hosted app has many layers of reverse proxies ahead.

Closes #57397.
2023-11-01 12:20:00 +00:00
Balázs Orbán
1caa58087a
fix: correctly set x-forwarded-* in Middleware (#57815)
### What?

Follow-up of #56797

While working on this, I noticed that some logic around stripping internal headers was duplicated, so I did some cleanup too.

### Why?

In #56797 we set these headers, but it only affected Route Handlers, Middleware is still missing them, which is a regression introduced in #52492

(Related: https://github.com/vercel/next-learn/issues/252)

### How?

Move to set these headers up to `base-server.ts` so they are present in Middleware too.

> Note: All headers are set with `??=` to respect the original value if set (with other words, only add these headers if they aren't set yet)

Closes NEXT-
Fixes #52266
2023-11-01 04:36:50 +00:00
Jiachi Liu
cd821c8e03
Fix nested esm package default import resolving mismatch (#57784)
For app router bundling layers "SSR rendering" and "browser" layer, which are used for server side rendering and client, we should still apply the module resolving rules to all assets since we bundled everything, removed the default exclude conditions as they'll not apply the rules to node_modules. That could cause the mismatch resolving for package.

E.g. You have two dual packages A and B are both compatible for ESM and CJS, and both have default export. B is depent on A, but when you import B in a client component that will be SSR'd, it's picking up the CJS asset like the case described in #57584 .

Fixes #57584 
Closes NEXT-1702
2023-10-31 00:31:23 +00:00
OJ Kwon
cce9f0d34f
fix(metadata): align metadata suffix hash between turbopack (#57544)
### What?

Wraps up metadata-dynamic-routes tests fixes for the turbopack. There is 1 remaining failing test due to lacks of supporting `import.meta.url` which need to be addressed separately.

I spent amount of time why turbopack cannot find the route for the dynamic metadata for a certain route. In the end, found there are mismatching expectations for the route due to different hash for the certain route. We do use the same djb2 hash between next.js and turbopack both, so it was quite confusing why we don't get deterministic hash.

After trying some experiments, found out root cause was how 2 different runtimes handle overflow for given type of numbers. In rust + turbpack we use u32 and do 32-bit hash calculation for given string, while in js we implicitly used number type as is, in result overflow occurs with default 53-bit float. 

Originally I tried to adjust hash in turbopack side to preserve js hash as-is, but so far I found it was non trivial to do so as rust there's no out of the box types we can coerce to the js number type. In result, unlike other fixes in turbopack this PR changes how js hash is being generated. I hope this woulndn't be a breaking changes; expect so since this is a metadata specific hash that we do not have written spec for it.

Closes WEB-1890
2023-10-30 19:56:55 +00:00
Zack Tanner
9128b58654
fix gsp tracing issue (#57766)
This removes the ignores for dev react bundles which was added as an
optimization but causes issues when react is imported from an ESM module
since all requires are being analyzed for named exports.

Fixes #57582
2023-10-30 18:24:51 +01:00
Jiachi Liu
3553c6516d
Improve error for missing default export in dynamic metadata routes (#57711)
Displaying hints of "missing default export" if you didn't properly export the `default` handler for og image 

```
   ▲ Next.js 14.0.1-canary.2
   - Local:        http://localhost:3000

 ✓ Ready in 1089ms
 ✓ Compiled /opengraph-image/[[...__metadata_id__]]/route in 211ms (44 modules)
 ⨯ Error: Default export is missing in "/Users/huozhi/workspace/next.js/test/e2e/app-dir/metadata-dynam
ic-routes/app/opengraph-image.tsx"
    at eval (webpack:///app/opengraph-image.tsx?3407:11:1)
    at (app-metadata-route)/../../../../packages/next/dist/build/webpack/loaders/next-metadata-route-lo
ader.js?page=%2Fopengraph-image%2F%5B%5B...__metadata_id__%5D%5D%2Froute&isDynamic=1!./app/opengraph-im
age.tsx?__next_metadata_route__ (/Users/huozhi/workspace/next.js/test/e2e/app-dir/metadata-dynamic-rout
es/.next/server/app/opengraph-image/[[...__metadata_id__]]/route.js:362:1)
    at __webpack_require__ (/Users/huozhi/workspace/next.js/test/e2e/app-dir/metadata-dynamic-routes/.n
ext/server/webpack-runtime.js:33:42)
```
2023-10-30 16:36:24 +00:00
Dima Voytenko
08fdc1ebcf
OpenTelemetry: propagate a configured context(s) to root requests (#57084)
Implement OTEL propagation according to the OpenTelemetry API per
https://opentelemetry.io/docs/specs/otel/context/api-propagators/. Any
configured propagator should work with this API, including
`W3CTraceContextPropagator` (https://www.w3.org/TR/trace-context/).

Alternative to the https://github.com/vercel/next.js/pull/56891.

/cc @sfirrin
2023-10-27 14:10:15 -07:00
Jimmy Lai
cfbffb2ff4
Revert "Apply react-server transform and valication to middleware (#5… (#57504)
…7448)"

This reverts commit 17bd232cf2.

<!-- 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-10-26 05:29:49 -07:00
Jimmy Lai
3e7f96e16a
revert "Apply react-server condition for pages api (#57459)" (#57500)
This reverts commit 6b18f397cb.

<!-- 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-10-26 04:37:13 -07:00
Zack Tanner
4666e8545d
revert ppr logging changes (#57486)
need to investigate this better

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-10-26 00:28:24 -07:00
Jiachi Liu
07483d4b4c
Move serverActionsBodySizeLimit to serverActions.bodySizeLimit (#57433)
Scope all `serverActions` config in one group "serverActions" to make it
more semantics

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-10-26 08:51:32 +02:00
Zack Tanner
2c21635cf6
if there are errors during postpone, or postpone was caught, fail static generation (#57477)
Rather than sending a warning, we should fail the build if a postpone
call is caught. We should also fail the build if there are any errors
during static generation similar to the non-ppr case.

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-10-25 23:43:33 -07:00
Justin Ridgewell
df0fb70c8c
turbopack: Support Actions in both RSC and Client layers (#57475)
### What?

Adds support for Server Actions imported by both server and client.

### Why?

If an Action is imported by both the Client and RSC layers, we need to
support them as if they're the same action.

### How?

First, we need to ensure both layers create the same action hash ids,
which we can then use to deduplicate actions imported by both layers. If
a conflict is found, we prefer the RSC layer's action.

Closes WEB-1879

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-10-26 08:32:04 +02:00
Leah
a19b1ef796
fix(turbopack): make layout in group not cause a 404 (#57471)
### What?

Given the structure:
`/page.js`
`/(group)/layout.js`

Going to `/` would 404

Closes WEB-1878
2023-10-26 05:16:28 +00:00
Zack Tanner
0052c59022
add better messaging around wrapping postpone with try/catch (#57446)
when calls to `maybePostpone` throw but there's no postpone state, we want to handle those errors differently so that we can provide clearer messaging around how to prevent them, while still retaining any errors that were re-thrown by the user.

ex:
![CleanShot 2023-10-25 at 16 05 56@2x](https://github.com/vercel/next.js/assets/1939140/d86cce9f-f9ed-477d-8d1c-0ce7c934d073)
2023-10-26 02:50:58 +00:00
Jiachi Liu
6b18f397cb
Apply react-server condition for pages api (#57459)
Apply react-server condition and related API checks for pages API.

if you're doing react SSR with renderToString in middleware it should be disallowed. Imaging it could send the rendered html code to client and you display it in browser. But it might require hydration so it can be broken.

Follow up for #57448 , same reason explained in #57448
Closes NEXT-1653
2023-10-26 02:07:27 +00:00
Jiachi Liu
17bd232cf2
Apply react-server transform and valication to middleware (#57448)
Apply `react-server` resolving and server components invalid APIs
checking to middleware. We want to limit the react usage in in
middleware as so far it's not aimed for rendering purpose.

Another invalid case could be: if you're doing react SSR with
`renderToString` in middleware it should be disallowed. Imaging it could
send the rendered html code to client and you display it in browser. But
it might require hydration so it can be broken.

This PR will only let you import `react-server` export condition
packages.
2023-10-26 01:22:50 +02:00
Jiachi Liu
a301eb6a9f
Viewport exports (#57302)
## Story Time

Metadata API introduced two exports `metadata` and `generateMetadata` to the pages and layouts under app router, as partial prerendering work is going on and people are desiring to render the metadata asynchronizly, this change will be the preparation for moving to the dynamic & asynchronized land. In short, if we can render the metadata asynchronizedly, it will benefit the performance of the initial page loading and client page transiation a lot. Any slow data fetching can be handled while the essential page "shell" is rendered. 

For meta tags, there're few ones will visually affect your web page, such as `<meta name="viewport">`, `<meta name="theme color">` and `<meta name="color-scheme">`, rendering them lately after the page frame is ready might bring flickering to the page such as revreting whole page's theme color or shaking due to viewport updates. Those meta are not majorly the "metadata" for SEO, but more for user experience when opening the page. If we're rendering everything as async meta tags, it won't be ideal due to the flickering on your web pages.

## Solution Preparation 

We'll want to render the meta tags separately to make sure the visual ones are rendered as blocking along with web page, and then the ones for SEO or bots can be flushed later by later, like a suspense boundary keeps emitting them into the head of html. 

We optionally picked up 3 meta tag "viewport", "theme-color" and "color-scheme" to be render first into the web page with html "shell", to guarantee the layout viewport and basic styling are rendered first.

This PR introduced two module export in the page and layout files: `viewport` and `generateViewport`, in order to separate the visual meta tags from the SEO metadata.

### API

```ts
// page.js | layout.js
export const viewport = {
  // viewport meta tag
  width: 'device-width',
  initialScale: 1,
  maximumScale: 1,
  interactiveWidget: 'resizes-visual',
  // visual meta tags
  colorScheme: 'dark',
  themeColor: { color: 'cyan', media: '(prefers-color-scheme: dark)' },
}
```

There's also a dynamic API like what we did for metadata API

```ts
// page.js | layout.js
export function generateViewport() {
   return { ... }
}
```

## Notice 

This PR won't get SEO metadata rendered asyncronizedly, instead it's a preparation for the later work in partial prerendering and async metadata. We'll encourage the Next.js community moving to the new metadata viewport API if you're customzing those 3 meta tags. Usually you don't have to change viewport itself, so mostly like only theme-color and color-scheme could relate to it.
2023-10-25 05:20:23 +00:00
Shu Ding
59ebfbea9e
Fix request body hanging when middleware is preset (#57381)
Instead of `Readable.toWeb` we're gonna manually convert the Node.js stream to a Web stream. `toWeb` is either having a bug, or not compatible with middleware-cloned `PassThrough` streams.

Closes #56286. The case should be already covered with existing tests.
2023-10-25 02:44:10 +00:00
OJ Kwon
8433970213
test(next-image): adjust assertion compatible to turbopack (#57350)
### What

Updating test assertions to work with turbopack as well.


Closes WEB-1843
2023-10-24 22:25:57 +00:00
Zack Tanner
d6a933edbf
disable opentelemetry test in turbopack (#57361)
`experimental.instrumentationHook` is not currently supported by
Turbopack, so this skips the tests.
2023-10-24 14:15:57 -07:00
JJ Kasper
ae10b5c82b
Fix app ISR error handling (#57332)
This ensures when an error occurs during a revalidate in app router that
properly throw the error fully and don't store the error page in the
cache which matches the expected behavior for full route ISR as errors
are not meant to update the cache so that the last successful cache
entry can continue being served.

Fix was tested against the provided reproduction here
https://app-dir-example-ghl01cxtn-basement.vercel.app/

Fixes: https://github.com/vercel/next.js/issues/53195
2023-10-24 09:38:30 -07:00
Wyatt Johnson
741a08bc25
fix: ensure generateStaticParams isn't required for PPR (#57333) 2023-10-24 14:18:11 +00:00
JJ Kasper
8b01e2578e
Fix nested unstable_cache revalidating (#57316)
This ensures when we call `revalidateTag` and there are nested `unstable_cache` entries we properly revalidate.
2023-10-24 05:01:00 +00:00
JJ Kasper
ec4e32c764
Fix no-store/revalidate 0 inside of unstable_cache (#57313)
This ensures we don't unexpectedly error when a fetch attempts to cache
inside of `unstable_cache`, this also ensures `only-on-store` doesn't
unexpectedly error when `revalidate: 0` is set.
2023-10-23 20:52:56 -07:00
Wyatt Johnson
69388a52fe
Partial Prerendering (#57287)
This PR introduces a build optimization to create a "partial prerender" of the page.

1. During compilation, we create a static shell for the page using your existing Suspense boundaries. Components that can be static will be included in this static shell, leaving holes for the dynamic components.
1. Using `<Suspense />`, we can define fallbacks to be included in the partial prerender, as well as the holes for the dynamic components to stream into.

This means Next.js can initially serve a static loading skeleton, kicking off the dynamic parts in parallel. Then, the dynamic components stream in on demand. Dynamic components can use `cookies()`, `headers()`, `'cache': 'no-store'`, or `unstable_noStore()` to opt-into dynamic rendering.

Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
2023-10-24 01:13:05 +00:00
Shu Ding
78ffea42ee
Fix client reference proxies (#57301)
Each individual exported component should be a proxied reference. Not sure why it's been broken for so long and it didn't trigger any error.
2023-10-24 00:24:22 +00:00
JJ Kasper
7a1b01b85d
Skip i18n-ignore-rewrite-basepath-source irrelevant tests with turbopack (#57295)
Same as https://github.com/vercel/next.js/pull/57289 for another test
suite.
2023-10-23 15:01:24 -07:00
JJ Kasper
dc4138db0a
Update unrelated mdx tests with turbopack (#57294)
The non-mdxrs version of the plugin isn't expected to work with
turbopack so this skips it in that mode
2023-10-23 14:48:39 -07:00
JJ Kasper
1275cffe78
Skip i18n-ignore-rewrite-source irrelevant tests with turbopack (#57289)
These tests are expecting files to be written but this isn't the case
with turbopack for client files.
2023-10-23 14:13:49 -07:00
Jiachi Liu
cc4cceb4f0
Polish compiling and turbopack logging (#57270)
## Make server info log faster

Change to display the server info earlier before server handler is ready

## Make "compiling" message less laggy.

Change to log the compiling logs as previous timer which trying to skip the logging short-time compiling message makes it bit laggy, so we're removing it for now. Reduce the timer from 3s to 500ms

<img width="360" alt="image" src="https://github.com/vercel/next.js/assets/4800338/19afb2d6-ec42-4a67-b664-60f8c36b5287">


## Remove the turbopack

### Now
<img width="398" alt="image" src="https://github.com/vercel/next.js/assets/4800338/fb24d83f-66d0-4383-a42b-fdeff4fe223a">

### Before

<img width="398" alt="image" src="https://github.com/vercel/next.js/assets/4800338/59d6a6f2-32d6-41d1-a80d-12c599165e6a">
2023-10-23 19:28:36 +00:00
Jiachi Liu
4e1429182f
Move logging option to stable (#56791)
We introduced a data fetching logging before, and the control option was under experimental. After a bit experiments turns out users really loves it. We decide to move it to a stable option.

### Changes
We're going to move the `logging` option outside of `experimental`, and scope the `fetches` related config under `logging.fetches`.

```js
// next.config.js
logging?: {
    fetches?: {
      fullUrl?: boolean
    }
}
```
2023-10-23 18:45:00 +00:00
Shu Ding
eb4b41e513
Fix unmatched WebSocket upgrade requests being closed (#57245)
We should not close unmatched sockets as it might be handled by user's server.

Fixes #56996
Fixes #49334
Fixes #55299
2023-10-23 17:23:34 +00:00
OJ Kwon
3c3744631e
fix(loader_tree): propagate metadata to corresponding layout (#56956)
### What?

- fixes test 17553c5e25/test/e2e/app-dir/metadata/metadata.test.ts (L487)

The way next.js collects static metadata is read static metadata, and then read layout metadata to merge multiple metadatas into a single layout path (17553c5e25/packages/next/src/lib/metadata/resolve-metadata.ts (L347-L352))

When turbopack creates LoaderTree for the corresponding directory tree, it extracts `page` but skips metadata  in result there are orphan components that have a metadata doesn't have layout metadata, as well as a component have a layout doesn't have metadata. Latter is being rendered as a page (since it have correct layout), which eventually falls back to the default metadata instead.

PR trickles down the metadata when extracting page (creating a new component with `page`) to consolidates those.

Also PR expands Metadata to have base_page property to capture where it has been originally exists, as we clone down metadata then do `fillMetadataSegment` against the current page where LoaderTree is being created it creates a wrong relative path. For example, currently

```
/icon.svg
 - opengragph/
      - static -> path being `/opengraph/.../icon.svg` instead of `/icon.svg`
```

When recursively traverse directory tree, capture each components with corresponding base_page to calculate instead.

Unfortunately this doesn't make pass all of the metadata tests; there are lot to dig more. Would like to scope PR in a reasonable size.


Closes WEB-1795
2023-10-21 21:24:40 +00:00
Tim Neutkens
8502d0aab0
Skip webpack test in Turbopack test run (#57195)
This test checks webpack loaders so can be safely disabled for Turbopack.
2023-10-21 20:58:11 +00:00
Jiachi Liu
c43c915961
Polish auto create layout warning color (#57190)
### After
<img width="643" alt="image" src="https://github.com/vercel/next.js/assets/4800338/254f2ecf-1a22-4756-baf8-d497fa9485ee">

### Before
![image](https://github.com/vercel/next.js/assets/4800338/8acf8c6e-690f-4827-a8fb-bd729e276f0f)
2023-10-21 19:21:22 +00:00
Shu Ding
a383b93556
Encoding and decoding variables of Server Actions from the closure (#56377)
This PR implements encryption and decryption for Server Action bound values that are from the closure level. Explicit `.bind` values, function arguments and module-level values are NOT handled.

### Compiler

The compiler now groups all closure bound values to an array which gets wrapped with `encrypt`. And then inside the action body, it prepends an expression to recreate the values via `await decrypt`.

Since closure-closed variables will only exist on the server layer, the encryption utility has `"server-only"` annotated.

### Encryption

During build time, a private AES-GCM encryption key is randomly generated and stored in the built server manifest. Before encrypting/decrypting, an extra round of Flight server and client will be used to serialize/deserialize the value.

When encrypting, a salt that contains the action ID is provided to prevent replay attack towards different API endpoints. The encryption key can be overridden via the `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` env variable so it can be built on multiple machines on scale.

A global singleton for storing the client reference manifest was made for Flight's serialization/deserialization as that might happen outside of rendering.

After encryption, we then serialize the ArrayBuffer as Base64 to send it to the client.
2023-10-21 17:52:06 +00:00
Josh Story
5528cc6d4e
Remove the experimental serverActions flag (#57145)
Remove the experimental `serverActions` flag

Co-authored-by: Shu Ding <3676859+shuding@users.noreply.github.com>
Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-10-20 20:45:25 +00:00
Balázs Orbán
2277797dc2
chore: drop form-data (#57141)
### What?

Dropping `form-data` polyfill as Node.js 18+ versions all expose it on `globalThis`

### Why?

Reduce the amount of shipped code in Next.js

### How?

Similar to #56899

See: https://nodejs.org/docs/latest-v18.x/api/globals.html#class-formdata
2023-10-20 19:25:42 +00:00
Sebastian Markbåge
c41debd23a
Expose Experimental Taint APIs (#56507)
Exposes the new experimental Taint APIs using the `taint` flag which
enables experimental React.

As an example for how we can use it, I use it to taint `process.env`
with a better error message. I'm not sure where this should live since
it's a global init but it needs access to the global config. It's
unnecessary to retaint it for every render but not sure if there's a
better place for it.

---------

Co-authored-by: Jimmy Lai <laijimmy0@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-10-19 19:02:37 -07:00
Jimmy Lai
6d2df8636a
build: don't error when bundling optional peer deps (#57073)
This PR adds a resolver plugin to verify during bundling that when a module is unresolved, that it is not an optional peer dependency specified in the package.json of the caller. An error would happen if you try to bundle packages like `typeorm` since there are `require` calls in the code to those dependencies.

Also, swallow dynamic dependencies warnings in `require` calls error if they come from `node_modules`. They are not actionable at all generally.
2023-10-20 00:57:06 +00:00
Steven
a3aa6590ff
chore(next/image)!: mark domains as deprecated in favor remotePatterns (#57062)
We already had `domains` as "not recommended" but this PR marks it as "deprecated" and prints a warning if its detected.

I also updated all examples to switch from `domains` to `remotePatterns`.
2023-10-19 20:24:48 +00:00
Balázs Orbán
33db463fe4
chore: upgrade jest (#56909)
### What?

Upgrade jest to its latest version

### Why?

#56899 fails because historically Jest stripped the globals in Node.js, but 28+ isn't doing that anymore. If we upgrade, we don't have to keep track of Node.js globals and when they are added. This will be useful in removing even more polyfills for things that are natively shipped in Node.js now.

### How?

Jest 29 introduced a change to the snapshot format: https://jestjs.io/blog/2022/08/25/jest-29

First, I tried setting the old compat option to not require updating snapshots, but some tests were still failing: https://dev.azure.com/nextjs/next.js/_build/results?buildId=70633&view=logs&j=8af7cf9c-43a1-584d-6f5c-57bad8880974&t=7ae70e63-3625-50f4-6764-5b3e72b4bd7a&l=273 So going through the pain now instead.
2023-10-19 17:38:24 +00:00
Jiachi Liu
a6d4c77f41
Prefer module over main on main fields for app router server compiler (#56960)
Reverts vercel/next.js#56766
Reland vercel/next.js#56532 

x-ref: https://vercel.slack.com/archives/C04DUD7EB1B/p1697576563771229?thread_ts=1697146531.305779&cid=C04DUD7EB1B

This change was breaking package which was actually a dual package but marked as esm module, and when the package is ESM but import a CJS dependency without fully spepcified file path it will fail to resolve. The way is to either remove the esmodule type in package.json or change the import to full path import with extension for the package
2023-10-19 16:06:47 +00:00
Jiachi Liu
a3e56c9c1e
Move ImageResponse to next/og (#56662)
### Story

Since we introduced `ImageResponse` into `next/server` export, there're a few libraries relying on `next/server` that accidentally ended up with bundling og image into the bundle. As og package is quite large that could easily raise the size concern for middleware, edge runtime routes.

### Struggles
 We've done optimizations. The tree-shaking strategies are tricky, we tried modularize imports and also optimize cjs require/exports to make sure you're not including og package into bundle if it's not being used. However, it's still not 100% can handle all the bundle optimization cases, such as `import {..} from "next/server.js"` could also ended up with the cjs bundle that failed the tree-shaking.

### Move on
So we decide to move og `ImageResponse` into a separate export `next/og`

Closes NEXT-1660
2023-10-19 14:26:48 +00:00
Quentin
abe8b1e0a8
Improve performance of String.prototype.split uses (#56746)
This PR adds the optional `limit` parameter on String.prototype.split uses.

> If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#syntax)

While the performance gain may not be significant for small texts, it can be huge for large ones.

I made a benchmark on the following repository : https://github.com/Yovach/benchmark-nodejs

On my machine, I get the following results:
`node index.js`
> normal 1: 570.092ms
> normal 50: 2.284s
> normal 100: 3.543s

`node index-optimized.js`
> optmized 1: 644.301ms
> optmized 50: 929.39ms
> optmized 100: 1.020s

The "benchmarks" numbers are : 
- "lorem-1" file contains 1 paragraph of "lorem ipsum"
- "lorem-50" file contains 50 paragraphes of "lorem ipsum"
- "lorem-100" file contains 100 paragraphes of "lorem ipsum"
2023-10-19 00:25:15 +00:00
Wyatt Johnson
07c434d54b
Web Streams cleanup (#56819)
This updates some code related to web streams and encoding.

- Removes some unused code related to base64 encoding/decoding (Edge runtime currently supports it natively via `Buffer`)
- Prefer readable stream `pull` versus `.on("data", (chunk) => { ... })` event handlers (simplifies execution)
- Utilize `pipeTo` and `pipeThrough` on web streams to remove custom code related to stream pumping
- Updates pipe readable function to utilize web streams first class rather than relying on manual pumping + stream management
  - This also takes advantage of the `AbortController` when piping so that the response can use it to cancel the stream
2023-10-18 21:38:28 +00:00
Janicklas Ralph
5e474a3f19
Adding useGoogleTagManager hook to @next/third-parties (#56106)
This PR adds the `useGoogleTagManage` hook to `@next/third-parties` repo.
2023-10-17 18:04:53 +00:00
Jimmy Lai
451a54cb2e
cache: add unstable_noStore API (#56930)
This PR introduces a new API, `unstable_noStore`, which will allow users to declaratively opt out of caching anywhere during static generation in the same way that you can specify `cache: 'no-store'` on a fetch call in Next.js.

An important caveat and difference from just calling `cookies()` to opt-out of static generation is that this won't opt you out when called from within `unstable_cache` and instead defers to the cache configuration to it.

```
import {unstable_noStore as noStore} from 'next/cache';

export default async function Component() {
  noStore();
  const result = await db.query(...);
}
```
2023-10-17 14:52:46 +00:00
dpnolte
218d0709eb
feat: set status code to 500 if unexpected error occurs before streaming in app router (#56236)
<!-- 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 #

-->

This PR therefore introduces to always set response status code to 500
unless it is a `NotFoundError` or `RedirectError`. This PR would fix
issue #56235. See also:
https://codesandbox.io/p/sandbox/nice-panini-2z3mcp .

**Current Behavior**
At the moment, when an unexpected error occurs during app server
rendering, a 200 ok is returned as status code. This seems to be
undesirable because of the success status CDNs will cache the error
pages and crawlers will index the page considering the error content as
the actual content.

**Desired Behavior**
This issue is related to discussion
https://github.com/vercel/next.js/discussions/53225. Even though I
understand that the response status code cannot be set if streaming has
started, in my view it would be best to set the response status to 500
whenever it can (so before the streaming has started) for SEO and (CDN)
http caching. This would also be consistent with how 404s currently
work; that is, response status code is set to 404 if `NotFoundError`
occurred before streaming (related
[issue](https://github.com/vercel/next.js/issues/43831) &
[PR](https://github.com/vercel/next.js/pull/55542)).

Ideally, when a runtime error happens after streaming, a `<meta
name="robots" content="noindex" />` would also be added. But I didn't
want to make the PR too complex before receiving feedback.

---------

Co-authored-by: Vũ Văn Dũng <me@joulev.dev>
Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2023-10-17 15:32:20 +02:00
Josh Story
0a80017d03
Update React from d900fadbf to 09fbee89d. Removes server context and experimental prefix for server action APIs (#56809)
The latest React canary builds have a few changes that need to be
adopted for compatability.

1. the `useFormState` and `useFormStatus` hooks in `react-dom` and the
`formData` opiont in `react-dom/server` are no longer prefixed with
`experimental_`
2. server content (an undocumented React feature) has been removed. Next
only had trivial intenral use of this API and did not expose a coherent
feature to Next users (no ability to seed context on refetches). It is
still possible that some users used the React server context APIs which
is why this should go into Next 14.

### React upstream changes

- https://github.com/facebook/react/pull/27513
- https://github.com/facebook/react/pull/27514
- https://github.com/facebook/react/pull/27511
- https://github.com/facebook/react/pull/27508
- https://github.com/facebook/react/pull/27502
- https://github.com/facebook/react/pull/27474
- https://github.com/facebook/react/pull/26789
- https://github.com/facebook/react/pull/27500
- https://github.com/facebook/react/pull/27488
- https://github.com/facebook/react/pull/27458
- https://github.com/facebook/react/pull/27471
- https://github.com/facebook/react/pull/27470
- https://github.com/facebook/react/pull/27464
- https://github.com/facebook/react/pull/27456
- https://github.com/facebook/react/pull/27462
- https://github.com/facebook/react/pull/27461
- https://github.com/facebook/react/pull/27460
- https://github.com/facebook/react/pull/27459
- https://github.com/facebook/react/pull/27454
- https://github.com/facebook/react/pull/27457
- https://github.com/facebook/react/pull/27453
- https://github.com/facebook/react/pull/27401
- https://github.com/facebook/react/pull/27443
- https://github.com/facebook/react/pull/27445
- https://github.com/facebook/react/pull/27364
- https://github.com/facebook/react/pull/27440
- https://github.com/facebook/react/pull/27436

---------

Co-authored-by: Zack Tanner <zacktanner@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-10-16 15:46:10 -07:00
Balázs Orbán
1ff7f07875
feat: drop Node.js 16 (#56896)
### What?

BREAKING CHANGE: Bump the minimum required Node.js version.

### Why?

Node.js 16 has reached end-of-life in September.

Bumped to `18.18.2` since it contained some security-related patches: https://nodejs.org/en/blog/vulnerability/october-2023-security-releases

### How?

Bumped `engines` where needed, upgraded the workflows.

This will allow us to remove quite a few polyfills, I'll open separate PRs for those.
2023-10-16 21:41:38 +00:00
Steven
d32121b736
chore(test): test remote image from proxy (#56895)
This PR adds a test to ensure we don't regress again when using proxies
in front of Next.js with `connection: 'upgrade'`.

- Regression introduced in https://github.com/vercel/next.js/pull/56226
- Issue reported in https://github.com/vercel/next.js/issues/56038
- Rolled back in https://github.com/vercel/next.js/pull/56836
2023-10-16 14:48:29 -04:00
Zack Tanner
3e77d69ca0
improve next-image-proxy test (#56893)
This fixes a warning where the test server was exiting before all the
proxy requests finished. This also throws an error to fail the test if
any of the proxied requests aren't fulfilled.

[slack
x-ref](https://vercel.slack.com/archives/C04DUD7EB1B/p1697465502593889?thread_ts=1697143786.511779&cid=C04DUD7EB1B)
2023-10-16 08:36:34 -07:00
Tim Neutkens
82e1057a7f
Fix CSP test when using Turbopack (#56833)
Since Turbopack doesn't use eval-source-map the CSP nonce will pass correctly, nice improvement over the current state where you can't check CSP in dev.
2023-10-14 20:07:29 +00:00
Tim Neutkens
c6fe20a31f
Remove specific hash checks for metadata.test.ts (#56843)
This ensures the tests pass in both webpack and Turbopack.

<!-- 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-10-14 19:54:55 +02:00
Tim Neutkens
83d8867470
Add missing nanoid dependency to app-dir tests (#56830)
Fixes a bunch of the Turbopack test failures for `test/e2e/app-dir/app/index.test.ts`. Not sure how this passed with webpack before as the dep was indeed missing.
2023-10-14 11:32:10 +00:00
Balázs Orbán
fe0bfbf911
fix: add x-forwarded-* headers (#56797)
### What?

Adding back `x-forwarded-*` headers.


### Why?

Starting with #52492, these headers were lost.

### How?

We can populate these headers before executing a request.

Closes NEXT-1663
Fixes #55942
2023-10-13 17:58:33 +00:00
Zack Tanner
3264d91dab
memoize useParams (#56771)
`useParams` is not referentially equal between renders which can lead to unexpected behavior when used as a dep. 
This memoizes the response from `useParams` similar to `useSearchParams`.

[slack x-ref](https://vercel.slack.com/archives/C04DUD7EB1B/p1697145987740229)
2023-10-13 07:47:05 +00:00
Zack Tanner
476af24628
Revert "Prefer module over main on main fields for app router server compiler" (#56766)
This was causing some issues with our deployments.

[slack x-ref](https://vercel.slack.com/archives/C04DUD7EB1B/p1697146531305779)
2023-10-12 21:38:44 +00:00
Jiachi Liu
6814ff147a
Prefer module over main on main fields for app router server compiler (#56532)
This change is to pick the esm assets for RSC server layer and server rendering side, this is for production and development perf purpose, also to let them apply with the ESM related optimization such as tree-shaking, barrel files optimizations, etc.

We found a few packages that can't be optimized easily in bundling because we're using "main" field so the packages are not able to be tree-shaked, ending up with large bundle in the dist. This will change a lot for the bundling improvements as some packages are only having "main" and "module" field. So switching from CJS to ESM means better bundling, more optimization, and faster code.

#56501 was a precondition for this, as previously the bundling strategy was applied to some library but triggered the invalid hooks erroring.

### Other Monior Change
Previously we'll prefer to resolve CJS as there're 2 versions of react, using CJS assets will help let them pass by require-hook to use canary react for app router bundling assets. But now we changed the approach to bundling nextjs runtime and react packages. Now we dropped the condition that prefered to resolve CJS exports for externals, since if you're putting them in `serverComponentsExternalPackages`, they're not using the built-in react, so could potentially having trouble if any dependency is using react but excluded in bundles. So far we didn't see any report to this.

Closes NEXT-1286
2023-10-12 16:10:55 +00:00
Tim Neutkens
3bb80cfb83
Improve chunk encoding test to check right path for Turbopack (#56747)
This only fixes the test check, Turbopack wil have to `encodeURI` the
files in the FlightManifest. Similar to what the webpack plugin does:
https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/plugins/flight-manifest-plugin.ts#L111

<!-- 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-10-12 14:59:26 +02:00
JJ Kasper
dabeb51fff
Fix SSG query with experimental-compile (#56680)
Ensures query isn't omitted when using experimental compile mode with SSG pages as it skips the isReady delay that is normally done with prerendering.
2023-10-11 01:52:36 +00:00
Steven
35f507242c
fix: next dev with edge runtime on windows (#56502)
- Reverts https://github.com/vercel/next.js/pull/44616
- Regression introduced in https://github.com/vercel/next.js/pull/51651
- Fixes https://github.com/vercel/next.js/issues/55013
2023-10-10 14:03:03 +00:00
Will Binns-Smith
3c326ea5e8
Turbopack + app router: always use externals for predefined packages (#56440)
This applies the predefined list of packages in server-external-packages.json as always external when used by app router in Turbopack

Test Plan: Added integration tests

Closes WEB-1709
2023-10-06 19:08:01 +00:00
Tim Neutkens
5a49b88fb9
Use consistent name for App Router tests (#56352)
Ensures all App Router tests have a unique name and similar reference
(with `-`).

<!-- 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 #

-->

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-10-06 11:06:06 +02:00
Tim Neutkens
524b31513a
Fix logging level in actions test (#56473)
This test was using the old format and because of that failed in
Turbopack. This fixes that.

<!-- 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-10-05 12:30:39 +02:00
Josh Story
65b0bb24af
Separate RSC and SSR jsx-runtime modules (#56438)
There should be no shared react packages in our server runtime. rsc should always be separate from ssr.

This update reconfigures the runtiem to eliminate shared react modules. the jsx runtime will now be separate for RSC and SSR. this is necessary because the implementations for the jsx runtime rely on React and they need to see the right versions.

Additionally I fixed an alias so that the shared subset react is used when using react-server condition.

I also fixed a bug in 2 tests related to class/className.

Note: this PR blocks upgrading React canary due to internal changes in how React state is managed in when using the `react-server` condition
2023-10-04 20:29:10 +00:00
JJ Kasper
af04ef353b
Ensure loader generated export default has name (#56388)
When the function name is omitted webpack generates a name from the loader config which can be really long un-necessarily. Providing a name for the default function alleviates this. cc @shuding who found this
2023-10-04 09:28:07 +00:00
Josh Story
09b0ca42fb
App Router - preinitialize chunks during SSR (#54752)
Today when we hydrate an SSR'd RSC response on the client we encounter import chunks which initiate code loading for client components. However we only start fetching these chunks after hydration has begun which is necessarily after the initial chunks for the entrypoint have loaded.

React has upstream changes that need to land which will preinitialize the rendered chunks for all client components used during the SSR pass. This will cause a `<script async="" src... />` tag to be emitted in the head for each chunk we need to load during hydration which allows the browser to start fetching these resources even before the entrypoint has started to execute.

Additionally the implementation for webpack and turbopack is different enough that there will be a new `react-server-dom-turbopack` package in the React repo which should be used when using Turbopack with Next.

This PR also removes a number of patches to React src that proxy loading (`__next_chunk_load__`) and bundler requires (`__next_require__`) through the `globalThis` object. Now the react packages can be fully responsible for implementing chunk loading and all Next needs to do is supply the necessary information such as chunk prefix and crossOrigin attributes necessary for this loading. This information is produced as part of the client-manifest by either a Webpack plugin or Turbopack.

Additionally any modifications to the chunk filename that were previously done at runtime need to be made in the manifest itself now. This means we need to encode the deployment id for skew protection and encode the filename to make it match our static path matching (and resolutions on s3) when using `[` and `]` segment characters.

There are a few followup items to consider in later PRs
1. we currently bundle a node and edge version of react-server-dom-webpack/client. The node version has an implementation for busboy whereas the edge version does not. Next is currently configured to use busboy when handling a fetch action sent as multipart with a node runtime. Ideally we'd only bundle the one platform we are buliding for but some additional refactoring to support better forking is possibly required here

This PR also updates react from 09285d5a7 to d900fadbf.

### React upstream changes

- https://github.com/facebook/react/pull/27439
- https://github.com/facebook/react/pull/26763
- https://github.com/facebook/react/pull/27434
- https://github.com/facebook/react/pull/27433
- https://github.com/facebook/react/pull/27424
- https://github.com/facebook/react/pull/27428
- https://github.com/facebook/react/pull/27427
- https://github.com/facebook/react/pull/27315
- https://github.com/facebook/react/pull/27314
- https://github.com/facebook/react/pull/27400
- https://github.com/facebook/react/pull/27421
- https://github.com/facebook/react/pull/27419
- https://github.com/facebook/react/pull/27418
2023-10-03 21:40:25 +00:00
Shu Ding
8469d0ac27
Add test case for the permalink option of useFormState (#56329)
Ref: https://github.com/facebook/react/pull/27372.

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-10-03 13:47:03 +02:00
Tim Neutkens
b79a11f6ad
Fix mjs import for Turbopack test (#56354)
Turbopack currently supports fully-specified `.mjs` imports. Double
check with @sokra to verify if this is correct.

<!-- 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-10-03 13:33:04 +02:00
Omar McIver
3172cfedc8
fix: support both decoded and encoded url requests of conventioned files (#56187)
Co-authored-by: Omar McIver <omar.mciver@mode3cloud.com>
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-10-03 00:02:49 +02:00
Boris Besemer
a2f9ef5a34
fix(next/client): keep hash when navigating from app to pages router (#56223)
### What?

Fixes the pages router not receiving a hash when being linked from the
app router.

### Why?

The hash being removed breaks sites that rely on it for client side
features.

### How?

The hash gets omitted from the URL when used as a key for the
preflightCache. Once it's clear that it's an external URL and that it's
not empty we can use the initial href to send the hash as well.

Not completely sure if there's an edge case that might break, I added an
extra check for when the hash is only used to scroll the page.

This might need an additional test case just for
`navigate-reducer.test.tsx`.

Fixes #56212

---------

Co-authored-by: Zack Tanner <zacktanner@gmail.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-10-02 14:43:04 -07:00
Sukka
86274e68aa
fix(#53190): add missing crossOrigin to assetsPrefix resources (#56311)
Fixes #53190.

Next.js App Router comprises two categories of resources, same-origin ones (RSC payload, in the form of inline `<script />`) and possibly third-party ones (chunks that respect the `assetPrefix`). The latter should also respect the `crossOrigin` option from `next.config.js`.

Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2023-10-02 17:21:49 +00:00
Zack Tanner
e970e050a9
Reland static prefetches & fix prefetch bailout behavior (#56228)
Reland #54403

Also modifies the implementation of #55950 to not change the prefetch behavior when there is flight router state - we should only check the entire loader tree in the static prefetch case, otherwise we're inadvertently rendering the component tree for prefetches that match the current flight router state segment. ([slack x-ref](https://vercel.slack.com/archives/C03S8ED1DKM/p1695862974745849))

This includes a few other misc fixes for static prefetch generation:
- `next start` was not serving them (which also meant tests weren't catching a few small bugs)
- the router cache key casing can differ between build and runtime, resulting in a bad cache lookup which results suspending indefinitely during navigation
- We cannot generate static prefetches for pages that opt into `searchParams`, as the prefetch response won't have the right cache key in the RSC payload
- Layouts that use headers/cookies shouldn't use a static prefetch because it can result in unexpected behavior (ie, being redirected to a login page, if the prefetch contains redirect logic for unauthed users)

Closes NEXT-1665
Closes NEXT-1643
2023-10-02 17:12:55 +00:00
Jiachi Liu
ac95a20276
Fix flaky test for size output (#56303)
x-ref: https://github.com/vercel/next.js/actions/runs/6380869104/job/17316148085?pr=56187

Tune the regex that match the correct number but without leading zero
2023-10-02 15:47:09 +00:00
Tim Neutkens
5f7faac1e6
More Turbopack test fixes (#56248)
Skips additional production tests.

Follow-up to #56089

<!-- 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-10-01 17:50:03 +02:00
JJ Kasper
e78eee25c5
Add additional handling for experimental-compile (#56224)
This ensures we properly set the `isReady` flag when building with `experimental-compile` and enables our main app dir test suite to ensure we don't regress on it.
2023-09-29 22:11:45 +00:00
Tim Neutkens
2e36e7d85d
Fix middleware-general test for Turbopack (#56211)
This test failed for Turbopack because of the rename from `i18n` to `__i18n` to not use that config. Turbopack does checking of config options to ensure it doesn't run when an option is not implemented, so that caused the test run to bail out.
2023-09-29 15:46:14 +00:00
Jiachi Liu
7b48a1e2f0
fetching logging on edge (#56108)
Support fetching logging working with edge runtime. When you enable edge runtime and experimental logging config in `next.config.js` you should be able to see the data fetching logs like edge runtime

```
 GET /default-cache 200 in 4304ms
 │ GET https://next-data-api-endpoint.vercel.app/api/random?no-cache 200 in 373ms (cache: SKIP)
 │  │  Cache missed reason: (cache: no-cache)
 │  │ GET https://next-data-api-endpoint.vercel.app/api/random?force-cache 200 in 163ms (cache: SKIP)
 │  │  │  Cache missed reason: (cache-control: no-cache (hard refresh))
 │  │ GET https://next-data-api-endpoint.vercel.app/api/random?revalidate-0 200 in 196ms (cache: SKIP)
 ```

Closes #55495
Closes NEXT-1661
2023-09-27 22:39:42 +00:00
Zack Tanner
4cfb0acd72
Revert: "Generate prefetch RSC payload during build for SSR paths (#54403)" (#56059)
Investigating problems this is causing where incorrect flight data is being generated (potentially not correctly bailing on non-static data) causing navigation issues. 

Reverts #54403
2023-09-27 21:38:49 +00:00
Tim Neutkens
c59de31a9b
Ensure unique name for app dir css tests (#56088)
Ensures this and the other describe block in the codebase have unique
names so that they're reported separately.

<!-- 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-09-27 18:38:01 +02:00
Tim Neutkens
633719e8cf
Skip Babel tests for Turbopack (#56091)
Follow-up to #56089, skips the Babel tests.

<!-- 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-09-27 16:12:43 +02:00
Tim Neutkens
cbe8238ebd
Skip production tests for Turbopack (#56089)
Follow-up to #56045. Found there was a group of tests for production
called `server mode` instead of `production mode`, those are skipped too
now.

<!-- 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-09-27 15:19:42 +02:00
Tim Neutkens
3eadfe925b
Skip experimental.nextScriptWorkers test for Turbopack (#56086)
This option is not supported in Turbopack yet but it is not a priority
as it's experimental.

<!-- 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-09-27 13:58:11 +02:00
Tim Neutkens
f2ad08e2c8
Ensure tests suites have unique names (#56085)
Currently these share the same name which makes it hard to track down
which failed.

I think this was accidental based on the previous `pnpm new-test`
workflow.

<!-- 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-09-27 13:55:45 +02:00
Vũ Văn Dũng
9fd5b96e1f
Make permanentRedirect return 308 in route handlers (#56065)
### Why?

`permanentRedirect` currently still returns a 307 response if called inside a route handler

<img width="465" alt="image" src="https://github.com/vercel/next.js/assets/44609036/e0cddd37-0292-4865-a423-7bf11ad6beae">

This PR tries to fix that.

### How?

Make `handleTemporaryRedirectResponse` (now renamed `handleRedirectResponse`) accept a status value, then `getRedirectStatusCodeFromError` is used to retrieve that status (307 or 308).
2023-09-27 10:28:30 +00:00
Jiachi Liu
4a6319cd86
Respect the original exported route segment config for sitemap (#56007)
Re-export the original route segment config in metadata routes loader, so they can be picked up by app route module wrapper

Closes #54057
Closes NEXT-1645
2023-09-26 17:04:58 +00:00
Shu Ding
4e44598249
Progressive enhancement for form state (#55584)
This PR implements progressive enhancement for `useFormState`:
1. Inline the form state in `__next_f` and use it for hydration.
2. Encode the new form state based on the action return value, and pass
it to Flight/Fizz.

Also fixed a problem caused by inconsistent React tree structure between
static-generation and non-static-generation.
2023-09-26 18:03:31 +02:00
Tim Neutkens
5f03a7129a
Move SRI test into separate suite (#56015)
Moves the experimental.sri config test into a separate test suite.

<!-- 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-09-26 15:34:34 +02:00
Zack Tanner
df12508be2
use experimentalHttpsServer flag when determining image optimizer protocol (#55988)
Inferring the protocol from the request meta is not reliable when the next server is running over `http` but sitting behind an https proxy. This instead plumbs the experimental https flag through to the optimizer so we can more reliably determine the protocol

Fixes #55971
2023-09-26 05:35:06 +00:00
JJ Kasper
57bb52d37d
Fix stale revalidate stream handling (#55978)
This ensures we don't block sending the stream down when handling stale revalidates, in edge runtime we leverage the existing `waitUntil` handling and in node.js runtime we couple this into our pipe readable handling to wait to close the writable until after the promise resolves. 

x-ref: https://github.com/vercel/next.js/issues/54193
2023-09-25 20:50:00 +00:00
Jimmy Lai
ca57258dbd
app router: ensure static prefetch renders loading.js (#55950)
Should fix the problem surfaced in https://github.com/vercel/next.js/issues/43548#issuecomment-1732416492 

The scenario in which this breaks is when we're asking the app renderer to generate a prefetch payload statically. The renderer was not checking if there were loading.js below the current rendering level, which is the root when statically rendering, so it would always render null, which was incorrect.

Tested out with the original repro  https://next-instant-loading-8nv6psqod-feedthejim.vercel.app
2023-09-25 12:59:31 +00:00
Jiachi Liu
a63b89b140
Loose types of app routes return value (#55849)
### What

#51394 introduced a pretty strict type of return value of route type
that causing failure with `next build`.
There're few ways of writing a app route, it could contain few return
values based on the usage:

* return a `Response` or promise of it
* return `NextResponse` of promise of it, since it's extended from
`Response`, same type
* use `redirect()` or `notFound(), since it returns `never`, and the
below code is not reached, the handler itself could still return void.
e.g. using `redirect` in a `GET` route

We loosed the type so `redirect()` can be still allowed without
specifying the return value there.
Related typescript issue:
https://github.com/microsoft/TypeScript/issues/16608#issuecomment-309327984

### How
* Re-enable the bail on types / build error in the app-routes tests
* Separate the tests, move runtime erroring ones to
`test/e2e/app-dir/app-routes-errors`
* Add new case to app-routes tests of mixed return value

Closes #55623 
Related #55604
2023-09-25 11:34:21 +02:00
Jiachi Liu
2248cfc044
test: re-enabled the disabled manifest test (#55810)
Re-enable the disabled test
2023-09-23 17:57:08 +00:00
Zack Tanner
c2e0213fa5
fix bugs pertaining to server actions + navigation (#55853)
This fixes some scenarios where executing a server action after navigation can cause the action to behave incorrectly (double submitting, not resolving). There are two separate issues:

- `canonicalUrl` and `pendingNavigatePath` were not constructed using the same function (`createHrefFromUrl`) so in certain situations they'd be comparing different values
- a fulfilled inFlightServerAction should not be invoked again

Closes NEXT-1655
Closes NEXT-1654
Fixes #55845
Fixes #55814
Fixes #55805
2023-09-23 01:42:39 +00:00
Zack Tanner
f20728ac3d
fix Image when used with --experimental-https (#55775)
The IPC request to `imageOptimizer` assumed the server was listening on http, so this updates it to pull the protocol from `getRequestMeta` instead. This also adds the option to pass in a path to the CA Root so that the dev server can add it to `NODE_EXTRA_CA_CERTS`


Closes NEXT-1646
Fixes #55706
2023-09-22 14:26:20 +00:00
Jiachi Liu
063154918c
Fix react packages are not bundled for metadata routes (#55579)
`isAppLayer` condition was missing `app-metadata-route` layer, made it
as a util now like other webpack layer utils, add metadata route layer
to the group. Then `React.cache` can be available there.

Also update regex to be compatible across platform

Fixes #55561 
Closes NEXT-1635
2023-09-19 15:45:59 +02:00