Description
Currently we can only handle monorepos if the Next site root directory (that contains next.config.js
) can be set as base
in the netlify.toml
or UI. This means that it needs a package.json that installs all dependencies. It either needs to be self-contained, or it needs to be in a yarn workspace. This is because of the way Netlify handles monorepos.
The workaround of prepending cd the/site/root
before the build command does not currently work for Next sites, because build plugins always run from the base directory, meaning the plugin can't find all the Next files. This also doesn't work for monorepos that need commands to be run from the root. This is a particular problem for Nx sites, as they have no way of running commands from the app directory.
When building a Next site, the build plugin needs to know two things:
- The directory that contains the
next.config.js
. Currently the plugin assumes this iscwd()
: i.e. thebase
. This is why thecd
workaround fails. - The
distDir
(i.e. the.next
directory). In most cases we can find this out from the config at 1. The exception is with Nx, which I'll cover more below.
Information that can be set by the user:
- The
base
dir, which is currently where we assumenext.config.js
is, and where we run the build command. - The publish dir. By default this is
out
, and is where we move the files fromdistDir
. - The
distDir
, which is set innext.config.js
. By default this is.next
, except with Nx, which rewrites it on the fly unless it has been changed by the user to something else, in which case it's left alone.
Possible solutions:
- If the user sets the publish dir, we might assume that the
next.config.js
is in its parent directory. From this we can find thedistDir
.- Pros: Doesn't require any config. Works for most setups.
- Cons: if the publish dir isn't a sibling of the Next config then this fails. It also fails if the
distDir
can't be found from thenext.config.js
. Both of the are the case by default with Nx, which changes distDir on the fly to a directory outside of the site root.
- Allow the user to manually set a Next root directory.
- Pros: Avoids the issue with next config not being a sibling of the publish dir.
- Cons: doesn't solve the Nx issue, as the
distDir
can't be found from the config.
- Allow the user to manually set a Next root and a distDir in the plugin config.
- Pros: Works for all monorepos.
- Cons: Needs two new config options. Can be confusing.
- ✅ Solution 1, but require that Nx users set the
distDir
to a specific value, which overrides the behaviour where it rewrites it to a parent directory that we can't work out.- Pros: Doesn't require any new config options. We can check the config to ensure that the user has set the value.
- Cons: Needs special handling for Nx. Forces user to change their site config.
- Solution 1, plus special case handling for Nx: try reading details of the workspace from the config.
- Pros: Doesn't need any special work by the user, or any config options.
- Cons: More work. May not be possible: needs more investigation of Nx APIs. May be fragile if Nx changes APIs.
Monorepo types
The following project types have been tested
- All node_modules in Next subdirectory
- All node_modules at top level, Next in subdirectory
- Nx
- Lerna with yarn workspace
- Lerna with npm
- yarn workspace (?)
- TypeScript project reference (?)