Skip to content

Commit

Permalink
Improve top level await coverage (#64508)
Browse files Browse the repository at this point in the history
## What?

- Changes webpack output target to `es6` (required for `async function`
output)
- Adds tests for top level await in server components and client
components (App Router)
- Converted the async-modules tests to `test/e2e`
- Has one skipped test that @gnoff is going to look into. This shouldn't
block merging this PR 👍


Adds additional tests for top level `await`.

Since [Next.js
13.4.5](https://github.com/vercel/next.js/releases/tag/v13.4.5) webpack
has top level await support enabled by default.

Similarly Turbopack supports top level await by default as well.

TLDR: You can remove `topLevelAwait: true` from the webpack
configuration.


In writing these tests I found that client components are missing some
kind of handling for top level await (async modules) so I've raised that
to @gnoff who is going to have a look.

<!-- 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 NEXT-3126
Fixes #43382
  • Loading branch information
timneutkens committed Apr 17, 2024
1 parent dccc6ec commit 1fd93ee
Show file tree
Hide file tree
Showing 26 changed files with 2,817 additions and 564 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -42,3 +42,4 @@ test/development/basic/hmr/components/parse-error.js
packages/next-swc/docs/assets/**/*
test/lib/amp-validator-wasm.js
test/production/pages-dir/production/fixture/amp-validator-wasm.js
test/e2e/async-modules/amp-validator-wasm.js
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -39,3 +39,4 @@ bench/nested-deps/components/**/*
**/.tina/__generated__/**
test/lib/amp-validator-wasm.js
test/production/pages-dir/production/fixture/amp-validator-wasm.js
test/e2e/async-modules/amp-validator-wasm.js
2 changes: 1 addition & 1 deletion packages/next/src/build/webpack/config/blocks/base.ts
Expand Up @@ -18,7 +18,7 @@ export const base = curry(function base(
? 'node18.17' // Same version defined in packages/next/package.json#engines
: ctx.isEdgeRuntime
? ['web', 'es6']
: ['web', 'es5']
: ['web', 'es6']

// https://webpack.js.org/configuration/devtool/#development
if (ctx.isDevelopment) {
Expand Down
14 changes: 12 additions & 2 deletions packages/next/src/lib/typescript/writeConfigurationDefaults.ts
Expand Up @@ -9,7 +9,7 @@ import * as Log from '../../build/output/log'

type DesiredCompilerOptionsShape = {
[K in keyof CompilerOptions]:
| { suggested: any }
| { suggested: any; reason?: string }
| {
parsedValue?: any
parsedValues?: Array<any>
Expand All @@ -23,6 +23,11 @@ function getDesiredCompilerOptions(
tsOptions?: CompilerOptions
): DesiredCompilerOptionsShape {
const o: DesiredCompilerOptionsShape = {
target: {
suggested: 'ES2017',
reason:
'For top-level `await`. Note: Next.js only polyfills for the esmodules target.',
},
// These are suggested values and will be set when not present in the
// tsconfig.json
lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
Expand Down Expand Up @@ -168,7 +173,12 @@ export async function writeConfigurationDefaults(
}
userTsConfig.compilerOptions[optionKey] = check.suggested
suggestedActions.push(
cyan(optionKey) + ' was set to ' + bold(check.suggested)
cyan(optionKey) +
' was set to ' +
bold(check.suggested) +
check.reason
? ` (${check.reason})`
: ''
)
}
} else if ('value' in check) {
Expand Down
@@ -0,0 +1,6 @@
'use client'
const appValue = await Promise.resolve('hello')

export default function Page() {
return <p id="app-router-client-component-value">{appValue}</p>
}
5 changes: 5 additions & 0 deletions test/e2e/async-modules-app/app/app-router/page.tsx
@@ -0,0 +1,5 @@
const appValue = await Promise.resolve('hello')

export default function Page() {
return <p id="app-router-value">{appValue}</p>
}
16 changes: 16 additions & 0 deletions test/e2e/async-modules-app/app/layout.tsx
@@ -0,0 +1,16 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
21 changes: 21 additions & 0 deletions test/e2e/async-modules-app/index.test.ts
@@ -0,0 +1,21 @@
/* eslint-env jest */
import { nextTestSetup } from 'e2e-utils'

describe('Async modules', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('app router server component async module', async () => {
const browser = await next.browser('/app-router')
expect(await browser.elementByCss('#app-router-value').text()).toBe('hello')
})

// TODO: Investigate/fix issue with React loading async modules failing.
// Rename app/app-router/client-component/skipped-page.tsx to app/app-router/client-component/page.tsx to run this test.
it.skip('app router client component async module', async () => {
const browser = await next.browser('/app-router/client')
expect(
await browser.elementByCss('#app-router-client-component-value').text()
).toBe('hello')
})
})
1 change: 1 addition & 0 deletions test/e2e/async-modules-app/next.config.js
@@ -0,0 +1 @@
module.exports = {}
2,254 changes: 2,254 additions & 0 deletions test/e2e/async-modules/amp-validator-wasm.js

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions test/e2e/async-modules/index.test.ts
@@ -0,0 +1,60 @@
/* eslint-env jest */
import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'

describe('Async modules', () => {
const { next, isNextDev: dev } = nextTestSetup({
files: __dirname,
})

it('ssr async page modules', async () => {
const $ = await next.render$('/')
expect($('#app-value').text()).toBe('hello')
expect($('#page-value').text()).toBe('42')
})

it('csr async page modules', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('#app-value').text()).toBe('hello')
expect(await browser.elementByCss('#page-value').text()).toBe('42')
expect(await browser.elementByCss('#doc-value').text()).toBe('doc value')
})

it('works on async api routes', async () => {
const res = await next.fetch('/api/hello')
expect(res.status).toBe(200)
const result = await res.json()
expect(result).toHaveProperty('value', 42)
})

it('works with getServerSideProps', async () => {
const browser = await next.browser('/gssp')
expect(await browser.elementByCss('#gssp-value').text()).toBe('42')
})

it('works with getStaticProps', async () => {
const browser = await next.browser('/gsp')
expect(await browser.elementByCss('#gsp-value').text()).toBe('42')
})

it('can render async 404 pages', async () => {
const browser = await next.browser('/dhiuhefoiahjeoij')
expect(await browser.elementByCss('#content-404').text()).toBe("hi y'all")
})

// TODO: investigate this test flaking
it.skip('can render async AMP pages', async () => {
const browser = await next.browser('/config')
await check(
() => browser.elementByCss('#amp-timeago').text(),
'just now',
true
)
})
;(dev ? it.skip : it)('can render async error page', async () => {
const browser = await next.browser('/make-error')
expect(await browser.elementByCss('#content-error').text()).toBe(
'hello error'
)
})
})
7 changes: 7 additions & 0 deletions test/e2e/async-modules/next.config.js
@@ -0,0 +1,7 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('./amp-validator-wasm.js'),
},
},
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/e2e/tsconfig-module-preserve/index.test.ts
Expand Up @@ -37,6 +37,7 @@ describe('tsconfig module: preserve', () => {
"{
"compilerOptions": {
"module": "preserve",
"target": "ES2017",
"lib": [
"dom",
"dom.iterable",
Expand Down
12 changes: 0 additions & 12 deletions test/integration/async-modules/next.config.js

This file was deleted.

136 changes: 0 additions & 136 deletions test/integration/async-modules/test/index.test.js

This file was deleted.

0 comments on commit 1fd93ee

Please sign in to comment.