vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.49k stars 26.8k forks source link

[Bug] Issue with middleware run on BunJS docker container #56683

Closed HiIamLala closed 12 months ago

HiIamLala commented 12 months ago

Link to the code that reproduces this issue

https://gitlab.com/hiiamlala/bun-docker-nextjs-issue

To Reproduce

  1. Run docker build image from root path of repository.
    docker build --no-cache -t 'bun-test' .
  2. Start docker container from built image above.
    docker run -p 3000:3000 'bun-test'
  3. Access to localhost:3000

Current vs. Expected behavior

Just a simply app router NextJS with i18n and middleware follow this instruction from official document. After start the docker command, the container is started successfully.

❯ docker run -p 3000:3000 'bun-test'
$ next start
  ▲ Next.js 13.5.4
  - Local:        http://localhost:3000

 ✓ Ready in 1317ms

But when I try to access it with any web browser I got this error. In docker logs:

error: Attempt to export a nullable value for "TextDecoderStream"
      at /app/node_modules/next/dist/compiled/@next/react-dev-overlay/dist/middleware.js:1:99205

On browser image

Verify canary release

Provide environment information

Operating System:
  Platform: Ubuntu (WSL2)
  Arch: x64
  Version: Ubuntu (22.04.3 LTS) / Windows 11 (10.0.22621)
Binaries:
  Bun: v1.0.3 (25e69c71)
  Docker: v24.0.6, build ed223bc
Relevant Packages:
  next: 13.5.4
  eslint-config-next: 13.5.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: not set (also tried with standalone)

Which area(s) are affected? (Select all that apply)

Middleware / Edge (API routes, runtime)

Additional context

Another problem is if I don't set the NODE_ENV to production in Dockerfile then build script will be failed.

❯ docker build --no-cache -t 'bun-test' .
[+] Building 89.3s (9/9) FINISHED                                                                        docker:default
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 370B                                                                                  0.0s
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 474B                                                                               0.0s
 => [internal] load metadata for docker.io/oven/bun:alpine                                                         0.8s
 => [1/5] FROM docker.io/oven/bun:alpine@sha256:26ceedecb06836a118275a0ef3d8369aafe111020929cec762b02006ddf840de   0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 5.00kB                                                                                0.0s
 => CACHED [2/5] WORKDIR /app                                                                                      0.0s
 => [3/5] COPY . .                                                                                                 0.1s
 => [4/5] RUN bun install                                                                                         43.5s
 => ERROR [5/5] RUN bun run build                                                                                 44.7s
------
 > [5/5] RUN bun run build:
0.892 $ next build
0.973  ⚠ You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env
3.362 Attention: Next.js now collects completely anonymous telemetry regarding usage.
3.362 This information is used to shape Next.js' roadmap and prioritize features.
3.362 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
3.362 https://nextjs.org/telemetry
3.362
3.785    Creating an optimized production build ...
18.12  ✓ Compiled successfully
18.12    Linting and checking validity of types ...
25.04    Collecting page data ...
26.52    Generating static pages (0/3) ...
*** Removed due to long text code ***
                                               ^
44.50 error: <Html> should not be imported outside of pages/_document.
44.50 Read more: https://nextjs.org/docs/messages/no-document-import-in-page
44.50       at ei (/app/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:38:4357)
44.50       at Html (/app/.next/server/chunks/241.js:6:1303)
44.50       at renderWithHooks (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:15)
44.50       at renderIndeterminateComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:14)
44.50       at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:6)
44.50       at renderNodeDestructiveImpl (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:10)
44.50       at renderNodeDestructive (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:13)
44.50       at finishClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5688:2)
44.50       at renderClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:2)
44.50       at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:6)
44.50
44.50
44.50 Error occurred prerendering page "/404". Read more: https://nextjs.org/docs/messages/prerender-error
44.50 Error: <Html> should not be imported outside of pages/_document.
44.50 Read more: https://nextjs.org/docs/messages/no-document-import-in-page
44.50     at ei (/app/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:37:4503)
44.50     at Html (/app/.next/server/chunks/241.js:5:1361)
44.50     at renderWithHooks (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:21)
44.50     at renderIndeterminateComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5736:25)
44.50     at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:6)
44.50     at renderNodeDestructiveImpl (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:10)44.50     at renderNodeDestructive (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:6)
44.50     at finishClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5687:51)
44.50     at renderClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:29)
44.50     at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:6)
44.50
*** Removed due to long text code ***
                                               ^
44.50 error: <Html> should not be imported outside of pages/_document.
44.50 Read more: https://nextjs.org/docs/messages/no-document-import-in-page
44.50       at ei (/app/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:38:4357)
44.50       at Html (/app/.next/server/chunks/241.js:6:1303)
44.50       at renderWithHooks (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:15)
44.50       at renderIndeterminateComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:14)
44.50       at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:6)
44.50       at renderNodeDestructiveImpl (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:10)
44.50       at renderNodeDestructive (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:13)
44.50       at finishClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5688:2)
44.50       at renderClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:2)
44.50       at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:6)
44.50
44.50
44.50 Error occurred prerendering page "/500". Read more: https://nextjs.org/docs/messages/prerender-error
44.50 Error: <Html> should not be imported outside of pages/_document.
44.50 Read more: https://nextjs.org/docs/messages/no-document-import-in-page
44.50     at ei (/app/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:37:4503)
44.50     at Html (/app/.next/server/chunks/241.js:5:1361)
44.50     at renderWithHooks (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:21)
44.50     at renderIndeterminateComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5736:25)
44.50     at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:6)
44.50     at renderNodeDestructiveImpl (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:10)44.50     at renderNodeDestructive (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:6)
44.50     at finishClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5687:51)
44.50     at renderClassComponent (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:29)
44.50     at renderElement (/app/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:6)
44.50  ⨯ TypeError: null is not an object (evaluating 'N.current.useContext')
44.50     at usePathname (/app/.next/server/chunks/885.js:0:21818)
44.50     at ErrorBoundary (/app/.next/server/chunks/885.js:0:12290)
44.50 70 |  *
44.50 71 |  * Copyright (c) Meta Platforms, Inc. and affiliates.
44.50 72 |  *
44.50 73 |  * This source code is licensed under the MIT license found in the
44.50 74 |  * LICENSE file in the root directory of this source tree.
44.50
*** Removed due to long text code ***
        ^
44.50 TypeError: null is not an object (evaluating 'N.current.useContext')
44.50       at /app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:75:6838
44.50       at usePathname (/app/.next/server/chunks/885.js:1:21782)
44.50       at ErrorBoundary (/app/.next/server/chunks/885.js:1:12202)
44.50       at oZ (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:71092)
44.50       at /app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:75871
44.50       at at (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:77623)
44.50       at e (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:109575)
44.50       at an (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:111551)
44.50       at o7 (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:72510)
44.50       at /app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:77601
44.50
44.50
44.50 Error occurred prerendering page "/_not-found". Read more: https://nextjs.org/docs/messages/prerender-error
44.50 TypeError: null is not an object (evaluating 'N.current.useContext')
44.50     at <anonymous> (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:74:6865)
44.50     at usePathname (/app/.next/server/chunks/885.js:0:21818)
44.50     at ErrorBoundary (/app/.next/server/chunks/885.js:0:12290)
44.50     at oZ (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:10:71099)
44.50     at <anonymous> (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:10:75928)
44.50     at at (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:10:77639)
44.50     at e (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:10:109593)
44.50     at an (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:10:111563)
44.50     at o7 (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:10:72538)
44.50     at <anonymous> (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:10:77623)
 ✓ Generating static pages (3/3)
44.54
44.54 > Export encountered errors on following paths:
44.54   /_error: /404
44.54   /_error: /500
44.54   /_not-found
44.62 error: script "build" exited with code 1 (SIGHUP)
------
Dockerfile:16
--------------------
  14 |
  15 |     # Build
  16 | >>> RUN bun run build
  17 |
  18 |     # Expose port 3000
--------------------
ERROR: failed to solve: process "/bin/sh -c bun run build" did not complete successfully: exit code: 1
github-actions[bot] commented 12 months ago

We could not detect a valid reproduction link. Make sure to follow the bug report template carefully.

Why was this issue closed?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We need a link to a public GitHub repository (template for App Router, template for Pages Router), but you can also use these templates: CodeSandbox: App Router or CodeSandbox: Pages Router.

The bug template that you filled out has a section called "Link to the code that reproduces this issue", which is where you should provide the link to the reproduction.

What should I do?

Depending on the reason the issue was closed, you can do the following:

In general, assume that we should not go through a lengthy onboarding process at your company code only to be able to verify an issue.

My repository is private and cannot make it public

In most cases, a private repo will not be a sufficient minimal reproduction, as this codebase might contain a lot of unrelated parts that would make our investigation take longer. Please do not make it public. Instead, create a new repository using the templates above, adding the relevant code to reproduce the issue. Common things to look out for:

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps by opening a new issue.

I think my reproduction is good enough, why aren't you looking into it quickly?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources