Open BenjaminToby opened 9 months ago
You need to also add distDir: '.custom_dist'
to your next.config.js
This works for me
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
distDir: '.custom_dist',
}
module.exports = nextConfig
@Bahlaouane-Hamza thanks for the suggestion. But the bug still persists. The Next JS documentation provides the option for a conf
object which doesn't work in Next 13+, and this should be fixed.
In addition, there are scenarios where the next.config.js
file isn't sufficient. For example: in one of my projects I'm loading the config object from a JSON file: this JSON file is tweaked during every build to generate a new build directory(for versioning purposes), and the server.js
file picks up the new build directory automatically on every new build: of course it can't pick up the new directory in Next 13+ because the conf
variable is broken.
Here's what happens as far as I understand:
createServer
– which is what you get with require('next')
– instantiates NextCustomServer
. Its superclass NextServer
does save the passed config in this.options.conf
:
But does not pass it through to the router server. So it's not used eventually.
https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/server/next.ts#L274 https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/server/lib/start-server.ts#L49 https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/server/lib/router-server.ts#L63
The router server calls loadConfig
without passing a customConfig
/rawConfig
:
https://github.com/vercel/next.js/blob/c1f8d93/packages/next/src/server/lib/router-server.ts#L82
So loadConfig
always reads the next.config.js file:
So currently, the passed config is not used and next.config.js is currently only the source of truth.
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/next-js-custom-server-bug-6z8fhc
To Reproduce
npx create-next-app@latest
.cd
into the new directory and add a basicserver.js
file in the root directory with these content:const next = require("next");
const dev = process.env.NODE_ENV !== "production"; const hostname = "localhost"; const port = 3600;
const app = next({ dev, hostname, port, conf: { distDir: ".custom_dist", } });
const handle = app.getRequestHandler();
app.prepare() .then(() => { const server = createServer(async (req, res) => { try { const parsedUrl = parse(req.url || "", true); const { pathname, query } = parsedUrl;
Which area(s) are affected? (Select all that apply)
Not sure
Additional context
This bug appeared with Next 13. I'm currently running Next 12 on my apps to avoid this bug.