This project is bootstrapped with Create React App - PWA addressing common needs such as,
- A routing library
- An .env handler for handling environment
- SASS and tailwind for styling
- Dark mode enabled if needed
- Pre-built commonly needed components
- Auth-restricted pages/components
- A well-formed file structure
- Ability to turn it to a PWA
and addressing common workarounds for well known issues like persistence over refreshes to minimise the setup time almost to none.
npx create-react-app <project-path> --template cra-template-orange
- Download or Clone the repository.
- Run,
npx create-react-app <project-path> --template file:path/to/your/template/cra-template-orange
- Template files can be found in
template
folder to add/edit/remove. - To add/edit/remove packages edit the
template.json
file.
There are several readily available libraries, components and functions out of the box. Default scripts are also changed as shown below. In addition, several ready-made components are available to use as is or to customise.
{
"dependencies" : {
"react-router-dom": "^6.3.0"
},
"devDependencies": {
"autoprefixer": ">=10",
"dotenv-cli": ">=6",
"sass": ">=1",
"tailwindcss": ">=3"
},
"scripts": {
"build:test": "dotenv -e .env.dev react-scripts build && rm -rf build-test && cp -r build build-test",
"build:prod": "dotenv -e .env.production react-scripts build",
"build": "echo \"Please use build:dev or build:prod \" && exit 1"
}
}
Featured files only,
├── public
│ ├── .htaccess # This solves the refreshing problem for routes (Described in a later section)
│ └── index.html
├── src
│ ├── App.js # Acts as the router
│ ├── components
│ │ ├── common
│ │ │ ├── index.js # Indexes the sibling components so it will be able to import all in one line)
│ │ │ ├── Modal.js
│ │ │ └── ThemeToggle.js
│ │ ├── pages
│ │ │ ├── home
│ │ │ │ └── Home.js
│ │ │ ├── index.js
│ │ │ ├── login
│ │ │ │ ├── LoginForm.js
│ │ │ │ └── Login.js
│ │ │ └── NotFound.js # The 404 file
│ │ └── template
│ │ │ ├── Footer.js
│ │ │ ├── Header.js
│ │ │ └── index.js
│ ├── contexts
│ │ ├── index.js
│ │ ├── SiteSettingsContext.js
│ │ └── ThemeContext.js
│ ├── index.css
│ ├── index.js # The main index file where the #root resides
│ └── use-cases
│ ├── cache
│ │ └── get-cached-fetch.js
│ ├── create-markup.js
│ ├── index.js
│ ├── is-logged-in.js
│ ├── iso-local-date.js
│ ├── private-route.js
│ ├── relative-day.js
│ └── use-auth.js
└── tailwind.config.js
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://cra.link/PWA
serviceWorkerRegistration.unregister();
As the comment states, switching serviceWorker.unregister()
to serviceWorker.register()
will opt you in to using the service worker.
You can totally remove Google Workbox libraries from the package.json
if you don't desire this functionality.
The app.js
is working solely as a router. Apart from a several commonly used components in the Common
and Pages
folders under src
, there are some helpers provided to beat commonly rising issues with ReactJS.
PrivateRoute.js
will provide a solution for authentication while routing. You will have to save authentication details somewhere convenient to you and retrieve them on useAuth()
instead of retrieving from localStorage
if you prefer. You can find it under Helpers.js
.
ThemeContext
is included to function as the dark mode switcher. You can use ThemeToggle
component to toggle the theme. Remember to add ThemeProvider
to App.js
and set darkMode: 'class'
in tailwind.config.js
In order to make sure the App will be routed correctly when refreshed, an .htaccess is introduced to redirect the requests to index.html. This solution will only work with Apache servers. For other solutions read, this (StackOverflow) or this (React Docs).
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]