Adding external css and js will not working in Gatsby js - reactjs

I am trying to use my template to build a project in the gatsby js but all the CSS and js aren't working.
/**
* Layout component that queries for data
* with Gatsby's useStaticQuery component
*
* See: https://www.gatsbyjs.org/docs/use-static-query/
*/
import React from "react"
import PropTypes from "prop-types"
import { useStaticQuery, graphql } from "gatsby"
import {Helmet} from 'gatsby'
import Header from "./header"
import "./layout.css"
const Layout = ({ children }) => {
const data = useStaticQuery(graphql`
query SiteTitleQuery {
site {
siteMetadata {
title
}
}
}
`)
return (
<>
<Helmet>
<link href="js/vendor/bootstrap/bootstrap.min.css" rel="stylesheet"/>
<link href="js/vendor/slick/slick.min.css" rel="stylesheet"/>
<link href="js/vendor/fancybox/jquery.fancybox.min.css" rel="stylesheet"/>
<link href="js/vendor/animate/animate.min.css" rel="stylesheet"/>
<link href="css/style-light.css" rel="stylesheet"/>
<link href="fonts/icomoon/icomoon.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i" rel="stylesheet"/>
<script src="js/vendor/jquery/jquery.min.js"></script>
<script src="js/vendor/bootstrap/bootstrap.bundle.min.js"></script>
<script src="js/vendor/slick/slick.min.js"></script>
<script src="js/vendor/scrollLock/jquery-scrollLock.min.js"></script>
<script src="js/vendor/instafeed/instafeed.min.js"></script>
<script src="js/vendor/countdown/jquery.countdown.min.js"></script>
<script src="js/vendor/imagesloaded/imagesloaded.pkgd.min.js"></script>
<script src="js/vendor/ez-plus/jquery.ez-plus.min.js"></script>
<script src="js/vendor/tocca/tocca.min.js"></script>
<script src="js/vendor/bootstrap-tabcollapse/bootstrap-tabcollapse.min.js"></script>
<script src="js/vendor/isotope/jquery.isotope.min.js"></script>
<script src="js/vendor/fancybox/jquery.fancybox.min.js"></script>
<script src="js/vendor/cookie/jquery.cookie.min.js"></script>
<script src="js/vendor/bootstrap-select/bootstrap-select.min.js"></script>
<script src="js/vendor/lazysizes/lazysizes.min.js"></script>
<script src="js/vendor/lazysizes/ls.bgset.min.js"></script>
<script src="js/vendor/form/jquery.form.min.js"></script>
<script src="js/vendor/form/validator.min.js"></script>
<script src="js/vendor/slider/slider.js"></script>
<script src="js/app.js"></script>
</Helmet>
<Header siteTitle={data.site.siteMetadata.title} />
<div
style={{
margin: `0 auto`,
maxWidth: 960,
padding: `0 1.0875rem 1.45rem`,
}}
>
<main>{children}</main>
</div>
</>
)
}
Layout.propTypes = {
children: PropTypes.node.isRequired,
}
export default Layout
But all the files not included any CSS or JS from this example.
Here are all files
Tell me if I am doing something wrong. Any help would be appreciating.

After doing lots of research and #Ferran reply and these post
How to include local javascript on a Gatsby page?
How to use global css style sheet includes with GatsbyJS
I have been completed like this.
/**
* Layout component that queries for data
* with Gatsby's useStaticQuery component
*
* See: https://www.gatsbyjs.org/docs/use-static-query/
*/
import React from "react"
import PropTypes from "prop-types"
import { useStaticQuery, graphql } from "gatsby"
import Helmet from "react-helmet"
import { withPrefix, Link } from "gatsby"
import Header from "./header"
import "./layout.css"
import "../../static/js/vendor/bootstrap/bootstrap.min.css"
import "../../static/js/vendor/slick/slick.min.css"
import "../../static/js/vendor/fancybox/jquery.fancybox.min.css"
import "../../static/js/vendor/animate/animate.min.css"
import "../../static/css/style-light.css"
import "../../static/fonts/icomoon/icomoon.css"
// import "https://fonts.googleapis.com/css?family=Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i"
// import "https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i"
const Layout = ({ children }) => {
const data = useStaticQuery(graphql`
query SiteTitleQuery {
site {
siteMetadata {
title
}
}
}
`)
return (
<>
<Helmet>
<script src={withPrefix('js/vendor/jquery/jquery.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/slick/slick.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/scrollLock/jquery-scrollLock.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/instafeed/instafeed.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/countdown/jquery.countdown.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/imagesloaded/imagesloaded.pkgd.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/ez-plus/jquery.ez-plus.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/bootstrap-tabcollapse/bootstrap-tabcollapse.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/isotope/jquery.isotope.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/fancybox/jquery.fancybox.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/cookie/jquery.cookie.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/bootstrap-select/bootstrap-select.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/lazysizes/lazysizes.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/lazysizes/ls.bgset.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/form/jquery.form.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/form/validator.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/slider/slider.js')} type="text/javascript" />
<script src={withPrefix('js/app.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/bootstrap/bootstrap.bundle.min.js')} type="text/javascript" />
<script src={withPrefix('js/vendor/tocca/tocca.min.js')} type="text/javascript" />
{/* <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> */}
</Helmet>
<Header siteTitle={data.site.siteMetadata.title} />
<div
style={{
margin: `0 auto`,
maxWidth: 960,
padding: `0 1.0875rem 1.45rem`,
}}
>
<main>{children}</main>
</div>
</>
)
}
Layout.propTypes = {
children: PropTypes.node.isRequired,
}
export default Layout
I hope anyone won't waste at the same time as I wasted.

According to Gatsby's docs you need to point your static/external assets using relative paths, such as (extracted from their documentation):
render() {
// Note: this is an escape hatch and should be used sparingly!
// Normally we recommend using `import` for getting asset URLs
// as described in the “Importing Assets Directly Into Files” page.
return <img src={'/logo.png'} alt="Logo" />;
}
Without knowing your project folder structure it seems that your issue is related to this. Try to use relative paths in your component. If you have kept the Gatsby default structure, I would suggest something like this:
<link href="../js/vendor/bootstrap/bootstrap.min.css" rel="stylesheet"/>
And so on...
I've replied your structure on my project and it should work with ../js/your/path/to-file,../css/your/path/to-file, etc.

Related

SSR Next.js <Head> tag is not rendering at the server side

import { getMeta } from '#/helpers/globalHelpers';
import Head from 'next/head';
import { useDispatch, useSelector } from 'react-redux';
import { increment, decrement, selectValue, bulkUpdate, reset } from '#/slicers/propertySlice';
import Link from 'next/link';
export default function Home({ meta }) {
const data = useSelector(selectValue);
const dispatch = useDispatch();
console.log(meta);
return (
<>
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="og:title" content={meta.title} />
<meta name="og:description" content={meta.metadescription} />
<meta name="description" content={meta.metadescription} />
<meta name="keywords" content={meta.keywords} />
<title>{meta.title}</title>
</Head>
Hello world!
</>
)
}
export async function getServerSideProps({ resolvedUrl }) {
const pageData = await getMeta(resolvedUrl);
return {
props: { meta: pageData[0] }
}
}
I am working with next.js yarn and redux. I am trying to get my SEO data from my api and render is wit ssr. But it is rendering at the client side and SEO crawler tools (i am using screaming frog) can't see my title and description
Here is my code. I couldn't understand why my Head tag is rendering at the client side. Anyone can help?
Extra info, i checked _document.js, tag is correctly used and defined.
import { Html, Head, Main, NextScript } from 'next/document'
import Script from 'next/script'
export default function Document() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
<Script src="assets/js/jquery.min.js"></Script>
<Script src="assets/bootstrap/js/bootstrap.min.js"></Script>
<Script src="assets/js/aos.min.js"></Script>
<Script src="assets/js/bs-init.js"></Script>
<Script src="assets/js/checkbox-show-hide.js"></Script>
<Script src="assets/js/dropdown-saver.js"></Script>
<Script src="assets/js/floatsearch.js"></Script>
<Script src="assets/js/heroclean.js"></Script>
<Script src="assets/js/location-list.js"></Script>
<Script src="assets/js/mobilemenu.js"></Script>
</body>
</Html>
)
}

Uncaught SyntaxError: Unexpected token '!' reactjs mainchunk.js

I am learning reactjs I have made components from a html template, when I am calling them I am receiving this error:
class App extends !(function webpackMissingModule() { var e = new Error("Cannot find module './components'"); e.code = 'MODULE_NOT_FOUND'; throw e; }())
Please any one let me know why this is occurring
**App.js**
import React from 'react';
import Component from './components';
import logo from './logo.svg';
import './App.css';
import About from './components/about';
import Blog from './components/blog';
import Contact from './components/contact';
import Footer from './components/footer';
import Home from './components/home';
import Menus from './components/menus';
import Projects from './components/projects';
import Reasearch from './components/reasearch';
import Services from './components/services';
import Testimonials from './components/testimonials';
import Welcome from './components/welcome';
class App extends Component {
render() {
return (
<div id="colorlib-page">
<div id="container-wrap">
<Menus></Menus>
<Welcome></Welcome>
<Welcome></Welcome>
<Services></Services>
<Reasearch></Reasearch>
<Projects></Projects>
<About></About>
<Testimonials></Testimonials>
<Blog></Blog>
<Contact></Contact>
<Footer></Footer>
</div>
</div>
);
}
}
export default App;
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Arvind Pundir - A Fullstack Web & App Developer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:200,300,400,600,700,800,900"
rel="stylesheet">
<link rel="stylesheet" href="%PUBLIC_URL%/css/open-iconic-bootstrap.min.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/animate.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/owl.carousel.min.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/owl.theme.default.min.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/magnific-popup.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/aos.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/ionicons.min.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/flaticon.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/icomoon.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/style.css">
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!-- loader -->
<div id="ftco-loader" class="show fullscreen"><svg class="circular" width="48px" height="48px">
<circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee"/>
<circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10"
stroke="#F96D00"/></svg></div>
<script src="%PUBLIC_URL%/js/jquery.min.js"></script>
<script src="%PUBLIC_URL%/js/jquery-migrate-3.0.1.min.js"></script>
<script src="%PUBLIC_URL%/js/popper.min.js"></script>
<script src="%PUBLIC_URL%/js/bootstrap.min.js"></script>
<script src="%PUBLIC_URL%/js/jquery.easing.1.3.js"></script>
<script src="%PUBLIC_URL%/js/jquery.waypoints.min.js"></script>
<script src="%PUBLIC_URL%/js/jquery.stellar.min.js"></script>
<script src="%PUBLIC_URL%/js/owl.carousel.min.js"></script>
<script src="%PUBLIC_URL%/js/jquery.magnific-popup.min.js"></script>
<script src="%PUBLIC_URL%/js/aos.js"></script>
<script src="%PUBLIC_URL%/js/jquery.animateNumber.min.js"></script>
<script src="%PUBLIC_URL%/js/scrollax.min.js"></script>
<script src="%PUBLIC_URL%/js/main.js"></script>
</body>
The issue is with this line of code:
import Component from './components';
Notice that this is a directory for most of your other component imports:
import About from './components/about';
import Blog from './components/blog';
import Contact from './components/contact';
import Footer from './components/footer';
import Home from './components/home';
import Menus from './components/menus';
import Projects from './components/projects';
import Reasearch from './components/reasearch';
import Services from './components/services';
import Testimonials from './components/testimonials';
import Welcome from './components/welcome';
If you don't give an exact path to a component within your ./components directory there needs to be an index.js file within your ./components directory for the import Component from './components'; to work correctly. You can see this from your error message:
class App extends !(function webpackMissingModule() { var e = new Error("Cannot find module './components'"); e.code = 'MODULE_NOT_FOUND'; throw e; }())
This is telling you it can't find the ./components module associate with the import Component from './components'; import. Either add an index.js file within your ./components directory containing the necessary code for said component or double check that your path is correct for said import, i.e.:
import Component from './components'<COMPONENT>;
where <COMPONENT> is the name of the component you meant to import.
Hopefully that helps!

How to add multiple React components to HTML

I wonder if we can add multiple react components into HTML without having the related files usually downloaded with npm, or added to a normal HTML but we add a certain component for a chat app as my attempt here, here is a long example:
<html lang="en">
<head>
<script src="https://unpkg.com/babel-standalone#6/babel.min.js"></script>
<script crossorigin src="https://unpkg.com/react#16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom#16/umd/react-dom.development.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
class App extends React.Component{
state={
messages:[
{ }
],
userId:''
}
addMessage=(message)=>{
message.num = Math.random();
message.id=this.state.messages.id;
let messages = [...this.state.messages, message];
this.setState({
messages })
}
addId=(userId)=>{
this.setState({
userId
})
}
render() {
return (
<div className="appContainer">
<User userId={this.addId} />
<TopSection Users={this.state.userId}/>
<Messages userId = {this.state.userId} messages={this.state.messages}/>
<AddMessage addMessage={this.addMessage} />
</div>
);
}
}
}
const Messages = ({messages, userId}) =>{
const messageList= (messages.length)? (messages.slice(1).map(message=>{
return(
<div className="message" key={message.num}>
<span key={userId.num}>{message.content?(userId):(null)}</span>
<p>{message.content}</p>
</div>
)
})) : (null);
return(
<div className="textContainer">
{messageList}
</div>
)
}
ReactDOM.render(<App />, document.getElementById('app'));
</script>
</body>
</html>
please let me know if there is away to get this to work.
You mean add components to different dom nodes ?
import {render} from 'reactDOM'
import React from 'react'
import AppOne from './appOne'
import AppTwo from './appTwo'
render(<AppOne />, document.getElementById('appOne'));
render(<AppTwo />, document.getElementById('appTwo'));
React v16 has portals as well for adding componnets to differnt parts of the dom tree

GatsbyJS Layout.js returns error unexpected token

I am building a GatsbyJS website and created the layout.js, index.js with some static data and am trying to compile it but it does not compile. The code from layout.js and the error message are below.
import React from 'react'
import PropTypes from 'prop-types'
import Helmet from 'react-helmet'
import { StaticQuery, graphql } from 'gatsby'
import Header from './header'
import './theme.css'
const Layout = ({ children }) => (
<StaticQuery
query={graphql`
query SiteTitleQuery {
site {
siteMetadata {
title
}
}
}
`}
render={data => (
<Helmet
title={data.site.siteMetadata.title}
meta={[
{ name: 'description', content: 'Sample' },
{ name: 'keywords', content: 'sample, something' },
]}
>
<html lang="en" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="assets/ico/favicon.ico"/>
// Font icon
<link href="assets/css/plugin/font-awesome.min.css" rel="stylesheet" type="text/css" />
// CSS Global
<link href="../../assets/css/plugin/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="../../assets/css/plugin/bootstrap-select.min.css" rel="stylesheet" type="text/css" />
<link href="../../assets/css/plugin/owl.carousel.css" rel="stylesheet" type="text/css" />
<link href="../../assets/css/plugin/animate.css" rel="stylesheet" type="text/css" />
<link href="../../assets/css/plugin/subscribe-better.css" rel="stylesheet" type="text/css" />
// Custom CSS
<link href="../../assets/css/theme.css" rel="stylesheet" type="text/css" />
// Custom JS
<script src="../../assets/js/theme.js"></script>
<script src="../../assets/js/plugin/jquery-2.2.4.min.js"></script>
<script src="../../assets/js/plugin/bootstrap.min.js"></script>
<script src="../../assets/js/plugin/bootstrap-select.min.js"></script>
<script src="../../assets/js/plugin/owl.carousel.min.js"></script>
<script src="../../assets/js/plugin/jquery.plugin.min.js"></script>
<script src="../../assets/js/plugin/jquery.countdown.js"></script>
<script src="../../assets/js/plugin/isotope.js"></script>
<script src="../../assets/js/plugin/jquery.subscribe-better.min.js"></script>
<Helmet/>
<Header siteTitle={data.site.siteMetadata.title} class="main-header"/>
<div
style={{
margin: '0 auto',
maxWidth: 960,
padding: '0px 1.0875rem 1.45rem',
paddingTop: 0,
}}
>
{children}
</div>
)}
/>
)
Layout.propTypes = {
children: PropTypes.node.isRequired,
}
export default Layout
It returns the error:
/home/ec2-user/environment/bestpet-v1/src/components/layout.js
77:11 error Parsing error: Unexpected token, expected "}"
75 |
76 | Layout.propTypes = {
> 77 | children: PropTypes.node.isRequired,
| ^
78 | }
79 |
80 | export default Layout
It does not compile at all. I checked all the brackets and closing tags but apparently I'm missing something.
What am I doing wrong?
You aren't closing your Helmet tag. Right now you have this:
<Helmet>
<OtherStuff />
<Helmet />
That last “tag” is self-closing, not closing the opening tag. What you want is this:
<Helmet>
<OtherStuff />
</Helmet>

Very ReactJS with PHP

Hi I have started to learn React JS but when I'm Starting to fetch API I'm getting an error:
parsing failed SyntaxError: Unexpected end of input
This is my source code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style type="text/css">
body {
background: #538cb5;
}
.app-container {
background: #fff;
max-width: 480px;
margin: 60px auto;
padding: 40px 25px;
}
h3 {
color: #538cb5;
}
input {
margin: 20px 0;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js' crossorigin></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js" crossorigin></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js" crossorigin></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js" crossorigin></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class App extends React.Component {
render() {
return <Data />
}
}
class Data extends React.Component {
constructor(props) {
super(props)
this.state = {
data : [
{"play_slot_id":"227","playSlot":"E","date":"2018-10-06","time":"14:00","randValue":"683394","new_randValue":"","createdDate":"2018-10-06 13:26:30","updatedDate":"2018-10-06 13:26:30","status":"active"},{"play_slot_id":"227","playSlot":"E","date":"2018-10-06","time":"14:00","randValue":"683394","new_randValue":"","createdDate":"2018-10-06 13:26:30","updatedDate":"2018-10-06 13:26:30","status":"active"},{"play_slot_id":"227","playSlot":"E","date":"2018-10-06","time":"14:00","randValue":"683394","new_randValue":"","createdDate":"2018-10-06 13:26:30","updatedDate":"2018-10-06 13:26:30","status":"active"}
]
}
}
componentDidMount(){
fetch('http://iqexpert.in/android_login_api/getRand.php',{mode: 'no-cors'})
.then((res) => {
console.log(res)
return res.json()
})
.then((data) => {
console.log('parsed json', data)
this.setState({data : data})
})
.catch(function(ex) {
console.log('parsing failed', ex)
})
}
render(){
if(!this.state.data) return <p>Loading</p>
var elements = this.state.data.map( (item,index) => {
return <li key={item.id}>SLOT: {item.playSlot} | Random: {item.randValue} </li>
})
console.log(elements)
return <ol>{elements}</ol>
}
}
ReactDOM.render(<App />, document.getElementById("root"))
</script>
</body>
</html>
I tried with and without CDN but still getting the same errors in ReactJS. I have made API's for my Android Project. It is consumed properly in Android devices but I am not able to render properly in React JS.
I am not sure how we should implement React components between script tags, but don`t you need to import React as import React from 'react'
<script type=text/babel>
import React from 'react' // <<---- HERE
class App extends React.Component {

Resources