<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Musse Berhane </title>
    <description>The latest articles on DEV Community by Musse Berhane  (@musse).</description>
    <link>https://dev.to/musse</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2823751%2Fc5a3eb22-a944-4023-92d2-65d01b926304.png</url>
      <title>DEV Community: Musse Berhane </title>
      <link>https://dev.to/musse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/musse"/>
    <language>en</language>
    <item>
      <title>Github pages</title>
      <dc:creator>Musse Berhane </dc:creator>
      <pubDate>Mon, 10 Feb 2025 10:43:27 +0000</pubDate>
      <link>https://dev.to/musse/github-pages-ocp</link>
      <guid>https://dev.to/musse/github-pages-ocp</guid>
      <description></description>
      <category>github</category>
    </item>
    <item>
      <title>How to deploy a React for both CRA &amp; Vite app to github pages</title>
      <dc:creator>Musse Berhane </dc:creator>
      <pubDate>Thu, 06 Feb 2025 09:45:59 +0000</pubDate>
      <link>https://dev.to/musse/how-to-deploy-a-react-for-both-cra-vite-app-to-github-pages-90e</link>
      <guid>https://dev.to/musse/how-to-deploy-a-react-for-both-cra-vite-app-to-github-pages-90e</guid>
      <description>&lt;p&gt;GitHub Pages is a free hosting service provided by GitHub that allows developers to publish static websites directly from their repositories. It’s widely used for personal projects, documentation, and portfolio sites. Since React apps, when built, generate static files (HTML, CSS, and JavaScript), GitHub Pages is an excellent option for hosting them without the need for a dedicated server.&lt;/p&gt;

&lt;p&gt;While both Create React App (CRA) and Vite can be deployed to GitHub Pages, the setup differs slightly because of the way they handle builds and static files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which One Should You Use?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're starting a new project, Vite is recommended because it's faster and more efficient.&lt;br&gt;
If you’re working with an existing Create React App, just follow the CRA steps below.&lt;br&gt;
Both methods get your React app live on GitHub Pages, so pick the one that best fits your workflow!&lt;/p&gt;

&lt;p&gt;So let's take a look at both steps.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Deploying Create React App (CRA) to GitHub Pages&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;CRA generates a single-page application (SPA) that requires additional configuration to work on GitHub Pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Differences for CRA&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Needs the gh-pages package.&lt;/li&gt;
&lt;li&gt;Requires a homepage field in package.json to avoid routing issues.&lt;/li&gt;
&lt;li&gt;Uses npm run build before deployment.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;First create a new repository in your github account and make sure your terminal is sync with your github account. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To sync your terminal with GitHub for easy pushing, you need to set up SSH authentication or use HTTPS with saved credentials. With SSH authentication, you no longer need to enter passwords when pushing to GitHub. Just git push, and your changes are live!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps for CRA Deployment&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Install gh-pages&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install gh-pages --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Add a homepage field in package.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"homepage": "https://your-username.github.io/repository-name"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Add deployment scripts:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"predeploy": "npm run build",
"deploy": "gh-pages -d build"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Deploy using&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run deploy

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Finally you can view your new published site using the link you provide in the package.json&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"homepage": ["https://your-username.github.io/repository-name"](https://your-username.github.io/repository-name)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ℹ️ Issue with CRA: Since GitHub Pages serves static files, it doesn't support direct client-side routing (e.g., React Router). You may need a .htaccess file or redirect workaround.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Deploying Vite React App to GitHub Pages&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Vite is faster and more optimized than CRA but requires slight modifications for GitHub Pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Differences for Vite&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses vite.config.js to set the base URL.&lt;/li&gt;
&lt;li&gt;Doesn't require gh-pages, but it's commonly used.&lt;/li&gt;
&lt;li&gt;Supports faster builds compared to CRA.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Steps for Vite Deployment&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Install gh-pages&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install gh-pages --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Update vite.config.js (if not created, make one):&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Unlike CRA, Vite needs base: '/repository-name/' in vite.config.js so GitHub Pages knows where to find the static files.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  base: '/repository-name/' // Set the base path for GitHub Pages
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Add deployment scripts in package.json&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"predeploy": "npm run build",
"deploy": "gh-pages -d dist"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Deploy using:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run deploy

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Finally you can view your new published site using the link you provide in the package.json&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"homepage": ["https://your-username.github.io/repository-name"](https://your-username.github.io/repository-name)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, When deploying a React app to GitHub Pages, there are key differences between Create React App (CRA) and Vite. CRA generates a /build folder, while Vite creates a /dist folder. In CRA, you must set a homepage field in package.json, whereas Vite requires configuring the base path in vite.config.js. Both setups commonly use the gh-pages package for deployment. However, CRA may face routing issues that require additional workarounds, while Vite handles this more smoothly with its base configuration. Additionally, Vite offers significantly faster build times due to its optimized ES module-based approach, making it a better choice for new projects.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
