Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a route for production using React Router within Create React App

I'm using React Router inside Create React App to handle my apps navigation. Along with my pages, I also have a route used only for development to display styles (/styles). I'd like to exclude this route when the build command runs so it's removed in production.

I'm currently thinking of checking an environment variable but is there a better way?

like image 208
Padwah Avatar asked Nov 30 '25 06:11

Padwah


1 Answers

You can use the process variable for that.

If you wrap that router with process.env.NODE_ENV === 'development', this will become false during building and this route will be excluded, since process.env.NODE_ENV is production and static false statements will be removed.

{process.env.NODE_ENV === 'development' && <Route ... />}

You can read more about it here.

like image 52
Domino987 Avatar answered Dec 02 '25 05:12

Domino987



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!