├── src ├── vite-env.d.ts ├── pages │ ├── Home.tsx │ └── Contact.tsx ├── App.css ├── main.tsx ├── App.tsx ├── index.css └── assets │ └── react.svg ├── vite.config.ts ├── tsconfig.node.json ├── .gitignore ├── .eslintrc.cjs ├── tsconfig.json ├── public ├── 404.html └── vite.svg ├── index.html ├── package.json ├── .github └── workflows │ └── deploy.yml └── readme.md /src/vite-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/pages/Home.tsx: -------------------------------------------------------------------------------- 1 | export function Home() { 2 | return

Home

; 3 | } 4 | -------------------------------------------------------------------------------- /src/pages/Contact.tsx: -------------------------------------------------------------------------------- 1 | export function Contact() { 2 | return

Contact

; 3 | } 4 | -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "vite"; 2 | import react from "@vitejs/plugin-react"; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [react()], 7 | base: "/vite-react-router/", 8 | }); 9 | -------------------------------------------------------------------------------- /tsconfig.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "composite": true, 4 | "skipLibCheck": true, 5 | "module": "ESNext", 6 | "moduleResolution": "bundler", 7 | "allowSyntheticDefaultImports": true 8 | }, 9 | "include": ["vite.config.ts"] 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? 25 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { browser: true, es2020: true }, 3 | extends: [ 4 | 'eslint:recommended', 5 | 'plugin:@typescript-eslint/recommended', 6 | 'plugin:react-hooks/recommended', 7 | ], 8 | parser: '@typescript-eslint/parser', 9 | parserOptions: { ecmaVersion: 'latest', sourceType: 'module' }, 10 | plugins: ['react-refresh'], 11 | rules: { 12 | 'react-refresh/only-export-components': 'warn', 13 | }, 14 | } 15 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2020", 4 | "useDefineForClassFields": true, 5 | "lib": ["ES2020", "DOM", "DOM.Iterable"], 6 | "module": "ESNext", 7 | "skipLibCheck": true, 8 | 9 | /* Bundler mode */ 10 | "moduleResolution": "bundler", 11 | "allowImportingTsExtensions": true, 12 | "resolveJsonModule": true, 13 | "isolatedModules": true, 14 | "noEmit": true, 15 | "jsx": "react-jsx", 16 | 17 | /* Linting */ 18 | "strict": true, 19 | "noUnusedLocals": true, 20 | "noUnusedParameters": true, 21 | "noFallthroughCasesInSwitch": true 22 | }, 23 | "include": ["src"], 24 | "references": [{ "path": "./tsconfig.node.json" }] 25 | } 26 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | #root { 2 | max-width: 1280px; 3 | margin: 0 auto; 4 | padding: 2rem; 5 | text-align: center; 6 | } 7 | 8 | .logo { 9 | height: 6em; 10 | padding: 1.5em; 11 | will-change: filter; 12 | transition: filter 300ms; 13 | } 14 | .logo:hover { 15 | filter: drop-shadow(0 0 2em #646cffaa); 16 | } 17 | .logo.react:hover { 18 | filter: drop-shadow(0 0 2em #61dafbaa); 19 | } 20 | 21 | @keyframes logo-spin { 22 | from { 23 | transform: rotate(0deg); 24 | } 25 | to { 26 | transform: rotate(360deg); 27 | } 28 | } 29 | 30 | @media (prefers-reduced-motion: no-preference) { 31 | a:nth-of-type(2) .logo { 32 | animation: logo-spin infinite 20s linear; 33 | } 34 | } 35 | 36 | .card { 37 | padding: 2em; 38 | } 39 | 40 | .read-the-docs { 41 | color: #888; 42 | } 43 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | React Router 6 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom/client"; 3 | import App from "./App.tsx"; 4 | import "./index.css"; 5 | 6 | import { RouterProvider, createBrowserRouter } from "react-router-dom"; 7 | import { Home } from "./pages/Home.tsx"; 8 | import { Contact } from "./pages/Contact.tsx"; 9 | 10 | const router = createBrowserRouter([ 11 | { 12 | path: "/vite-react-router/", 13 | element: , 14 | children: [ 15 | { 16 | path: "/vite-react-router/", 17 | element: , 18 | }, 19 | { 20 | path: "/vite-react-router/contact", 21 | element: , 22 | }, 23 | ], 24 | }, 25 | ]); 26 | 27 | ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( 28 | 29 | 30 | 31 | ); 32 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Vite + React + TS 8 | 9 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import reactLogo from "./assets/react.svg"; 2 | import viteLogo from "/vite.svg"; 3 | import "./App.css"; 4 | import { Link, Outlet } from "react-router-dom"; 5 | 6 | function App() { 7 | return ( 8 | <> 9 |
10 | 11 | Vite logo 12 | 13 | 14 | React logo 15 | 16 |
17 |

Vite + React

18 | 19 | 24 | 25 | 26 | 27 |

Click on the Vite and React logos to learn more

28 | 29 | ); 30 | } 31 | 32 | export default App; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vite-react-router", 3 | "homepage": "https://erickks.github.io/vite-react-router/", 4 | "private": true, 5 | "version": "0.0.0", 6 | "type": "module", 7 | "scripts": { 8 | "dev": "vite", 9 | "build": "tsc && vite build", 10 | "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0", 11 | "preview": "vite preview" 12 | }, 13 | "dependencies": { 14 | "react": "^18.2.0", 15 | "react-dom": "^18.2.0", 16 | "react-router-dom": "^6.14.0" 17 | }, 18 | "devDependencies": { 19 | "@types/react": "^18.0.37", 20 | "@types/react-dom": "^18.0.11", 21 | "@typescript-eslint/eslint-plugin": "^5.59.0", 22 | "@typescript-eslint/parser": "^5.59.0", 23 | "@vitejs/plugin-react": "^4.0.0", 24 | "eslint": "^8.38.0", 25 | "eslint-plugin-react-hooks": "^4.6.0", 26 | "eslint-plugin-react-refresh": "^0.3.4", 27 | "typescript": "^5.0.2", 28 | "vite": "^4.3.9" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | build: 10 | name: Build 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: Checkout repo 15 | uses: actions/checkout@v2 16 | 17 | - name: Setup Node 18 | uses: actions/setup-node@v1 19 | with: 20 | node-version: 16 21 | 22 | - name: Install dependencies 23 | uses: bahmutov/npm-install@v1 24 | 25 | - name: Build project 26 | run: npm run build 27 | 28 | - name: Upload production-ready build files 29 | uses: actions/upload-artifact@v2 30 | with: 31 | name: production-files 32 | path: ./dist 33 | 34 | deploy: 35 | name: Deploy 36 | needs: build 37 | runs-on: ubuntu-latest 38 | if: github.ref == 'refs/heads/main' 39 | 40 | steps: 41 | - name: Download artifact 42 | uses: actions/download-artifact@v2 43 | with: 44 | name: production-files 45 | path: ./dist 46 | 47 | - name: Deploy to GitHub Pages 48 | uses: peaceiris/actions-gh-pages@v3 49 | with: 50 | github_token: ${{ secrets.GITHUB_TOKEN }} 51 | publish_dir: ./dist 52 | -------------------------------------------------------------------------------- /public/vite.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | :root { 2 | font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; 3 | line-height: 1.5; 4 | font-weight: 400; 5 | 6 | color-scheme: light dark; 7 | color: rgba(255, 255, 255, 0.87); 8 | background-color: #242424; 9 | 10 | font-synthesis: none; 11 | text-rendering: optimizeLegibility; 12 | -webkit-font-smoothing: antialiased; 13 | -moz-osx-font-smoothing: grayscale; 14 | -webkit-text-size-adjust: 100%; 15 | } 16 | 17 | a { 18 | font-weight: 500; 19 | color: #646cff; 20 | text-decoration: inherit; 21 | } 22 | a:hover { 23 | color: #535bf2; 24 | } 25 | 26 | body { 27 | margin: 0; 28 | display: flex; 29 | place-items: center; 30 | min-width: 320px; 31 | min-height: 100vh; 32 | } 33 | 34 | h1 { 35 | font-size: 3.2em; 36 | line-height: 1.1; 37 | } 38 | 39 | button { 40 | border-radius: 8px; 41 | border: 1px solid transparent; 42 | padding: 0.6em 1.2em; 43 | font-size: 1em; 44 | font-weight: 500; 45 | font-family: inherit; 46 | background-color: #1a1a1a; 47 | cursor: pointer; 48 | transition: border-color 0.25s; 49 | } 50 | button:hover { 51 | border-color: #646cff; 52 | } 53 | button:focus, 54 | button:focus-visible { 55 | outline: 4px auto -webkit-focus-ring-color; 56 | } 57 | 58 | @media (prefers-color-scheme: light) { 59 | :root { 60 | color: #213547; 61 | background-color: #ffffff; 62 | } 63 | a:hover { 64 | color: #747bff; 65 | } 66 | button { 67 | background-color: #f9f9f9; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/assets/react.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |
2 |

⚜️ V I T E   D E P L O Y ⚜️

3 |
4 | 5 |
6 | youtube video 7 |
8 | 9 |
10 | 11 | ### Follow the steps below on how to deploy a vite react app with router: 12 | 13 | ## 💻 Deployment 14 | 15 | #### 01. Create a vite react app 16 | 17 | ```npm 18 | npm create vite@latest 19 | ``` 20 | 21 | #### 02. Set the base on _vite.config_ 22 | 23 | ```js 24 | base: "/[REPO_NAME]/"; 25 | ``` 26 | 27 | #### 03. Create ./github/workflows/deploy.yml and add the code bellow 28 | 29 | ```yml 30 | name: Deploy 31 | 32 | on: 33 | push: 34 | branches: 35 | - main 36 | 37 | jobs: 38 | build: 39 | name: Build 40 | runs-on: ubuntu-latest 41 | 42 | steps: 43 | - name: Checkout repo 44 | uses: actions/checkout@v2 45 | 46 | - name: Setup Node 47 | uses: actions/setup-node@v1 48 | with: 49 | node-version: 16 50 | 51 | - name: Install dependencies 52 | uses: bahmutov/npm-install@v1 53 | 54 | - name: Build project 55 | run: npm run build 56 | 57 | - name: Upload production-ready build files 58 | uses: actions/upload-artifact@v2 59 | with: 60 | name: production-files 61 | path: ./dist 62 | 63 | deploy: 64 | name: Deploy 65 | needs: build 66 | runs-on: ubuntu-latest 67 | if: github.ref == 'refs/heads/main' 68 | 69 | steps: 70 | - name: Download artifact 71 | uses: actions/download-artifact@v2 72 | with: 73 | name: production-files 74 | path: ./dist 75 | 76 | - name: Deploy to GitHub Pages 77 | uses: peaceiris/actions-gh-pages@v3 78 | with: 79 | github_token: ${{ secrets.GITHUB_TOKEN }} 80 | publish_dir: ./dist 81 | ``` 82 | 83 | #### 04. Create a new repository on GitHub and initialize GIT 84 | 85 | ```git 86 | git init 87 | git add . 88 | git commit -m "add: initial files" 89 | git branch -M main 90 | git remote add origin https://github.com/[USER]/[REPO_NAME] 91 | git push -u origin main 92 | ``` 93 | 94 | #### 05. Active workflow 95 | 96 | ```js 97 | Config -> Actions -> General -> Workflow permissions -> Read and Write permissions 98 | Actions -> failed deploy -> re-run-job failed jobs 99 | Config -> Pages -> gh-pages -> save 100 | ``` 101 | 102 | ## React Router 103 | 104 | #### 01. Install React Router 105 | 106 | ```npm 107 | npm i react-router-dom 108 | ``` 109 | 110 | #### 02. Create the pages 111 | 112 | ``` 113 | src 114 | └── pages 115 | ├── Home.tsx 116 | └── Contact.tsx 117 | ``` 118 | 119 | #### 03. Configuring the routing on _main.tsx_ 120 | 121 | ```js 122 | import { RouterProvider, createBrowserRouter } from "react-router-dom"; 123 | import { Home } from "./pages/Home.tsx"; 124 | import { Contact } from "./pages/Contact.tsx"; 125 | 126 | const router = createBrowserRouter([ 127 | { 128 | path: "/vite-react-router/", 129 | element: , 130 | children: [ 131 | { 132 | path: "/vite-react-router/", 133 | element: , 134 | }, 135 | { 136 | path: "/vite-react-router/contact", 137 | element: , 138 | }, 139 | ], 140 | }, 141 | ]); 142 | 143 | ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( 144 | 145 | 146 | 147 | ); 148 | ``` 149 | 150 | #### 04. Configuring the routing on _App.tsx_ 151 | 152 | ```js 153 | import { Link, Outlet } from "react-router-dom"; 154 | 155 | export default function App() { 156 | return ( 157 | <> 158 | [FIXED_CONTENT] 159 | 160 | 165 | 166 | 167 | 168 | [FIXED_CONTENT] 169 | 170 | ); 171 | } 172 | ``` 173 | 174 | #### 05. Specify the homepage in _package.json_ 175 | 176 | ```json 177 | { 178 | "homepage": "[PROJECT_URL]" 179 | } 180 | ``` 181 | 182 | #### 06. Create the _404.html_ file in the _public_ folder 183 | 184 | ```html 185 | 186 | 187 | 188 | 189 | React Router 190 | 209 | 210 | 211 | 212 | ``` 213 | 214 | #### 07. Add the script below inside the head tag in _index.html_ 215 | 216 | ```html 217 | 231 | ``` 232 | 233 | #### 08. Push 234 | 235 | ```git 236 | git add . 237 | git commit -m "feat: routing" 238 | git push 239 | ``` 240 | --------------------------------------------------------------------------------