Commit graph

368 commits

Author SHA1 Message Date
Tobias Koppers
aa3422a56f always allow to close the error overlay (vercel/turbo#3624)
It's difficult to test an app, if a single error blocks using the whole
page.

Therefore, error overlay should always be closeable.
2023-02-03 19:10:47 +01:00
Tobias Koppers
bb0977cf77 initial work for compile-time evaluation (vercel/turbo#3495)
Allows to skip unreachable code based on compile-time constant
conditions

e. g.

```
if (!process.turbopack) {
  require("will-not-be-processed");
}
```

* add if() and process.turbopack fixes WEB-491
* Removes unreachable code fixes WEB-498
* evaluate some logical operations `&&`, `||`, `??`, `!`
* fix arguments handling fixes WEB-529
* nested effects for function called with closures
* handle closures when array methods are used fixes WEB-538
* evaluates `process.turbopack` fixes WEB-496
2023-02-03 17:34:17 +01:00
Will Binns-Smith
0851ce9885 Move integration test entries to input/ (vercel/turbo#3590)
Moves all next-dev integration test code into a parent `input/`
directory, like our snapshot tests. This prepares for introducing
`issues` snapshots to next-dev integration tests.

Test Plan: CI
2023-02-03 11:26:47 +01:00
Tobias Koppers
8cd9781f24 update next.js to 13.1.7-canary.2 (vercel/turbo#3595) 2023-02-02 18:23:42 +01:00
Alex Kirszenberg
5dce8dc0e2 Share request resolving logic between http and update servers (vercel/turbo#3597)
With the addition of the Next.js routing layer at the root of our
content sources, we need the update server to also be able to follow
Next.js rewrites, like the HTTP server.

This requires the following:
* Rewrites need to be able to indicate a source where to "resume" after
rewriting. Otherwise, we would enter an infinite loop of the Next.js
layer rewriting to the Next.js layer.
* The resolving logic from `process_request_with_content_source` needs
to be extracted into its own function, so it may be called both from the
HTTP server (which expects fully resolved `ReadRef`s) and the update
server (which needs a `VersionedContentVc`): this is where
`resolve_source_request` comes in.

Co-authored-by: Justin Ridgewell <justin@ridgewell.name>
2023-02-02 15:08:06 +01:00
Tobias Koppers
4a2d7cac53 improve error reporting for fatal errors (vercel/turbo#3550)
This adds the turbo-tasks function name as `context` of all fatal errors
propagated from executing or reading turbo-tasks functions.

That should help in debugging fatal errors.
2023-02-02 06:53:36 +01:00
Leah
0e28913e17 show turbopack warnings in error overlay (vercel/turbo#3465) 2023-02-01 18:45:39 +01:00
Leah
9056eaf71b external node_modules for SSR (vercel/turbo#3361)
Adds resolve plugins (currently only running `after_resolve`, could add
`resolve` later)

Had to fix `ResolveResult::merge_alternatives` because it merged two
external results into `Unresolvable`
2023-02-01 17:26:10 +01:00
Alex Kirszenberg
862a0a1535 Wrap CSS chunk items in a @layer (vercel/turbo#3542)
In Turbopack, as a consequence of our lazy compilation model, CSS chunks
can contain duplicate CSS chunk items. This can cause issues with
precedence. Take the following example:

Initial CSS chunk:
```css
/* ... */

/* chunk item A */
h1 {
  font-size: 2rem;
}

/* ... */

/* other chunk item */
h1 {
  font-size: 4rem;
}

/* ... */
```

Dynamic CSS chunk (loaded after the first page load completes)
```css
/* ... */

/* chunk item A */
h1 {
  font-size: 2rem;
}

/* ... */
```

In this example, when the page first loads, the following rule will be
applied:
```css
h1 {
  font-size: 4rem;
}
```

But as soon as the dynamic CSS chunk loads, the following rule will be
applied instead:
```css
h1 {
  font-size: 2rem;
}
```

However, from the order of rules in the initial load, we know that the
former should still apply.

We can remedy this particular issue by wrapping each CSS chunk item into
its own
[`@layer`](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer)
(thanks @sokra for the idea!). This ensures that when a CSS chunk item
is re-encountered at a later time, it is automatically de-duplicated
thanks to the inherent CSS layering algorithm.

This is not an issue in Next.js as we can't have duplicated CSS chunk
items.
2023-02-01 13:58:46 +01:00
Tobias Koppers
d5de3a409b add worker condition to edge compilation (vercel/turbo#3549)
some packages expect `worker` or `browser` condition to work on edge
runtime
2023-02-01 08:06:32 +01:00
Will Binns-Smith
1b2a24058f font/google test: update cssmodule classnames (vercel/turbo#3568)
#3437 wasn't up to date with main when it was merged.

Test Plan: CI
2023-01-31 18:04:56 -08:00
Will Binns-Smith
817e089a30 turbopackLoaders: accept basic loader options (vercel/turbo#3540)
Fixes WEB-525

This allows webpack loaders defined by `experimental.turbopackLoaders` in `next.config.js` to pass serializable options to loaders.

It expands `turbopackLoaders` to accept not just loader names for a given extension, but also `{loader: string, options: Object}`. These options are verified to be serializable and passed to `loader-runner` and exposed as `getOptions()` to the loader.

Test Plan: Added an integration test for basic options. Verified the serializable check by passing `class Foo {}; new Foo()` as an option value and verified the error was displayed.
2023-01-31 20:44:00 +00:00
Will Binns-Smith
eccd34fc75 css modules: use a terser selector format that requires less escaping (vercel/turbo#3437)
Fixes WEB-447.

Previously, generated classnames would include literal `/` characters,
which need escaping when generating selectors. While users should still
escape selectors, this avoids it for this case and makes generated code
easier to read. This format is also more aligned with what webpack-based
css modules generates.

This also removes the square character as a separator, since it breaks
words and makes double-clicking for selection more difficult.
2023-01-31 10:13:55 -08:00
Alex Kirszenberg
779bb9e06a Return a 404 status code when a data route is .notFound (vercel/turbo#3545)
Returning a 404 status code is required for the client-side router to
redirect to the error page.
2023-01-31 17:43:01 +01:00
Will Binns-Smith
7409489910 @next/font/google: css variable followup (vercel/turbo#3514)
This addresses feedback from vercel/turbo#3505
2023-01-30 21:32:09 +00:00
Will Binns-Smith
c6701e1e79 Align ids and data attributes with Next.js's redbox (vercel/turbo#3522)
Next.js's integration test suite depends on these ids and data attributes to select against items in running test pages. This gets us passing some of those tests.
2023-01-30 07:37:25 +00:00
Leah
786dd71bf1 add pageExtensions support to appDir (vercel/turbo#3510)
The previous PR vercel/turbo#3499 only implemented support for `pages` directory.

The `app` directory previously had no validation at all, it's added with
this PR.

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2023-01-28 22:09:11 +01:00
Justin Ridgewell
aa0fb57093 Fix infinite loop caused by error page rendering (vercel/turbo#3525)
The new error paths introduced in vercel/turbo#3448 both write their outputs to
`.next/server/pages/`, which conflicts with:
- The main pages source
- Each other

The infinite loop is very fun:
1. We need a `NodeJsPoolVc` to render pages
2. To get a `NodeJsPoolVc`, you need to write your files onto disk
    - So a pool is dependent on the contents of those files
3. When we render an error page, we need to write those files to disk
4. The error page shares the same file entrypoint as the main page
source

So, to render an error, we write the entrypoint, which is shared with
main source. This alone is pretty bad, because it will invalidate our
page source's node pool (and kill those processes). But, the loop is
triggered by a more subtle bug:

When we write a file, we read it to see if the contents have changed.
Writing creates a dependency on the read! So when the error page is
written to disk, it invalidated the read we preformed when we wrote the
main page. That invalidated the main page (and it's node pool), and so
we rendered again. That wrote the main page's code to disk, invalidating
the read of the error page performed when we wrote the error page. ♾️

(I'll be opening more PRs…)
2023-01-28 21:04:07 +01:00
Alex Kirszenberg
9a5695c72b Add headers to app routes IPC (vercel/turbo#3518) 2023-01-27 15:56:25 -05:00
Alex Kirszenberg
822bb5d15c Handle getStaticProps.redirect (vercel/turbo#3497)
This PR adds support for returning `redirect: { destination, permanent,
statusCode }` from `getStaticProps`.
2023-01-27 14:39:10 +01:00
Alex Kirszenberg
0cf02c89ed Handle 404 page when getStaticProps.notFound = true or the route is not found (vercel/turbo#3448)
This specifically supports the case where `getStaticProps.notFound =
true` or a matching route is not found, but not the case where
`getStaticPaths.fallback = false` and the route is not enumerated;

This separate case will be implemented in another PR.
2023-01-27 14:38:20 +01:00
Tobias Koppers
6416dcc276 enable and fix backtracing for fatal errors in tasks (vercel/turbo#3504)
Enables the anyhow backtrace feature to capture backtraces for errors
too when `RUST_BACKTRACE` is enabled.

When running in release mode compiling with
`CARGO_PROFILE_RELEASE_DEBUG=1` is recommended to get debug info into
the errors.
2023-01-27 08:49:21 +01:00
Will Binns-Smith
c1217ad3e3 @next/font/google: support setting a css variable for fonts (vercel/turbo#3505)
Fixes WEB-501

Fixes https://github.com/vercel/turbo/issues/3139

This adds proper support for the [`variable`
property](https://nextjs.org/docs/api-reference/next/font#variable) when
constructing fonts. This results in:

* defining a selector in the css module for a classname defining the css
custom property (css variable), whose value is the font family
* adding a `variable` property to the resulting js object whose value is
the classname used in the selector above

Test Plan: Added an integration test.
2023-01-27 08:48:15 +01:00
Justin Ridgewell
eb1ca3fb70 Review fixes for Next routing (vercel/turbo#3501)
This completes a few review requests from vercel/turbo#3446.
2023-01-26 21:02:54 -05:00
Justin Ridgewell
c474d53ac1 Handle routing by calling Next.js code (vercel/turbo#3446)
This implements routing by using a-yet-to-be-implemented API exposed by
Next.js. The API follows something similar to:

```typescript
type MakeResolver = (config: NextConfig) => Resolver;
type Resolver = (IncomingMessage, ServerResponse) => Promise<void> | void;

import { makeResolver } from "next/dist/...";
const resolver  = makeResolver(nextConfig as object);

// Later, once we have a request we'd like to route:
// We don't care what the promise resolved to, we just want it to settle.
await resolver(req, res);
```

The resolver can do 1 of 3 things with this:
1. ~~Return a redirect response~~ Removed
2. Return a rewrite response
3. Stream a middleware response

> ~~1. Return a redirect response~~

<details>

First, ensure a `x-nextjs-route-result: 1` header is present on the
response. Then return a JSON encoded body:

```typescript
{
  url: string,
  statusCode: u16,
  headers: Record<string, string>
  isRedirect: true
}
```

The Rust server will then respond with a redirect using to the
appropriate location.

</details>

> 2. Return a rewrite response

First, ensure a `x-nextjs-route-result: 1` header is present on the
response. Then return a JSON encoded body:

```typescript
{
  url: string,
  headers: Record<string, string>
}
```

The Rust server will use this updated URL to request content from our
handlers.

> 3. Stream a middleware response

Ensure `x-nextjs-route-result` header **is not present** on the
response. All headers will be sent back, and the body will be streamed
back to the browser.

- - -

TODO:
- ~~Do `headers` actually matter to a `redirect`~~ Yes?
- ~~Does `statusCode` actually matter to a `rewrite`?~~ No
- ~~We can't handle streaming body responses yet.~~ Mocked by buffering.

Fixes WEB-228

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-01-26 21:15:48 +01:00
Leah
dc13baf6e7 add support for pageExtensions in next.config.js (vercel/turbo#3499) 2023-01-26 20:14:35 +00:00
github-actions[bot]
ff69c0f4e0 Update Google font-data.json (2023-01-26) (vercel/turbo#3479)
Automated changes by
[create-pull-request](https://github.com/peter-evans/create-pull-request)
GitHub action

Co-authored-by: wbinnssmith <wbinnssmith@users.noreply.github.com>
2023-01-26 10:07:13 -08:00
Leah
767dccb9c5 decode magic identifiers in error overlay (vercel/turbo#3433) 2023-01-26 17:50:20 +01:00
Tobias Koppers
8b42ea51de add edge compilation, runtime config parsing and edge apis (vercel/turbo#3396) 2023-01-26 12:55:51 +01:00
Tobias Koppers
991de97b75 partially support next metadata (vercel/turbo#3464)
This adds support for metadata in pages and layouts.

Full metadata support also needs support for implicit metadata with
files named `icon.ico` etc.

This PR also improves the test suite and adds a basic test case for app
dir support
2023-01-25 19:25:33 +01:00
github-actions[bot]
639ac4ff60 Update Google font-data.json (2023-01-25) (vercel/turbo#3463)
Automated changes by
[create-pull-request](https://github.com/peter-evans/create-pull-request)
GitHub action

Co-authored-by: wbinnssmith <wbinnssmith@users.noreply.github.com>
2023-01-25 08:26:02 +01:00
Alex Kirszenberg
7902145858 Fix data requests for dynamic routes (vercel/turbo#3435)
In addition to the fix, I've extracted the path regex/params matching logic from `turbopack-node` to `next-core`. `turbopack-node` now only declares a value trait which `next-core` implements.
2023-01-24 18:30:23 +00:00
Tobias Koppers
b256922644 refactor content sources to avoid evaluating content before serving (vercel/turbo#3334)
fixes WEB-141

This changes the ContentSource API.

Before a ContentSource returned a tuple of specificity and content.
Now it returns a tuple of specificity and a get_content function.

The old way made it very inefficient to combine multiple ContentSource.
All combined ContentSources were ask before, all returned specificity
and content, and they are ordered after that. That required to compute
all contents, even if it is not really used after ordering.

Now we order specificity and a get_content function and only compute the
content (by calling get_content) with the final result.

It also changes how data needs are expressed in the `get_content`
function. Instead of asking it over and over again with more data, it
statically returns the needed data with `vary` and the `get` function is
only called with that data. This is technically more limited than the
old way, but one can workaround that if needed. But practically we
probably never need that. On the other hand it improves the number of
function calls, since the `vary` method is only called once and the
`get` only once per request. Before data needs required at least 2 `get`
calls per request.
2023-01-24 16:16:33 +01:00
Justin Ridgewell
98c9d723ff Handle empty pageData during HMR (vercel/turbo#3412)
Our `pageData` HMR process treated an undefined response as an error condition, but if the page doesn't have a `getXyzProps` exported method, then this is the default response. This prevented us from having any pages without page props.

The new code just sends down an empty object, which seems to work for establishing the connection. HMR updates are sent down (and seem to trigger restarts, at least from what I tested with `getStaticProps`).

Fixes WEB-445
2023-01-21 06:47:38 +00:00
Leah
f3501d940b prevent injecting non js files in app renderer (vercel/turbo#3399) 2023-01-20 20:11:54 +00:00
Justin Ridgewell
82ac6f45f8 Update mdxjs and testing crates (vercel/turbo#3398)
This updates our `mdxjs` dependency to the tip of
https://github.com/wooorm/mdxjs-rs/pull/11, so that we can update it's
`swc_core` dependency (it conflicts with ours).

It also updates the `testing` crate to update it's `swc_common`
dependency (also conflicting with ours), and makes it a workspace
dependency.
2023-01-20 13:47:41 -05:00
Donny/강동윤
90801bfd3d Introduce next-dev-tests to rebuild automatically on test input changes (vercel/turbo#3288)
I verified that the crate is rebuilt if I make a change of inputs.

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2023-01-20 15:12:13 +01:00
Tobias Koppers
f8cd413786 improve resolveAlias to allow conditions to allow to apply aliases to… (vercel/turbo#3324)
… client/server only
2023-01-20 12:54:21 +01:00
Tobias Koppers
a168ccc9d3 add garbage collection (vercel/turbo#3094)
fixes WEB-265
fixes WEB-268
fixes WEB-4
fixes WEB-359
2023-01-20 10:16:02 +01:00
Will Binns-Smith
ed4bd62f2d Always mark styled-jsx as external to avoid duplicating with bundled copies (vercel/turbo#3386)
Fixes WEB-412.

Previously, references to styled-jsx inserted by the swc transform would be bundled, which duplicated the copy required by next, which is external. This marks the dependency is to be resolved as external always.

Test Plan: Verified in a small app that `<style jsx>` nodes result in a server-rendered response with the appropriate `<style>` tag in the head.
2023-01-20 04:23:26 +00:00
Will Binns-Smith
a922403b64 NodeProcessPool: implement debug mode for a single-process passing --inspect-brk (vercel/turbo#3385)
This implements a debug mode for spawning Node processes across multiple surfaces including `evaluate` and `get_renderer_pool`.

It:

* automatically limits concurrency to 1 process per pool
* passes `--inspect-brk` to spawned Node process
* sets the timeout to effectively-infinite duration

Test Plan: set the value to true for the call to `get_renderer_pool` in render_static.rs and attached to a debuggable server renderer process.
2023-01-20 01:58:11 +00:00
Will Binns-Smith
0aaf4fd87d Basic @next/font/google integration test (vercel/turbo#3170)
This:

* Implements a basic http server to mock returning a stylesheet from the Google Fonts API (**Note**: This importantly does *not* mock returning the font resources themselves, which are currently loaded by the browser. We should implement something to do this when we implement caching arbitrary http resources traced from `url()` and this is no longer loaded by the test browser)
* Adds an integration test that asserts the basic shape of the JS object returned by font functions
* ~Adds an integration test that asserts the browser correctly loads a font for the ascii unicode range, along with the appropriate `font-display`, variant, etc.~ Unfortunately `document.fonts` is not reliable with different font-display loading patterns as any of them can fall back.

As noted, this does not mock responses from Google for the font resources themselves, which are currently loaded by the test browser. This means that we'll be dependent on this external service for passing integration tests until we implement caching of `url()`s in Turbopack. We should monitor the reliability of this test.
2023-01-19 22:11:28 +00:00
Leah
1d3e782841 add full next.js config (vercel/turbo#3343) 2023-01-19 21:54:47 +01:00
Justin Ridgewell
f9dbb5d90a Install AsyncLocalStorage for appDir rendering (vercel/turbo#3374)
https://github.com/vercel/next.js/pull/44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by patching the node import onto `globalThis.` Importantly, it's then used in the module scope during imports, so we need to install the polyfill early in the app rendering startup.

Fixes https://github.com/vercel/turbo/issues/3319
2023-01-19 07:32:42 +00:00
Leah
dc45f2d460 fix error overlay sometimes now showing (vercel/turbo#3331) 2023-01-19 02:15:44 +01:00
Tobias Koppers
ee6ad2e081 rename turbopackWebpackLoaders to turbopackLoaders (vercel/turbo#3325)
since they don't have exactly the same API

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-01-18 10:19:42 +01:00
Leah
611cc70338 read env vars from next.config.js (vercel/turbo#3128) 2023-01-17 18:51:29 +01:00
Tobias Koppers
1159b548c6 improve SSR data handling for pages (vercel/turbo#3326) 2023-01-16 22:39:26 +01:00
Alex Kirszenberg
ac8fa6849d Add basic test for next/dynamic (vercel/turbo#3307)
This adds two basic tests for next/dynamic:
* one ensures that SSR supports dynamic components;
* the other ensures that SSR doesn't run when `ssr: false`.
2023-01-13 16:40:40 +01:00
Alex Kirszenberg
4ffd82c8da Apply the next/dynamic SWC transform (vercel/turbo#3184)
This PR adds the next/dynamic SWC transform to the repo. This is copied
over from the implementation in
[](f70a6bfb86/packages/next-swc/crates/core/src/next_dynamic.rs),
with an additional mode for Turbopack.

For now, it applies it to all sources files (excluding node_modules, but
including embedded modules).

I've refactored the Next transforms logic to make it more easily
extendable in the future, and avoid creating more Vcs than absolutely
necessary.

This PR is the last item in the list of what's needed to support
next/dynamic in development.
2023-01-13 14:18:14 +01:00