vercel / vercel/next.js

dynamic assetPrefix not working in "standalone" build, because of serialization

Open
#65,807 5 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Output Runtime
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/lionskape/next-standalone-assetprefix-bugrepot/blob/main/next.config.js

To Reproduce
  1. create next config like this:
let nextConfig = {
    output: 'standalone',
    assetPrefix: process.env.CDN_URI ?? '',
};
  1. run next build without CDN_URI variable
  2. run nextjs server CDN_URI="https://cdn.net" node .next/standalone/server.js
Current vs. Expected behavior

Current behavior:
No prefix will be added to resources because of config serialization - https://github.com/vercel/next.js/blob/81d5daddf365943e3f2c3a258d00c8ac76f9c81c/packages/next/src/build/utils.ts#L2116

Expected behavior:
dynamic assetPrefix works fine, environment variable is consumed.

Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: N/A
  pnpm: 7.32.1
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: standalone
Which area(s) are affected? (Select all that apply)

Output (export/standalone), Runtime

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

next build (local)

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the standalone serialization logic in packages/next/src/build/utils.ts around the linked line, then reproduce the issue using the provided next.config.js and the standalone server command. Verify the fix by confirming that CDN_URI set at runtime adds the prefix to generated resources after building without it.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js, node.js
Domain
backend, build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.