Search code examples
reactjsherokucachingcreate-react-app

Reduce heroku slug size while whitelisting 'serve' and its dependencies


I have a React app (CRA) that exceeds heroku's maximum slug size soft limit (300MB), so I use this solution to reduce the slug size:

  1. heroku buildpacks:add https://github.com/opencounter/heroku-buildpack-post-build-clean.git
  2. Add the file .slug-post-clean at the project root:
src
public
node_modules

But, as the same time, I'm serving my app using serve, package.json:

...
"dependencies": {
...
  "serve": "^12.0.1",
...
}, 
...
"scripts" : {
  "start": "serve -s build",
}

Excluding node_modules altogether will also exclude serve, making heroku unable to serve my app.

How to reduce heroku slug size while whitelisting 'serve' and its dependencies ?

For now, I'm excluding some big independent packages (e.g. @mui, @testing-library, etc.), but that's a bit hacky.


Solution

  • After excluding node-modules/.cache, the slug size has been reduced down 90% (~400M). So the solution is quite simple:

    In .slug-post-clean, add:

    node-modules/.cache