next.js: mismatch BUILD_ID in multi server
I have two server running a next program for load balancing, when I run npm run build in this two server, they generate two different BUILD_ID in their own .next dir. Then when I visit my site, the server would throw an error Build id mismatch! Seems like the server and the client version of files are not the same. do you know how to solve this problem?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 20 (14 by maintainers)
riffing off of @arunoda heres what we are using to get around this
As another approach to generate
buildId, we might be able to reusehashorchunkhashofwebpack. https://webpack.github.io/docs/long-term-caching.html@iaincollins This is a good research. Thanks. For the absolute path issue, I suggest to refer this tread: https://github.com/zeit/next.js/issues/198
For the buildId, sticky session is a good solution, but it’s not for scaling. So, we need to go for something else. We can go for a hash based on webpack or file content. But we need to do some good testing and research on that. So, I’d like to defer that.
But for the short term, I’d like to show you a way to override the
BUILD_ID. Add the following into your num script.And you can also get the BUILD_ID via env variables like this too:
Update to get this working with AWS EBS without Docker using the git hash and the new
generateBuildId.Use
.gitattributesthat runs ongit archiveto replacegenerateBuildIdwith the git hash.Updated working solution
.gitattributesfile:next.config.jsfile set:package.jsonto run build and start the serverCame across this issue as well, and just went with a Docker setup.
Wrote an article that describes how to deploy to Elastic Beanstalk with Docker that avoids mismatched
BUILD_IDs, and absolute path issues.I got this working with AWS EBS without Docker using the git hash. I had an issue with something like @rickysahu’s solution because
eb deployusesgit archivewhich doesn’t copy over the.git/folder.You can get around this by using
.gitattributesthat runs ongit archiveto replace build file contents with the git hash.My working solution
.gitattributesfile:build/in the root of your projectbuild/BUILD_IDfile:build/build-stats.jsonfile:package.jsonscripts to copy over the build files after a buildI can look into this tomorrow if nobody else is on it, also critical for us.
@iaincollins Thanks for the feedback and I glad my suggestion helped you. Anyway, I’d like to have a way to expose a user defined BUILD_ID via an option in the CLI.
If you have some free time, feel free to send a PR.
Thanks to #3873, you can now use the
next-build-idpackage within your next.config.js module to easily set your BUILD_ID to the latest git commit hash (instead of using the default uuid). See an example here.I made a patch that should allow a build id to be passed in through the environment.
https://github.com/zeit/next.js/pull/3873
@iaincollins may be there’s already something similar to BUILD_ID in the beanstalk env. So, you could use that too.
You meant to mention me ? 😄
yep, I meant
yarn.lockandnpm-shrinkwrap.json. I think we can just create hash from.nextdir (except.next/dist) since we serve only results of webpack compilations usingbuildId, as far as I read https://github.com/zeit/next.js/pull/745. Maybe it’s not so difficult ?Another problem of the current approach is that build artifacts would be invalidated even if you just updated custom server and served content was not changed at all, for example.
@n8agrin What do you mean by the lockfile of deps? yarn.lock? If we need to create consistent build ids, we need to hash all the code, static content, lockfiles and other stuff. That’s doable, but I don’t think that’s worthy. Build once, run many instances would be the best approach.
As an easy fix, we could provide a way to override the build id.