Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor base server to remove native dependencies #33499

Merged
merged 7 commits into from Jan 20, 2022

Conversation

shuding
Copy link
Member

@shuding shuding commented Jan 20, 2022

Part of #31506, this PR removes loadEnvConfig and chalk from the base server while keeping the same behavior for the node server.

Bug

  • Related issues linked using fixes #number
  • Integration tests added
  • Errors have helpful link attached, see 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
  • Integration tests added
  • Documentation added
  • Telemetry added. In case of a feature if it's used or not.
  • Errors have helpful link attached, see contributing.md

Documentation / Examples

  • Make sure the linting passes by running yarn lint

@ijjk ijjk added created-by: Next.js team PRs by the Next.js team type: next labels Jan 20, 2022
@shuding shuding marked this pull request as ready for review January 20, 2022 15:33
@ijjk

This comment has been minimized.

@shuding shuding mentioned this pull request Jan 20, 2022
15 tasks
packages/next/lib/chalk.ts Outdated Show resolved Hide resolved
@ijjk
Copy link
Member

ijjk commented Jan 20, 2022

Failing test suites

Commit: 806cf3d

test/production/dependencies-can-use-env-vars-in-middlewares/index.test.ts

  • dependencies can use env vars in middlewares > uses the environment variables
Expand output

● dependencies can use env vars in middlewares › uses the environment variables

expect(received).toContain(expected) // indexOf

Expected substring: "{\"string\":\"a constant string\",\"hello\":\"env-var-used-in-middleware\",\"customPackage\":\"my-custom-package-env-var\"}"
Received string:    "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width\"/><meta charSet=\"utf-8\"/><title>500: Internal Server Error</title><meta name=\"next-head-count\" content=\"3\"/><noscript data-n-css=\"\"></noscript><script defer=\"\" nomodule=\"\" src=\"/_next/static/chunks/polyfills-5cd94c89d3acac5f.js\"></script><script src=\"/_next/static/chunks/webpack-7d0ef319438b7284.js\" defer=\"\"></script><script src=\"/_next/static/chunks/framework-2b9d04f27d98bba1.js\" defer=\"\"></script><script src=\"/_next/static/chunks/main-68a981eb26ad28de.js\" defer=\"\"></script><script src=\"/_next/static/chunks/pages/_app-ba0d1cdf43a37972.js\" defer=\"\"></script><script src=\"/_next/static/chunks/pages/_error-6679c1efb053c50e.js\" defer=\"\"></script><script src=\"/_next/static/wYdp0PPt1-mj9KCegPycp/_buildManifest.js\" defer=\"\"></script><script src=\"/_next/static/wYdp0PPt1-mj9KCegPycp/_ssgManifest.js\" defer=\"\"></script><script src=\"/_next/static/wYdp0PPt1-mj9KCegPycp/_middlewareManifest.js\" defer=\"\"></script></head><body><div id=\"__next\" data-reactroot=\"\"><div style=\"color:#000;background:#fff;font-family:-apple-system, BlinkMacSystemFont, Roboto, &quot;Segoe UI&quot;, &quot;Fira Sans&quot;, Avenir, &quot;Helvetica Neue&quot;, &quot;Lucida Grande&quot;, sans-serif;height:100vh;text-align:center;display:flex;flex-direction:column;align-items:center;justify-content:center\"><div><style>body { margin: 0 }</style><h1 style=\"display:inline-block;border-right:1px solid rgba(0, 0, 0,.3);margin:0;margin-right:20px;padding:10px 23px 10px 0;font-size:24px;font-weight:500;vertical-align:top\">500</h1><div style=\"display:inline-block;text-align:left;line-height:49px;height:49px;vertical-align:middle\"><h2 style=\"font-size:14px;font-weight:normal;line-height:inherit;margin:0;padding:0\">Internal Server Error<!-- -->.</h2></div></div></div></div><script id=\"__NEXT_DATA__\" type=\"application/json\">{\"props\":{\"pageProps\":{\"statusCode\":500}},\"page\":\"/_error\",\"query\":{},\"buildId\":\"wYdp0PPt1-mj9KCegPycp\",\"isFallback\":false,\"err\":{\"name\":\"Internal Server Error.\",\"message\":\"500 - Internal Server Error.\",\"statusCode\":500},\"gip\":true,\"scriptLoader\":[]}</script></body></html>"

  60 |     expect(envVars).toContain('MY_CUSTOM_PACKAGE_ENV_VAR')
  61 |   })
> 62 |
     | ^
  63 |   it('uses the environment variables', async () => {
  64 |     const html = await renderViaHTTP(next.url, '/api')
  65 |     expect(html).toContain(

  at Object.<anonymous> (production/dependencies-can-use-env-vars-in-middlewares/index.test.ts:62:22)

test/integration/middleware/hmr/test/index.test.js

  • HMR with middleware > works for pages when middleware is compiled
  • HMR with middleware > refreshes the page when middleware changes
Expand output

● HMR with middleware › works for pages when middleware is compiled

expect(received).toEqual(expected) // deep equality

Expected: true
Received: false

  33 |
  34 |   it('works for pages when middleware is compiled', async () => {
> 35 |     const browser = await webdriver(context.appPort, `/`)
     |                                                          ^
  36 |
  37 |     try {
  38 |       await browser.eval('window.itdidnotrefresh = "hello"')

  at Object.<anonymous> (integration/middleware/hmr/test/index.test.js:35:58)

● HMR with middleware › refreshes the page when middleware changes

expect(received).toEqual(expected) // deep equality

Expected: "AboutA"
Received: "Unhandled Runtime Error"

  43 |       expect(await browser.eval('window.itdidnotrefresh')).toBe('hello')
  44 |     } finally {
> 45 |       await browser.close()
     |                      ^
  46 |     }
  47 |   })
  48 |

  at Object.<anonymous> (integration/middleware/hmr/test/index.test.js:45:22)

test/integration/middleware/with-base-path/test/index.test.js

  • Middleware base tests > dev mode > should execute from absolute paths
  • Middleware base tests > production mode > should execute from absolute paths
  • Middleware base tests > production mode > should redirect via preflight middleware request
Expand output

● Middleware base tests › dev mode › should execute from absolute paths

expect(received).toBe(expected) // Object.is equality

Expected: "/root/redirect-with-basepath"
Received: "/redirect-with-basepath"

  38 |     runPreflightTests()
  39 |   })
> 40 | })
     |   ^
  41 |
  42 | function runTests() {
  43 |   it('should execute from absolute paths', async () => {

  at Object.<anonymous> (integration/middleware/with-base-path/test/index.test.js:40:68)

● Middleware base tests › production mode › should execute from absolute paths

expect(received).toBe(expected) // Object.is equality

Expected: "/root/redirect-with-basepath"
Received: "/redirect-with-basepath"

  38 |     runPreflightTests()
  39 |   })
> 40 | })
     |   ^
  41 |
  42 | function runTests() {
  43 |   it('should execute from absolute paths', async () => {

  at Object.<anonymous> (integration/middleware/with-base-path/test/index.test.js:40:68)

● Middleware base tests › production mode › should redirect via preflight middleware request

expect(received).toBe(expected) // Object.is equality

Expected: "/root/about"
Received: null

  54 |       context.appPort,
  55 |       '/root/redirect-with-basepath'
> 56 |     )
     |      ^
  57 |     const html = await res.text()
  58 |     const $ = cheerio.load(html)
  59 |     expect($('.title').text()).toBe('About Page')

  at Object.<anonymous> (integration/middleware/with-base-path/test/index.test.js:56:30)

@ijjk

This comment has been minimized.

}
return chalk
},
}) as typeof import('next/dist/compiled/chalk')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type assertion must be avoided here. We should use type narrowing

@ijjk

This comment has been minimized.

Copy link
Member

@huozhi huozhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice hack on chalk, we could probably add some test to it later.

@ijjk
Copy link
Member

ijjk commented Jan 20, 2022

Stats from current PR

Default Build
General Overall increase ⚠️
vercel/next.js canary shuding/next.js shu/d205 Change
buildDuration 17.9s 14.9s -3s
buildDurationCached 3.9s 4s ⚠️ +129ms
nodeModulesSize 355 MB 355 MB ⚠️ +2.82 kB
Page Load Tests Overall increase ✓
vercel/next.js canary shuding/next.js shu/d205 Change
/ failed reqs 0 0
/ total time (seconds) 3.49 3.557 ⚠️ +0.07
/ avg req/sec 716.33 702.93 ⚠️ -13.4
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.674 1.654 -0.02
/error-in-render avg req/sec 1493.84 1511.81 +17.97
Client Bundles (main, webpack, commons)
vercel/next.js canary shuding/next.js shu/d205 Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.2 kB 42.2 kB
main-HASH.js gzip 27.2 kB 27.2 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71 kB 71 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary shuding/next.js shu/d205 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary shuding/next.js shu/d205 Change
_app-HASH.js gzip 1.37 kB 1.37 kB
_error-HASH.js gzip 194 B 194 B
amp-HASH.js gzip 312 B 312 B
css-HASH.js gzip 326 B 326 B
dynamic-HASH.js gzip 2.37 kB 2.37 kB
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 919 B 919 B
image-HASH.js gzip 4.87 kB 4.87 kB
index-HASH.js gzip 263 B 263 B
link-HASH.js gzip 2.13 kB 2.13 kB
routerDirect..HASH.js gzip 321 B 321 B
script-HASH.js gzip 383 B 383 B
withRouter-HASH.js gzip 318 B 318 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.2 kB 14.2 kB
Client Build Manifests
vercel/next.js canary shuding/next.js shu/d205 Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes
vercel/next.js canary shuding/next.js shu/d205 Change
index.html gzip 531 B 531 B
link.html gzip 545 B 545 B
withRouter.html gzip 526 B 526 B
Overall change 1.6 kB 1.6 kB

Default Build with SWC (Decrease detected ✓)
General Overall increase ⚠️
vercel/next.js canary shuding/next.js shu/d205 Change
buildDuration 21.9s 18.9s -3s
buildDurationCached 4s 3.9s -74ms
nodeModulesSize 355 MB 355 MB ⚠️ +2.82 kB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary shuding/next.js shu/d205 Change
/ failed reqs 0 0
/ total time (seconds) 3.612 3.67 ⚠️ +0.06
/ avg req/sec 692.1 681.13 ⚠️ -10.97
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.77 1.808 ⚠️ +0.04
/error-in-render avg req/sec 1412.43 1382.71 ⚠️ -29.72
Client Bundles (main, webpack, commons)
vercel/next.js canary shuding/next.js shu/d205 Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.3 kB 42.3 kB
main-HASH.js gzip 27.3 kB 27.3 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.3 kB 71.3 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary shuding/next.js shu/d205 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary shuding/next.js shu/d205 Change
_app-HASH.js gzip 1.35 kB 1.35 kB
_error-HASH.js gzip 180 B 180 B
amp-HASH.js gzip 305 B 305 B
css-HASH.js gzip 321 B 321 B
dynamic-HASH.js gzip 2.36 kB 2.36 kB
head-HASH.js gzip 342 B 342 B
hooks-HASH.js gzip 911 B 911 B
image-HASH.js gzip 4.88 kB 4.88 kB
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.19 kB 2.19 kB
routerDirect..HASH.js gzip 314 B 314 B
script-HASH.js gzip 375 B 375 B
withRouter-HASH.js gzip 309 B 309 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.2 kB 14.2 kB
Client Build Manifests
vercel/next.js canary shuding/next.js shu/d205 Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes
vercel/next.js canary shuding/next.js shu/d205 Change
index.html gzip 532 B 532 B
link.html gzip 545 B 545 B
withRouter.html gzip 526 B 526 B
Overall change 1.6 kB 1.6 kB
Commit: d243ba1

@kodiakhq kodiakhq bot merged commit 1c1a4de into vercel:canary Jan 20, 2022
@shuding shuding deleted the shu/d205 branch January 21, 2022 00:19
natew pushed a commit to natew/next.js that referenced this pull request Feb 16, 2022
Part of vercel#31506, this PR removes `loadEnvConfig` and `chalk` from the base server while keeping the same behavior for the node server.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `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`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants