Blank page when deploying a react app to github pages and vite
Asked Answered
U

8

5

When i try to deploy my react app to github pages with the package gh-pages, the result page is blank.result page

The page I am trying to deploy is here: LINK I don't know if it matters but I am currently using the free domain given to me by GitHub: www.elvas.me

I tried following the react official docs: Link, but it didn't work for me... Perhaps it's because I am using vite and not create-react-app?

*Edit*

Found out that the site is trying to get the .js and the .css from the wrong place. enter image description here

I just don't know what I am doing wrong...

Unhitch answered 21/11, 2022 at 12:24 Comment(9)
Have you set the "/" route ?Farcy
How do I do that? In the index.html?Unhitch
I mean in the app.js, where you define your routes, is the "/" (the home route) defined ?Farcy
I don't think it is... But I am not using react-router, do I need it?Unhitch
If you only have one route, no. But if you want to develop many routes in the future, I recommend you to use react-router.Farcy
For this specific project, i do not need more than one route :)Unhitch
Did you do npm run build? It looks like your files aren't the build files in the github repoStratovision
I did! I am using yarn, so yarn run build. The files don't look the same because I was testing stuff, but now it's all updated. Still not working tho.Unhitch
when i do yarn deploy, it will automatically do the yarn run build right?Unhitch
U
11

Ok, so to solve this the only thing that I had to do was to add base:"{repName}" to the vite.config.ts file.

Source: https://vitejs.dev/guide/static-deploy.html

The images were not loading, I used this to fix them: Github pages vite JS build not showing the images

Unhitch answered 22/11, 2022 at 23:0 Comment(0)
C
11

I see that you have managed to deploy your React project to Github pages successfully, but here is how I did it in case anyone needs help:

  • First things first, make sure that your ".git" folder and your project are in the same folder.

enter image description here

  1. Run npm run build. You should have a dist folder now.
  2. Open the file vite.config.js (or .ts).
  3. Add the base file with your repository name. Include the two /.

Example: let's say your github project's URL is https://github.com/atlassian/react-beautiful-dnd.

export default defineConfig({
  base: "/react-beautiful-dnd/",
  plugins: [react()],
});
  1. Open your .gitignore file and delete the dist line from it. You want to make sure that the dist folder is pushed to github.
  2. git add .
  3. git commit -m "deploy"
  4. git subtree push --prefix dist origin gh-pages
  5. Wait for a couple minutes (in my case it took 4 minutes) and open the page. In the example above, the URL would look like this: https://atlassian.github.io/react-beautiful-dnd

In case it's still showing a blank page, it's very likely to do with the step number 3. Ensure you added the correct repository URL and that it begins and ends with the / sign.

That is about it, I hope it helps. I used this blog post for guidance, it is a more detailed explanation of the above.

Chelyuskin answered 27/12, 2022 at 5:48 Comment(1)
Thanks for this. I didn't see these instructions anywhere in the github docs.Jube
S
4

Make sure that

  1. vite.config.js contains base set to your repository name.
    For https://<USERNAME>.github.io/<REPO> your vite.config.js file should be something like this.
/*
...
*/
export default defineConfig({
    plugins: [react()],
    base: "/<REPO>/",
});
  1. If you are using react route then set basename as your <REPO> name for <BrowserRoute> component.
    Your main.jsx file should be something like this.
/*
...
*/

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <BrowserRouter basename={"/<REPO>/"}>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

I had issue deploying on github pages as I didn't set the basename property.

For more details check here

Hope this will find helpful to someone.

Sumach answered 5/7, 2023 at 17:50 Comment(0)
B
1

To anyone else encountering this problem in the future and finding this thread: Check your file names very carefully for your vite.config.ts and such. I'd accidentally misnamed vite.config.ts as vite.config.js (which one you use MUST match tsconfig.node.json). Everything worked just fine in preview until I tried to deploy, and suddenly, white screen. Took me a couple hours to figure out what was going wrong.

Also, if you're using a custom domain, the proper base is "/" which is also the default (nothing).

And finally, there's a lot of blog posts giving you suggestions about what github action to use. When the one from the docs didn't work, I tried using others that were recommended in such guides. But when I fixed the underlying problem, my deploy still didn't work; it was only when returning to the suggested config from the actual documentation that my deploy was successful.

There's a lot to mess up on, hopefully as vite matures, there are more correct answers and (relatively) fewer wrong ones, and I hope this helps someone!

Belgrade answered 7/3, 2023 at 8:17 Comment(0)
G
0

I beat my head against a wall with this issue for many hours.

I had correctly set the base in my config file, but it wasn't being prepended to the 'assets' folder path during the vite build of dist/index.html. Check your dist/index.html after build to make sure base is being prepended correctly.

It took me a long time to figure out why the prepend didn't work. It turned out to be because the vanilla Vite React template I used to initialize the project had placed my vite.config.js file in the src subdirectory, whereas it should have been in the root directory. After moving it to the root, vite build worked correctly.

Garbe answered 17/5, 2023 at 15:56 Comment(0)
G
0

Make sure those things

  1. the dist folder is not in the .gitignore file
  2. in the vite.config.js file the base name should be repository name
  3. all route bases should be start with the repository name
Galegalea answered 18/10, 2023 at 5:51 Comment(1)
I think is a good practice to put dist in gitignore, because besides being necessary data in the repository, it can create conflicts when you are working with a team.Junker
C
0

On the top of everything that has been pointed out, another possible error (which I did):

When configuring Github Actions on the repository, the default content shown in the workflow static.yml looked awfully similar to that suggested in the vite documentation page. So I did not bother to change the default suggestion and committed it.

The culprit in the default suggestion was :

with:
          # Upload entire folder
          path: '.'

Vite documentation rightfully pointed out which I ignored:

with:
          # Upload dist folder
          path: './dist'

This final change had my project working!

Connieconniption answered 12/3 at 20:13 Comment(0)
U
0

if any one that comes here using the react-router-dom then , you would have to add this into your App.tsx

import SignIn from './pages/Signin'

function App() {
return (
  <BrowserRouter basename="/your-repositorie-name">
    <Routes>
      <Route path="/" element={<SignIn />} />
    </Routes>
  </BrowserRouter>
)
}

export default App 
Unwinking answered 19/9 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.