Skip to content

[NEXT-1236] next@13.4.3 next/config return undefined with custom server #50315

@yceffort

Description

@yceffort

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.10
      pnpm: 8.5.1
    Relevant packages:
      next: 13.4.4-canary.9
      eslint-config-next: 13.4.3
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

https://github.com/yceffort/next-config-bug

To Reproduce

npm i && npm run build && npm run start
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
    serverRuntimeConfig: {
        'hello': 'world'
    },
    publicRuntimeConfig: {
        'foo': 'bar'
    }
}

module.exports = nextConfig
// server.ts
import next from 'next'
import Koa from 'koa'
import mount from 'koa-mount'
import getConfig from "next/config";
async function main() {
    const nextApp = next({dev: false})
    await nextApp.prepare()
    const nextAppHandler = nextApp.getRequestHandler()

    const app = new Koa()

    const config = getConfig()

    console.log("config >>>>>>>>>>>", config)  // undefined

    app.use(
        mount('/', (ctx) => {
            ctx.respond = false
            nextAppHandler(ctx.req, ctx.res)
        }),
    )

    app.listen(3000)
}

main()

Describe the Bug

In version 13.4.3 of next, when running with a custom server, next/config returns undefined. This was fine in previous versions.

»  npm i && npm run build && npm run start

changed 3 packages, and audited 355 packages in 4s

126 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> config@0.1.0 build
> tsc -p tsconfig.server.json && next build --debug

- info Creating an optimized production build  
- info Compiled successfully
- info Linting and checking validity of types  
- info Collecting page data  
- info Generating static pages (4/4)
- info Finalizing page optimization  

Route (app)                                Size     First Load JS
┌ ○ /                                      4.95 kB         127 kB
└ ○ /favicon.ico                           0 B                0 B
+ First Load JS shared by all              122 kB
  ├ chunks/2443530c-d4a6bd7fc7f21e3c.js    50.5 kB
  ├ chunks/488-441ec15fc214c3f1.js         24.7 kB
  ├ chunks/framework-6f53985fc108c1c2.js   45.1 kB
  ├ chunks/main-app-2bfe5c03c0626a2e.js    220 B
  └ chunks/webpack-a71e0afe4755ac1d.js     1.68 kB

Route (pages)                              Size     First Load JS
─ ○ /404                                   181 B          74.8 kB
+ First Load JS shared by all              74.6 kB
  ├ chunks/framework-6f53985fc108c1c2.js   45.1 kB
  ├ chunks/main-ee95a2f67a80a342.js        27.6 kB
  ├ chunks/pages/_app-b555d5e1eab47959.js  195 B
  └ chunks/webpack-a71e0afe4755ac1d.js     1.68 kB

○  (Static)  automatically rendered as static HTML (uses no initial props)

Redirects

┌ source: /:path+/
├ destination: /:path+
└ permanent: true
 


> config@0.1.0 start
> node dist/server.js

# unexpected behaviour
config >>>>>>>>>>> undefined

Expected Behavior

It should return serverRuntimeConfig and publicRuntimeConfig.

# 13.4.2
~/private/config  main ✗                                                                                                                                                                                                                                                                                                                                                                                                                                                   130 ⚠️
»  npm i && npm run build && npm run start

changed 3 packages, and audited 355 packages in 5s

126 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> config@0.1.0 build
> tsc -p tsconfig.server.json && next build --debug

- info Creating an optimized production build  
- info Compiled successfully
- info Linting and checking validity of types  
- info Collecting page data  
- info Generating static pages (4/4)
- info Finalizing page optimization  

Route (app)                                Size     First Load JS
┌ ○ /                                      4.93 kB        81.9 kB
└ ○ /favicon.ico                           0 B                0 B
+ First Load JS shared by all              76.9 kB
  ├ chunks/139-bbae89bf9cd2e4a9.js         24.5 kB
  ├ chunks/2443530c-d4a6bd7fc7f21e3c.js    50.5 kB
  ├ chunks/main-app-ca1f37152f50a304.js    215 B
  └ chunks/webpack-a71e0afe4755ac1d.js     1.68 kB

Route (pages)                              Size     First Load JS
─ ○ /404                                   178 B            86 kB
+ First Load JS shared by all              85.8 kB
  ├ chunks/main-ec7b1d67e0a9d0ca.js        83.9 kB
  ├ chunks/pages/_app-c544d6df833bfd4a.js  192 B
  └ chunks/webpack-a71e0afe4755ac1d.js     1.68 kB

○  (Static)  automatically rendered as static HTML (uses no initial props)

Redirects

┌ source: /:path+/
├ destination: /:path+
└ permanent: true
 


> config@0.1.0 start
> node dist/server.js

# works fine in 13.4.2
config >>>>>>>>>>> {
  serverRuntimeConfig: { hello: 'world' },
  publicRuntimeConfig: { foo: 'bar' }
}

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1236

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions