Error 404 on Github Pages using React Router v6.6.1 and Vite

huangapple go评论70阅读模式
英文:

Error 404 on Github Pages using React Router v6.6.1 and Vite

问题

I'm trying to publish my first React app using Vite, React-Router-Dom-v6.6.1, and Github Pages, but for some reason the index.html file is not detected, and the "404 error" is being shown. However, this error is shown on a page added by me to deal with possible errors, and there is an option to return to the homepage; however, this homepage is generated from the reading of the index.html file and respective JavaScript. But at this moment, the browser is already able to interpret the index.html file, and everything behaves similarly to the development environment, without errors.

That is, for some reason, the browser does not identify the index.html file on its first load (I think), even though the path pointed in base: "/presentation/" is pointing to the right place.

Below are the files I find most relevant, the link to my Github Pages (where this issue is happening) as well as a link to a screen recording I made of this situation so you can better understand what I'm trying to explain.

Github Page in question:

Problem screen recording video:

VITE files

main.jsx

import React from "react";
import ReactDOM from "react-dom/client";
import {
  createBrowserRouter,
  RouterProvider,
  createRoutesFromElements,
  Route,
} from "react-router-dom";
import "./index.css";

// Redux Toolkit
import { Provider, useSelector } from "react-redux";
import store from "./reduxTlk/store";

/* existing imports */
import Root from "./routes/root";
import ErrorPage from "./error-page";
import Home from "./routes/home";
import WeatherStatus from "./routes/weatherStatus";
import { weatherLoader } from "./components/projects/ipma/TempTable";

const router = createBrowserRouter(
  createRoutesFromElements(
    <>
      <Route path="/" element={<Root />} errorElement={<ErrorPage />} />
      <Route index element={<Home />} />
      <Route
        path="WeatherStatus"
        element={<WeatherStatus />}
        loader={weatherLoader}
      />
    </>
  )
);

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <Provider store={store}>
      <RouterProvider router={router} />
    </Provider>
  </React.StrictMode>
);

package.json

{
  "name": "vite-project",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@emotion/react": "^11.10.5",
    "@emotion/styled": "^11.10.5",
    "@mui/icons-material": "^5.11.0",
    "@mui/material": "^5.11.7",
    "@reduxjs/toolkit": "^1.9.1",
    "dompurify": "^2.4.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-redux": "^8.0.5",
    "react-router-dom": "^6.6.1"
  },
  "homepage": "https://cristianolm.github.io/presentation/",
  "devDependencies": {
    "@types/react": "^18.0.24",
    "@types/react-dom": "^18.0.8",
    "@vitejs/plugin-react": "^2.2.0",
    "vite": "^3.2.3"
  }
}

vite.config.js

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
  base: "/presentation/",
  plugins: [react()],
});

dist/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/presentation/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cristiano Martins</title>
    <script type="module" crossorigin src="/presentation/assets/index.6cda9410.js"></script>
    <link rel="stylesheet" href="/presentation/assets/index.85f54fc4.css">
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

Firstly, I tried to build for production with the base line: "/presentation/," in vite.config.js, then I tried to do this same process without this line, and finally I added that line again, but I added it to the package.json the line homepage": "https://cristianolm.github.io/presentation/.

And of course, I tried to search for another solution on Google, but without success.

英文:

I'm trying to publish my first React app using Vite, React-Router-Dom-v6.6.1 and Github Pages but for some reason the index.html file is not detected and the "404 error" is being shown, however this error is shown on a page added by me to deal with possible errors and there is an option to return to the homepage ; however this homepage is generated from the reading of the index.html file and respective javascript. But at this moment the browser is already able to interpret the index.html file and everything behaves similarly to the development environment, without errors.
That is, for some reason the browser does not identify the index.html file on its first load (I think), even though the path pointed in base: &quot;/presentation/&quot; is pointing to the right place.

Below are the files I find most relevant, the link to my `Github Pages` (where this issue is happening) as well as a link to a screen recording I made of this situation so you can better understand what I'm trying to explain.

Github Page in question:

Problem screen recording video:

VITE files


main.jsx

import React from &quot;react&quot;;
import ReactDOM from &quot;react-dom/client&quot;;
import {
  createBrowserRouter,
  RouterProvider,
  createRoutesFromElements,
  Route,
} from &quot;react-router-dom&quot;;
import &quot;./index.css&quot;;

// Redux Toolkit
import { Provider, useSelector } from &quot;react-redux&quot;;
import store from &quot;./reduxTlk/store&quot;;

/* existing imports */
import Root from &quot;./routes/root&quot;;
import ErrorPage from &quot;./error-page&quot;;
import Home from &quot;./routes/home&quot;;
import WeatherStatus from &quot;./routes/weatherStatus&quot;;
import { weatherLoader } from &quot;./components/projects/ipma/TempTable&quot;;

const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;&gt;
      &lt;Route path=&quot;/&quot; element={&lt;Root /&gt;} errorElement={&lt;ErrorPage /&gt;}&gt;
        &lt;Route index element={&lt;Home /&gt;} /&gt;
        &lt;Route
          path=&quot;WeatherStatus&quot;
          element={&lt;WeatherStatus /&gt;}
          loader={weatherLoader}
        /&gt;
      &lt;/Route&gt;
    &lt;/&gt;
  )
);

ReactDOM.createRoot(document.getElementById(&quot;root&quot;)).render(
  &lt;React.StrictMode&gt;
    &lt;Provider store={store}&gt;
      &lt;RouterProvider router={router} /&gt;
    &lt;/Provider&gt;
  &lt;/React.StrictMode&gt;
);

package.json

{
  &quot;name&quot;: &quot;vite-project&quot;,
  &quot;private&quot;: true,
  &quot;version&quot;: &quot;0.0.0&quot;,
  &quot;type&quot;: &quot;module&quot;,
  &quot;scripts&quot;: {
    &quot;dev&quot;: &quot;vite&quot;,
    &quot;build&quot;: &quot;vite build&quot;,
    &quot;preview&quot;: &quot;vite preview&quot;
  },
  &quot;dependencies&quot;: {
    &quot;@emotion/react&quot;: &quot;^11.10.5&quot;,
    &quot;@emotion/styled&quot;: &quot;^11.10.5&quot;,
    &quot;@mui/icons-material&quot;: &quot;^5.11.0&quot;,
    &quot;@mui/material&quot;: &quot;^5.11.7&quot;,
    &quot;@reduxjs/toolkit&quot;: &quot;^1.9.1&quot;,
    &quot;dompurify&quot;: &quot;^2.4.1&quot;,
    &quot;react&quot;: &quot;^18.2.0&quot;,
    &quot;react-dom&quot;: &quot;^18.2.0&quot;,
    &quot;react-redux&quot;: &quot;^8.0.5&quot;,
    &quot;react-router-dom&quot;: &quot;^6.6.1&quot;
  },
  &quot;homepage&quot;: &quot;https://cristianolm.github.io/presentation/&quot;,
  &quot;devDependencies&quot;: {
    &quot;@types/react&quot;: &quot;^18.0.24&quot;,
    &quot;@types/react-dom&quot;: &quot;^18.0.8&quot;,
    &quot;@vitejs/plugin-react&quot;: &quot;^2.2.0&quot;,
    &quot;vite&quot;: &quot;^3.2.3&quot;
  }
}

vite.config.js

import { defineConfig } from &quot;vite&quot;;
import react from &quot;@vitejs/plugin-react&quot;;

// https://vitejs.dev/config/
export default defineConfig({
  base: &quot;/presentation/&quot;,
  plugins: [react()],
});

dist/index.html


&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;link rel=&quot;icon&quot; type=&quot;image/svg+xml&quot; href=&quot;/presentation/vite.svg&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;Cristiano Martins&lt;/title&gt;
    &lt;script type=&quot;module&quot; crossorigin src=&quot;/presentation/assets/index.6cda9410.js&quot;&gt;&lt;/script&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;/presentation/assets/index.85f54fc4.css&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
    
  &lt;/body&gt;
&lt;/html&gt;

Firstly, I tried to build for production with the base line: &quot;/presentation/, in vite.config.js, then I tried to do this same process without this line and finally I added that line again, but I added it to the package. json the line homepage&quot;: &quot;https://cristianolm.github.io/presentation/,.

And of course I tried to search for another solution on google, but without success.

答案1

得分: 1

I suspect you may only need to additionally specify/indicate that the router is running from a sub-directory, beyond what is already specified in the package.json and the vite.config files. You can specify a basename prop on the router that matches the directory the app is hosted from and all routes and links will work relative to this location.

basename

The basename of the app for situations where you can't deploy to the
root of the domain, but a sub directory.

createBrowserRouter(routes, {
  basename: "/app",
});

For the host: "https://cristianolm.github.io/presentation"

Example:

const router = createBrowserRouter(
  createRoutesFromElements(
    <Route
      path="/" // <-- "/presentation"
      element={<Root />}
      errorElement={<ErrorPage />}
    >
      <Route
        index // <-- "/presentation"
        element={<Home />}
      />
      <Route
        path="WeatherStatus" // <-- "/presentation/WeatherStatus"
        element={<WeatherStatus />}
        loader={weatherLoader}
      />
    </Route>
  ),
  { basename: "/presentation" }
);
英文:

I suspect you may only need to additionally specify/indicate that the router is running from a sub-directory, beyond what is already specified in the package.json and the vite.config files. You can specify a basename prop on the router that matches the directory the app is hosted from and all routes and links will work relative to this location.

> basename
>
> The basename of the app for situations where you can't deploy to the
> root of the domain, but a sub directory.
>
> createBrowserRouter(routes, {
> basename: "/app",
> });

For the host: &quot;https://cristianolm.github.io/presentation&quot;

Example:

const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;Route
      path=&quot;/&quot; // &lt;-- &quot;/presentation&quot;
      element={&lt;Root /&gt;}
      errorElement={&lt;ErrorPage /&gt;}
    &gt;
      &lt;Route
        index // &lt;-- &quot;/presentation&quot;
        element={&lt;Home /&gt;}
      /&gt;
      &lt;Route
        path=&quot;WeatherStatus&quot; // &lt;-- &quot;/presentation/WeatherStatus&quot;
        element={&lt;WeatherStatus /&gt;}
        loader={weatherLoader}
      /&gt;
    &lt;/Route&gt;
  ),
  { basename: &quot;/presentation&quot; }
);

答案2

得分: -2

这似乎与Vite无关,我认为你明白:

const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;&gt;
-     &lt;Route path=&quot;/&quot; element={&lt;Root /&gt;} errorElement={&lt;ErrorPage /&gt;}&gt;
+     &lt;Route path=&quot;/presentation&quot; element={&lt;Root /&gt;} errorElement={&lt;ErrorPage /&gt;}&gt;
        &lt;Route index element={&lt;Home /&gt;} /&gt;
        &lt;Route
          path=&quot;WeatherStatus&quot;
          element={&lt;WeatherStatus /&gt;}
          loader={weatherLoader}
        /&gt;
      &lt;/Route&gt;
    &lt;/&gt;
  )
);

看起来是这样的:
https://cristianolm.github.io/presentation/
实际路由将是 /presentation 而不是 /

英文:

this is not really vite related i think you understand:

const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;&gt;
-     &lt;Route path=&quot;/&quot; element={&lt;Root /&gt;} errorElement={&lt;ErrorPage /&gt;}&gt;
+     &lt;Route path=&quot;/presentation&quot; element={&lt;Root /&gt;} errorElement={&lt;ErrorPage /&gt;}&gt;
        &lt;Route index element={&lt;Home /&gt;} /&gt;
        &lt;Route
          path=&quot;WeatherStatus&quot;
          element={&lt;WeatherStatus /&gt;}
          loader={weatherLoader}
        /&gt;
      &lt;/Route&gt;
    &lt;/&gt;
  )
);

it looks like this:
https://cristianolm.github.io/presentation/
the actual route will be /presentation not /

huangapple
  • 本文由 发表于 2023年2月6日 16:26:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定