Gatsby Links failure after deployment on github pages - reactjs

I created website in Gatsby (my first one) and I have trouble with the Gatsby's Link on the deployed page.
I am using a gatsby-starter-react-bootstrap which includes gatsby and react-bootstrap as the name says :)
I located Links in the NavDropdown.Item which is an element of the react-bootstrap.
import React from "react"
import {Link} from "gatsby"
import {Navbar, Nav, NavDropdown, Image} from "react-bootstrap"
import Logo from "../images/Logo_White_RGB_200x42px.png";
import customer_logo from "../images/customer_logo.svg";
const CustomNavbar = ({pageInfo}) => {
return (
<>
<Navbar variant="dark" expand="md" id="site-navbar">
{/* <Container> */}
<Link to="/" className="link-no-style">
<Navbar.Brand as="span">
<Image src={Logo} />
</Navbar.Brand>
</Link>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto" activeKey={pageInfo && pageInfo.pageName}>
<NavDropdown title="Project" id="collapsible-nav-dropdown">
<NavDropdown.Item><Link to="360-viewer" activeClassName="active">360 view</Link></NavDropdown.Item>
<NavDropdown.Item><Link to="map" activeClassName="active">map</Link></NavDropdown.Item>
<NavDropdown.Item><Link to="description" activeClassName="active">description</Link></NavDropdown.Item>
</NavDropdown>
</Nav>
<Nav className="ml-auto">
<Navbar.Text>
Customer: Customer Group <Image className="customer-logo" src={customer_logo}/>
</Navbar.Text>
</Nav>
</Navbar.Collapse>
{/* </Container> */}
</Navbar>
</>
)
};
export default CustomNavbar
For deployment I use https://www.npmjs.com/package/gh-pages.
Development version run localy on localhost:8000 works totaly fine. Dropdown and all of the links work perfectly.
Routing stops to work when I try to use version for production - gatsby build creates public folder where is index.html. Routing doesn't work also when I deploy page on the github pages.
Summary:
development version works fine
production and deployed version have problems :
when I click on the dropdown, the dropdown menu doesn't open and # sign is added to the URL address - www.website.com/#
when I add to the website address 360-viewer the page is opening but when I click again on the dropdown menu, the # sign is added again to the URL -www.website.com/360-viewer/#

Your application breaks in production with Github Pages because, unlike localhost, it's not served from the root URL. To fix this, you can let Gatsby know from which path your application will be served. Gatsby will then fix the routing and links for you.
In gatsby-config.js:
module.exports = {
pathPrefix: "/your-repo-name",
}
Then add --prefix-paths flag to your build command: gatsby build --prefix-paths
They explain this a bit more in their docs: https://www.gatsbyjs.org/docs/how-gatsby-works-with-github-pages/#deploying-to-a-path-on-github-pages

Related

React not displaying images after deploy

I created a react app and it looked good , until I deployed it. After deployment, i cannot see the images, even if react loads them (i see that in the networking tab on chrome). Also the Content type was set right. Also when i go directly to the miage links, chrome renderes them.
This is the component responsable for showing the images:
import "./../styles/firstPart.css";
import logo from "./../images/croped_down.png";
import background from "./../images/background.jpg";
import PreloadImage from "react-preload-image";
import Navbar from "./../components/navbar";
const FirstPart = () => {
return (
<div className="firstPart">
<div className="mainGradient"></div>
<PreloadImage className="background" src={background} />
<PreloadImage className="logo" src={logo} />
<Navbar />
</div>
);
};
export default FirstPart;
Image path is relative and works for local. The same won't be working in prod as it takes absolute page. Check the path of image in Developer Console and see if the path is same as existed

REACT Website npm start doesn't start website

When I use NPM Start it opens up a webpage in Chrome but after a while, it says
"Aw, Snap! Something went wrong while displaying this webpage."
GitHub Repository for the code. I can't give a small piece fo code because I don't know what is giving the error.
I would like it to run and show my webpages. Not sure why it isn't working. The first time I started the server using NPM start it gave me this page.
The issue is with the Navbar.js
The issue is your class name here was Nav but Nav was also used in the render function. I have renamed Nav to CustomNavbar and imported the bootstrap nav from the npm package.
change your code to this
import React from 'react';
import { Link } from 'react-router-dom';
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import { Button, FormControl, Form, NavDropdown } from "react-bootstrap";
export default class CustomNavbar extends React.Component {
render() {
return (
<div>
<Navbar bg="light" expand="lg">
<Navbar.Brand href="#home">Pawsitively Delicious</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/">Home</Nav.Link>
<Nav.Link href="AboutUs.js">About Us</Nav.Link>
<NavDropdown title="Ingredients" id="basic-nav-dropdown">
<NavDropdown.Item href="./TypesOfDogTreats">Types of Dog Treats</NavDropdown.Item>
<NavDropdown.Item href="AllIngredients.js">Ingredients</NavDropdown.Item>
</NavDropdown>
</Nav>
<Form inline>
<FormControl type="text" placeholder="Search" className="mr-sm-2" />
<Button variant="outline-success">Search</Button>
</Form>
</Navbar.Collapse>
</Navbar>
</div>
);
} // END OF RENDER
} // END OF Navbar
I was having this issue, for me the problem was caused by adding a button component so i deleted a part of my codes
const checkButtonStyle = STYLES.includes(buttonStyle)?buttonStyle:STYLES[0]
const checkButtonSize = SIZE.includes(buttonSize) ? buttonSize: SIZE[0]
try this link for more options on how to solve it
1:

react-bootstrap cant render Nav.Link

I'm currently developing a web application with React and react-bootstrap. Everything is runnning fine except the render process of my navigation area on the top of my webpage.
The problem looks as follows:
I want to display a element inside my navigation bar with a couple of elements. But everytime my page is displayed inside the web browser, the element Nav.Link isn't loaded and thus defined as undefined.
Hereis the corresponding file:
import React,{Component} from 'react';
import './HorizontalNavBar.css';
import atom from './res/atom.svg';
import file from './res/file_icon.svg';
import tag from './res/tag.svg';
import user from './res/user_icon.svg';
import {Navbar,Nav,NavDropdown,Form,FormControl} from 'react-bootstrap';
import { bootstrapUtils } from 'react-bootstrap/lib/utils';
class HorizontalNavBar extends React.Component
{
constructor(props)
{
super(props);
this.state =
{
actions:["Files","TagManager","Profile"],
};
}
render()
{
return(
<Navbar bg="dark" variant="dark" expand="lg">
<Navbar.Brand href="#">
<img
src={atom}
alt="Logo"/>
SciTag
</Navbar.Brand>
<Navbar.Collapse>
<Nav>
<Nav.Link href="#home">Home</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
export default HorizontalNavBar;
Here are a couple of things of solutions I've already tried out:
Checked the official documentation of react-bootstrap
Downloaded the Bootstrap Stylesheet again
Reconfigured webpack
Updated React to the latest stable version
Edit:
I've discovered the source of the error myself. While the latest documentation is explaining how developers can utilize react-bootstrap version 1.0.0-beta.5, I was using my < 1.0.0 version with the latest tutorials. Of course, this didn't work out because the components which are explained it the tutorials are not included in my setup. The solution was to download latest beta version and use the stylesheets from Bootstrap 4 and not Bootstrap 3.
Your issue is in the way you implemented it:
You put the NavLink in the Navbar.Collapse Wrapper. That Navbar.Collapse wrapper is part of ensuring a responsive behaviour of your navigation:
Use the expand prop as well as the Navbar.Toggle and Navbar.Collapse components to control when content collapses behind a button.
Please see here: https://react-bootstrap.github.io/components/navbar/
In fact, your NavLink item is there - but you do not see it because it is hidden unless you press the Navbar.Toggle item. Since you didn't implement the item for now, there is no way of seeing it at all.
Now you have two options: implementing the Navbar component without responsive behaviour or with it.
Option 1, without responsive behaviour (we removed the Navbar.Collapse wrapper):
<Navbar bg="dark" variant="dark" expand="lg">
<Navbar.Brand href="#">SciTag</Navbar.Brand>
<Nav>
<Nav.Link href="#home">Home</Nav.Link>
</Nav>
</Navbar>
Option 2, with responsive behaviour (we added the Navbar.Collapse wrapper with some options)
<Navbar collapseOnSelect bg="dark" variant="dark" expand="lg">
<Navbar.Brand href="#">SciTag</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse>
<Nav>
<Nav.Link href="#home">Home</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
Working sandbox: https://codesandbox.io/s/8l85rqqk8

Reactjs URL not working on Safari Browser , only on chrome browser in Production Mode - Netlify

I have deployed a static site in netlify , but the routes I defined in the development mode, only run in chrome browsers, rest other browsers return 404 - page not found.
import React, { Component } from 'react';
import { Navbar , NavItem } from 'react-materialize';
import { Link } from 'react-router-dom';
class NavBar extends Component {
render() {
return (
<div>
<Navbar brand='MT' right style={{backgroundColor:'#7f8fa6'}}>
<NavItem href="/" to="/">About Me</NavItem>
<NavItem href="/services" to="/services">My Services</NavItem>
<NavItem href="/works" to="works">My Works</NavItem>
<NavItem href="/videos" to="videos">My Videos</NavItem>
<NavItem href="/request" to="request">Request a Quote</NavItem>
</Navbar>
</div>
)
}
}
export default NavBar;
This is my Netlify Url.
https://mithilesh-tarkar.netlify.com/
It Opens on Chrome browser on mobile and on laptop.
But it doesnt open on safari and firefox. why is this so ??? can someone help.
The index page is where react router can handle the routing, and I think this issue occurs because if you route to say '/store' somehow if you don't take the below steps it actually routes you to '/store' rather than your react route kicking in from the index page, as to why it works on some browsers and not others, I'm not sure. Would love to know.
By checking your site, (nice site) you seem to have this figured out.
But others with the same issue visiting this page could try creating a file in your public folder called _redirects.
Then inside that file add:
/* /index.html 200
More info can be found here:
https://www.crookm.com/2018/02/one-page-app-routing-on-netlify.html
https://www.netlify.com/docs/redirects/#history-pushstate-and-single-page-apps

react-bootstrap navbar odd behavior

So I just dived into the react-bootstrap library today to implement a navbar.
The page with code and demo I looked into: https://react-bootstrap.github.io/components/navbar/#navbars-mobile-friendly
Notice how the examples are already nicely styled. I tried doing pretty much the same, but it looks depressing and unstyled at all, not even the bg is working.
My code:
import React from "react";
import {Nav, Navbar} from 'react-bootstrap'
import './style.css';
import logo from '../../assets/logo.png';
function LoginNav() {
return (
<Navbar expand="lg" bg="dark" variant="dark">
<Navbar.Brand>
<img id="logo"
className="d-inline-block align-top"
src={logo} alt="logo"
/>
</Navbar.Brand>
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="#home">Login</Nav.Link>
<Nav.Link href="#link">Link</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
};
export default LoginNav;
My CSS doesn't do anything other than resizing the logo, and I did yarn add react-bootstrap as well as npm i --save react-bootstrap already. I have no idea why it is not working. Any insight can help! Thanks
Without errors for us to look at, there is not much to go off of. I would start with checking where you are importing the LoginNav to. if the <Navbar></Navbar> is a container level element and you are inserting it into a row or col level it may be causing formatting issues.
Update 5-15-2020
You stated the above code is all you have and if so you need to add your Navbar to existing HTML or create a new React App. Either way the info is here https://reactjs.org/.
I was able to run your code just fine and it worked. I changed the bg from dark to light just to see if it changed, and it did.
The commands below will create a React App, and will start the node server environment. Run the commands in console, in a directory you want the app saved. The 'my-app' at the end of the first command is going to be the name of your App. So it can be whatever you want, and 'npx' isn't a typo.
npx create-react-app my-app
cd my-app
npm start
The code below is how I used the Navbar and it displayed just fine. If you already have all this set up, try and put the style sheet import right before the ReactDOM.render() in index.js
Update
Is this how your file set-up looks?
//index.html
< !DOCTYPE html >
<html lang="en">
<head>
// loads of stuff here, taken out for shorthand. Not important for the example
// but important for the overall project to work and is included when you create
// the app. Plus any additional things you add
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
//index.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './fileLocation'
//import style sheet here and see if that helps with formatting
ReactDOM.render(<App />, document.getElementById('root'))
//App.js
import React, { Component } from 'react'
import LoginNav from './fileLocation'
class App extends Component {
render(){
return (<LoginNav />);
}
}
export default App

Resources