Search code examples
javascriptnext.jsvercelturborepo

How to modify turborepo build command to only rebuild specific Next.js application based on latest commit?


I have a question about using turborepo with Next.js. I have two applications in my repository's apps folder, called web and docs. I am deploying them separately application on Vercel, but I am facing an issue where whenever I commit something in the web repository, both applications are rebuilt. I want to modify the build command to only rebuild the application that has changed or its dependencies.

The command I am currently using for building the project on Vercel is:

cd ../.. && npx turbo run build --scope=web --no-deps --include-dependencies

How can I modify this command to only rebuild the relevant application and its dependencies based on the latest commit?


Solution

  • Go to the vercel.json file in your web app and add this

    "ignoreCommand": "npx turbo-ignore"
    

    Then insider your vercel project for the same app, go to the git menu and scroll down to find the section named Ignored Build Step. Add the ignore script npx turbo-ignore under Production Overrides and this one under Project Settings

    git diff --quiet HEAD^ HEAD ./ && git diff --quiet HEAD^ HEAD ../../package.json
    

    This will ensure that any other changes that are not part of your web app or its package.json will be ignored. Repeat the same process for your docs app as well.

    As your projects grow and you add some shared packages and you want the build to be triggered when something in those packages change then you can simply include their relative path in that command. For example, let's say I have shared package named ui. The command will look like this

    git diff --quiet HEAD^ HEAD ./ && git diff --quiet HEAD^ HEAD ../../packages/shared/ui && git diff --quiet HEAD^ HEAD ../../package.json
    

    I hope this was what you were looking for, let me know if you have any doubts and if you want to learn more about this then here is the vercel link for the guide.