Commit graph

25 commits

Author SHA1 Message Date
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
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
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
Vũ Văn Dũng
f47c409174
Check for type of route handler returned value at build time (via the TS plugin) and at runtime (#51394)
### What?

Fixes #51130. Before this PR, the package assumes that route handlers return a `Response` which is not necessarily the case.

The linked issue specified three suggestions to resolve this

1. Return a default 200 response
2. Throw a better error message
3. or both

~~In this issue I implemented (3), except that it is a warning and not an error. Do tell if the team wants to follow a different approach, as it is not too hard to change this.~~

This PR implements (2).

### How?

The returned value of the handler is checked at runtime to ensure it is actually a `Response` instance.

The return type `AppRouteHandlerFn` is also modified to `unknown` to avoid similar assumptions elsewhere.

The TS plugin is also modified to check for the return type during build time.



Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-09-08 03:26:53 +00:00
Jiwon Choi
043114773f
fix: cookies().has() breaks in app-route (#54112)
- Added `has` to ResponseCookies in [edge-runtime/cookies#533](https://github.com/vercel/edge-runtime/pull/533)
- Upgraded edge-runtime/cookies to 3.3.0 #54117 
- Added a test case


Fixes #54005 #54111
2023-08-17 01:06:07 +00:00
Wyatt Johnson
3e821ef189
Support basePath with edge runtime for Custom App Routes (#52910)
Fixes a bug where the edge runtime didn't support `basePath` with Custom App Routes.

- Fixes #49661
2023-07-19 23:54:34 +00:00
Shu Ding
7dfa56c714
Fix missing request body in DELETE and OPTIONS Route Handlers (#51874)
It looks like the `content-length` header can't be ignored for the request forwarded to the render worker. See https://github.com/nodejs/node/issues/27880.

Closes #48096, closes #49310. Fix NEXT-1194
fix NEXT-1114
2023-06-27 11:44:01 +00:00
Steven
4b4925f4e8
fix: set cookies followed by redirect() (#49965)
- Fixes #49237 


fix NEXT-1120

Co-authored-by: Wyatt Johnson <633002+wyattjoh@users.noreply.github.com>
2023-05-19 00:17:19 +00:00
Wyatt Johnson
aca83d2a09
Move App Route handler logic into bundle (#47633)
Previously the app route handler was included in the server bundle, and called into user code to execute the underlying handler logic. This PR serves to move the handler code into the bundle so that the Node.js environment more closely matches the Edge environment.

This also updates the Rust code for the new loader.
fix NEXT-712 ([link](https://linear.app/vercel/issue/NEXT-712))
2023-03-29 19:31:30 +00:00
JJ Kasper
4d0f5a0503
Revert "Move App Route handler logic into bundle" (#47614)
Reverts vercel/next.js#47582

This breaks turbopack support so we should address that before landing.
Potentially we can re-use the same loader for both which would eliminate
this issue.

x-ref:
https://github.com/vercel/next.js/actions/runs/4544370605/jobs/8010399819
x-ref:
https://github.com/vercel/next.js/actions/runs/4545240650/jobs/8012475072?pr=47611
fix NEXT-712 ([link](https://linear.app/vercel/issue/NEXT-712))
2023-03-28 10:12:03 -07:00
Wyatt Johnson
5dc31354b9
Move App Route handler logic into bundle (#47582)
Previously the app route handler was included in the server bundle, and
called into user code to execute the underlying handler logic. This PR
serves to move the handler code into the bundle so that the Node.js
environment more closely matches the Edge environment.
fix NEXT-712 ([link](https://linear.app/vercel/issue/NEXT-712))
2023-03-28 16:38:17 +02:00
Ngô Đức Anh
c748b16555
Feat(next-types-plugin): added support for Route Handlers (#47185)
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 or adding/fixing 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?

This PR adds next-types-plugin's support for Route Handlers.

### Why?

Since `route.js` is somewhat like `page.js` and `layout.js`, I find it to be necessary that we also enable TS checks for these files as well.

### How?

Just changing the `createTypeGuardFile` function a bit and adding a few new tests, yeah.
2023-03-26 23:34:54 +00:00
Jiachi Liu
93152db9b1
Fix bad route path for custom metadata routes (#47286)
We introduced static route `robots.txt` and dynamic route `robots.js` for metadata, it should still allow users to create their own customized version. This issue is caused by a route conflicts. Only append `/route` to page path when there's not ending with `/route`


Fixes #47198
Closes NEXT-850
2023-03-21 15:55:32 +00:00
Wyatt Johnson
0be4db325e
Warn about default exports with App Routes (#47263)
When a user exports a default function from a `route.ts` file, they may
think that this would be handled in the same way that our `pages/api/`
routes handled it. App Routes require an exported function for each HTTP
method instead.

This adds warnings to the development console when a user provides a
default export (or no HTTP method at all).

Future ideas:

- Error during production build when these conditions are met
fix #46375
fix NEXT-660 ([link](https://linear.app/vercel/issue/NEXT-660))

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-03-20 14:52:41 -07:00
JJ Kasper
76f7645ce6
Update app route handler proxy handling (#47088)
This ensures our Proxy wrapping request fields works properly in the
edge-runtime by explicitly binding to the correct request instance.

x-ref: [slack
thread](https://vercel.slack.com/archives/C03UR7US95F/p1678730563467089?thread_ts=1678662292.695769&cid=C03UR7US95F)
2023-03-13 11:34:22 -07:00
Wyatt Johnson
45492eacc0
Fix originalRequest not available for edge requests (#46741)
For requests made via the edge runtime, they will not contain the
`originalRequest` object used by the request storage to enable
headers/cookie access. This uses the request object passed when in the
edge runtime.
2023-03-03 18:14:49 +00:00
Wyatt Johnson
3229ed79a6
Support force-static on App Routes (#46693)
Supports `export const dynamic = 'force-static'` on App Routes. This strips pieces of information from the request object to ensure subsequent requests don't accidentally cache user specific data:

- Removes `searchParams`
- Removes `headers`
- Removes `cookies`
- Removes `host`
- Removes `port`
- Removes `protocol`

[NEXT-682](https://linear.app/vercel/issue/NEXT-682/force-static-doesnt-work-with-app-routes)
2023-03-02 19:28:37 +00:00
JJ Kasper
99a7a34f96
Ensure generateStaticParams works in route (#46310)
x-ref: [slack
thread](https://vercel.slack.com/archives/C04DUD7EB1B/p1677165000745519)

## Bug

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

---------

Co-authored-by: Jimmy Lai <laijimmy0@gmail.com>
2023-02-23 18:33:08 +01:00
JJ Kasper
1253a3035f
Add ISR handling for app routes (#46133)
Follow-up to https://github.com/vercel/next.js/pull/45716 this updates
the ISR handling for app routes. This also ensures the E2E deploy tests
are running for the `app-routes` test suite.

fixes NEXT-509

Relies on https://github.com/vercel/vercel/pull/9489

---------
2023-02-21 15:25:42 -08:00
Tim Neutkens
5599c47ed6
Add edge support for route handlers (#45990)
- Add test for edge route
- Add edge route loader
- Ensure edge route does not trigger static generation
- Remove unused import
- Use new loader during compilation
- Add names for routeKind to help debugging
- Ensure route is considered a appDir page
- Return response from edge runtime
- Handle edge route in dev and prod

Fixes NEXT-510

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

## Bug

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

## Feature

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

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-16 16:32:37 +01:00
JJ Kasper
3e919b6add
Fix flakey app cli output checks (#45960)
x-ref:
https://github.com/vercel/next.js/actions/runs/4186567737/jobs/7255320047
2023-02-15 11:33:17 -08:00
Hannes Bornö
087aa6e83e
Log dev error in Server Router (#45951)
Fixes NEXT-561

## Bug

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

## Feature

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

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-15 17:09:48 +00:00
JJ Kasper
778a2a7442
Fix app routes on deploy (#45931)
Follow-up to https://github.com/vercel/next.js/pull/45716 this ensures
we correctly construct the initial URL value as it must be a fully
qualified URL. Existing tests caught this failure when running in deploy
mode.
2023-02-15 09:54:39 +01:00
JJ Kasper
7d8f85bd8d
Update E2E deploy tests (#45929)
Updates test cases that aren't expected to work for deploy mode at the
moment.

x-ref:
https://github.com/vercel/next.js/actions/runs/4180235531/jobs/7241206278
2023-02-14 20:00:58 -08:00
Wyatt Johnson
74ca99c866
Server Router Improvements (#45716)
This adds updated matching handle for the server to separate out the matching and executing of different route types e.g. page routes, API routes, and app routes. 

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-02-15 00:16:45 +00:00