stats count
Thursday, July 7, 2022
Home Technology React Deploy Github Pages routes

React Deploy Github Pages routes


Fix github.io gh-pages routes react js BrowserRouter

To implement front-end deployment of your React application on Github Pages is very simple. Firstly use the command below in your project to install gh-pages:

npm install –save-dev gh-pages

Next we will make some configurations in our archive package.json.

Addition to archive entry, acima logo of the name of your project and url in the “homepage” of your repository in Github in the format of Github Pages. Example:

[{

  “homepage”: “http://user.github.io/repo”,

  “name”: “project name”,

  “version”: “0.1.0”,

…]

* Substitua “user” by its username and “repo” by its repository on Github.

But, in the same archive, in “Scripts” add “predeploy” and “deploy” scripts to their scripts.

[“scripts”: {

    “predeploy”: “npm run build”,

    “deploy”: “gh-pages -d build”,

    “start”: “react-scripts start”,

    “build”: “react-scripts build”,

    “test”: “react-scripts test”,

    “eject”: “react-scripts eject”

  },]

Agora basta rodar o command “npm run deploy” (sem aspas) em seu terminal. Follow the url “http://user.github.io/repo” to view your project online.

You can find out how to troubleshoot root (router) problems while displaying pages in error or error 404. To resolve this, add “basename = {process.env.PUBLIC_URL}” (without aspas) to your “BrowserRouter”. Example:

[return (

        <BrowserRouter basename={process.env.PUBLIC_URL}>

            <Routes>

                <Route element={<Home/>} path=”/” />

                <Route element={<SignUp />} path=”/signup” />]

Do not hesitate to use links in your page or link to correct them:

[<a href={process.env.PUBLIC_URL + ‘/signup’} className=”buttonSignup”>]

Thus, in order to solve the problem for the initial page, other pages must solve the problem of error 404, so it is necessary to make a change. Copy the code below, or clicking here, and created a 404.html denominated archive inside the “public” directory of his project. Switch to “pathSegmentsToKeep” variable for 1 case using a custom domain or for 0 cases using github domain.

[<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <title>Single Page Apps for GitHub Pages</title>
    <script type="text/javascript">
      // Single Page Apps for GitHub Pages
      // MIT License
      // https://github.com/rafgraph/spa-github-pages
      // This script takes the current url and converts the path and query
      // string into just a query string, and then redirects the browser
      // to the new url with only a query string and hash fragment,
      // e.g. https://www.foo.tld/one/two?a=b&c=d#qwe, becomes
      // https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
      // Note: this 404.html file must be at least 512 bytes for it to work
      // with Internet Explorer (it is currently > 512 bytes)

      // If you're creating a Project Pages site and NOT using a custom domain,
      // then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
      // This way the code will only replace the route part of the path, and not
      // the real directory in which the app resides, for example:
      // https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
      // https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
      // Otherwise, leave pathSegmentsToKeep as 0.
      var pathSegmentsToKeep = 0;

      var l = window.location;
      l.replace(
        l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
        l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
        l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
        (l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
        l.hash
      );

    </script>
  </head>
  <body>
  </body>
</html>]

It follows the index.html archive, which is inside the "public" directory and adds the thread of abode code no head start of your code, antes das tags meta.

[<script type="text/javascript">

        // Single Page Apps for GitHub Pages

        // MIT License

        // https://github.com/rafgraph/spa-github-pages

        // This script checks to see if a redirect is present in the query string,

        // converts it back into the correct url and adds it to the

        // browser's history using window.history.replaceState(...),

        // which won't cause the browser to attempt to load the new url.

        // When the single page app is loaded further down in this file,

        // the correct url will be waiting in the browser's history for

        // the single page app to route accordingly.

        (function (l) {

            if (l.search[1] === '/') {

var decoded = l.search.slice (1) .split ('&'). map (function (s) {

return s.replace (/ ~ and ~ / g, '&')

}). join ('?');

window.history.replaceState (null, null,

l.pathname.slice (0, -1) + decoded + l.hash

);

}

} (window.location))

]

Pronto, agora basta rodar o command "npm run deploy" (sem aspas) em seu terminal. Follow the url "http://user.github.io/repo" to view your project online.





Source link

RELATED ARTICLES

Rafael Nadal confirms his sister and his father were telling him to retire from QF match vs Taylor Fritz at Wimbledon – Rafael Nadal...

Rafael Nadal speaks to the media after reaching the semi-finals of Wimbledon following victory over Taylor Fritz. “They told me to retire from the...

WATCH LIVE: ESTRELLA DAMM LADIES OPEN

https://www.youtube.com/watch?v=HWCpLSXsFnY Follow the stars of the LET across the final two rounds of the Estrella Damm Ladies Open staged at Club de Golf...

Up And Down: WWE Gets Some Good News And AEW Gets Some Bad News

IMG Credit: WWE / All Elite WrestlingThey could use it. WWE has been all over the place in recent months and that...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Rafael Nadal confirms his sister and his father were telling him to retire from QF match vs Taylor Fritz at Wimbledon – Rafael Nadal...

Rafael Nadal speaks to the media after reaching the semi-finals of Wimbledon following victory over Taylor Fritz. “They told me to retire from the...

WATCH LIVE: ESTRELLA DAMM LADIES OPEN

https://www.youtube.com/watch?v=HWCpLSXsFnY Follow the stars of the LET across the final two rounds of the Estrella Damm Ladies Open staged at Club de Golf...

Up And Down: WWE Gets Some Good News And AEW Gets Some Bad News

IMG Credit: WWE / All Elite WrestlingThey could use it. WWE has been all over the place in recent months and that...

Darren Till injured out of fight with Jack Hermansson at UFC London

Darren Till can not catch a break. ESPN's Marc Raimondi broke the news today (Weds., July 6, 2022) that the former Ultimate...

Recent Comments