Correct picture path - reactjs

I have this site
(Gatsby and Strapi API deployed in Heroku). My problem is that I can't find the proper path to render the pictures.
Here is the API and here is the repo.
My code regarding the rendering of the pics:
.map(band => (
<article className="bands" key={band.id}>
<h2>{band.band_name}</h2>
<img
src={band.band_img.name}
alt={band.band_name}
/>
<p>{band.band_desc}</p>
I've already replace src={band.band_img.name} to a number of things like:
band.band_img.formats.thumbnail.url
band.band_img.url
so on and so forth to no avail.
A friend of mine told me to create .env files
uploads_path: /uploads/ (.env.production)
and
uploads_path: uploads/ (.env)
I did and nothing.
Picture of console.log(band.band_img)
Please, any suggestions? Thank you in advance.

Your problem is that the images are not being uploaded. If you look at the following images path, they won't be displayed (what you were trying so far):
https://lebraojohns.netlify.app/static/uploads/thumbnail_badreligion_7007c2c028.jpeg
https://lebraojohns.netlify.app/uploads/thumbnail_badreligion_7007c2c028.jpeg
I've tried looking at several paths but none of them worked for me.
You won't see any image so your code is not able to display anything at band.band_img.name with according to your API is where it should be:
band_img: {
id: 7,
name: "badreligion.jpeg",
alternativeText: "",
caption: "",
width: 315,
height: 160,
formats: {
thumbnail: {
ext: ".jpeg",
url: "/uploads/thumbnail_badreligion_7007c2c028.jpeg",
hash: "thumbnail_badreligion_7007c2c028",
mime: "image/jpeg",
name: "thumbnail_badreligion.jpeg",
path: null,
size: 9.63,
width: 245,
height: 124
}
}
P.S: I love Bad Religion and all the bands 🤘
Your code looks good as soon as you fix your image uploading/server issue the code should work alone.
The images that you have stored under /src/assets will be never accessible for your code (transpiled to the public folder) unless you use them in one component.
Depending on your specifications, you can bypass it by adding your images under the /static folder to clone them into the public one.

It's a problem with your server, for example the following should work:
const getBandImg = (band) => band.band_img?.formats?.url;
And to use it anywhere you want to get the img url:
.map(band => (
<article className="bands" key={band.id}>
<h2>{band.band_name}</h2>
<img
src={getBandImg(band)}
alt={band.band_name}
/>
<p>{band.band_desc}</p>
Which you already tried with band.band_img.formats.thumbnail.url, but after taking a look at your website, I noticed that the images aren't reachable either in the API or the frontend, for example:
https://john-strapi.herokuapp.com/uploads/dri_6a22b5e947.jpeg
Doenst return the image
https://lebraojohns.netlify.app/uploads/dri_6a22b5e947.jpeg
Neither
So it is something you have to solve in your host, not a problem with gatsby

Related

Is there a way to rename automatically generated routes JSON file in Next.js?

I have a problem, when I click to go to the /analytics page on my site, adblockers block the analytics.json file that's being requested by Next.js as they think it's an analytics tracker (it's not, it's a page listing analytics products).
Is there a way to rename the route files Next.js uses when navigating to server-side rendered pages on the client-side?
I want to either obfuscate the names so they're not machine readable, or have a way to rename them all.
Any help appreciated.
With thanks to #gaston-flores I've managed to get something working.
In my instance /analytics is a dynamic page for a category, so I moved my pages/[category]/index.tsx file to pages/[category]/category.tsx and added the following rewrite:
// next.config.js
module.exports = {
async rewrites() {
return [
{
source: "/:category",
destination: "/:category/category",
},
];
},
};
This now gets the category.json file rather than analytics.json, which passes the adblockers checks and renders as expected.
Note that due to having a dynamic file name in the pages/[category] directory (pages/[category]/[product].tsx), I had to move that to pages/[category]/product/[product].tsx as I was seeing the /analytics page redirected to /analytics/category for some reason without this tweak.

How to change the url of gatsby sitemap?

I used https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/ to create the sitemap for my website.
Currently, the sitemap is listed at https://www.myWebsite.com/sitemap/sitemap-0.xml, but I want it to be located at https://www.myWebsite.com/sitemap.xml
How can I change the sitemap's location? Sorry if this is obvious
You can't automatically since there's no option nor configuration to do so. Your only change is using any small Node script that changes the filename or doing it manually.
However, there's no SEO problem at all having a sitemap-0.xml as long as you point your Google's Search Console to that file.
Another solution is using gatsby-plugin-advanced-sitemap which by default outputs a file named sitemal.xml as you can see in their example: https://gatsby.ghost.org/sitemap.xml. This plugin is based on the one you are using so it should be quite straightforward swapping between them.
Another solution is to use the same gatsby-plugin-sitemap plugin and then add:
gatsby-plugin-robots-txt plugin and point to the file name in the gatsby-config Something as bellow:
{
resolve: 'gatsby-plugin-robots-txt',
options: {
host: SiteConfig.url,
sitemap: ${SiteConfig.url}/sitemap.xml, HERE PLACE sitemap-0.xml
policy: [{ userAgent: '*', allow: '/' }]
}
},
After doing that, look for set up on webpage/robots.txt to see where is the point

hugo URLs with protocol (http) not supported

I am using hugo (https://gohugo.io/) and trying to make an external link in the menu (docsy theme).
I changed the _index.md to:
---
title: "Documentation"
linkTitle: "Documentation"
url: "https://www.myurl.com"
weight: 20
menu:
main:
weight: 20
---
However hugo reports:
Rebuild failed: URLs with protocol (http*) not supported
Could anyone advise of how to create an external URL link directly on the menu?
Thanks, Gregor
Keep in mind that "url" is a predefined front matter variable that is meant to be used for defining the full path to the content page from the site root. If you want to add an external link in your front matter, I'd recommend giving the variable a different name, e.g. "external_url".
I just added something like this:
[[menu.main]]
name = "GitHub"
weight = 50
url = "https://github.com/google/docsy/"
To the config.toml file.
I found this solution for the docsy theme here: https://www.docsy.dev/docs/adding-content/navigation/
Hope this helps, Gregor

What is the best way to make my Gatsby rig pull in images from S3?

I am trying to incorporate ai2html into a Gatsby-built repo. I realized quickly that I cannot simply used gatsby-image because I am not deploying everything to one place: my code, via public/index.html, goes to one place and my images live in AWS S3. I'm looking for a way to pull images via URL into my src/index.js, ideally in a step-by-step tutorial way because I am new at Gatsby stuff.
So far, I've tried using gatsby-source-s3 (docs here) but I'm not sure I've set it up correctly in my index.js (I've already installed it and put the relevant info in gatsby-config.js:
import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import "../styles/ai_styles.css"
export default () => {
const data = useStaticQuery(graphql`
query IndexQuery {
images: allS3Images {
edges {
node {
Key
Url
}
}
}
}
`)
return (
<div>
<div id="g-crime-box" className="ai2html ai2html-box-v5">
<div id="g-mobile" className="g-artboard" style={{width: '280px', height: '2328.3424680476px'}} data-aspect-ratio="0.12" data-min-width="280" data-max-width="579">
<Img fluid={data.images.node.Url} alt="alt desktop image" />
...
</div>
My main questions are, is {data.images.node.Url} the best way to call the image or am I doing that wrong? And is this the best way to do this? Has anyone seen other ways to do this?
You're right that you should pull images from S3 at build time, and that using a community plugin for this –like gatsby-source-s3– will make your life easier.
Right now, though, you're using the AWS object URL directly in your source, so images will basically be downloaded directly from your bucket, unprocessed, by your users.
What you probably want is to download the image into a local node (this is handled by Gatsby's sourceNodes API) to process with sharp.
To do this, you'll need to change your GraphQL query to use childImageSharp on your S3 nodes. gatsby-source-s3 has an example of this on its README:
query IndexQuery {
images: allS3Image {
edges {
node {
file: localFile {
image: childImageSharp {
sizes(maxWidth: 400, maxHeight: 400) {
...GatsbyImageSharpSizes_withWebp_tracedSVG
}
}
}
}
}
}
}
Last but not least...
(Disclaimer: I'm the author of a plugin mentioned below.)
The gatsby-source-s3 plugin looks unmaintained, so you might run into issues with it.
For that reason, I built another S3 source plugin for my Gatsby sites, and published it here:
#robinmetral/gatsby-source-s3
The README has extensive documentation, including a section on processing images with sharp, that you might find useful.

Laravel returning a 404 on an image

This should be fairly simple though it is completely stumping me.
I have a backend Laravel installation running on localhost:8000
I have a front end Angular app running on localhost:9001.
I have some 'static' images I have included in my seed data (eg.
'1', 'user.png'), these images are being rendered perfectly in my front end (they are also served from the exact place my image uploads are going).
The URL I am currently serving images from is http://localhost:8000/images/{filename}
I can upload images from the front to the back end and they appear in the DB and the image is being put in the filesystem, I'm populating the correct URL in my front end (as evidenced by the previous URL).
My uploaded images are not being shown.
In my logs I am getting:
[2015-01-20 18:13:49] local.ERROR: NotFoundHttpException Route: http://localhost:8000/images/j249ae747ce28c317e02f1fb6d0a10c3.jpg [] []
[2015-01-20 18:13:49] local.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException'
I tried a method in my routes file, but couldnt see why, when I am already serving some images already?
I have also set all permissions to 755 on my /images folder.
Any ideas?
I'm not sure I follow every bit of multi-system interaction you have going on, but I'd drop back to first HTTP principles.
Try accessing the image URL directly.
http://localhost:8000/images/j249ae747ce28c317e02f1fb6d0a10c3.jpg
If the error in your browser (or your logs, if you're not developing with debug set to true) is
local.ERROR: NotFoundHttpException Route: http://localhost:8000/images/j249ae747ce28c317e02f1fb6d0a10c3.jpg
This means your web server couldn't find a file at images/j249ae747ce28c317e02f1fb6d0a10c3.jpg, and handed the request to Laravel. This means you need to figure out why your webserver can't see the file.
Assuming you're serving index.php from the public folder
Do you have a public/images/j249ae747ce28c317e02f1fb6d0a10c3.jpg file?
Are you sure? Copy and paste the path into terminal and to a ls public/images/j249ae747ce28c317e02f1fb6d0a10c3.jpg to make sure your brain isn't missing some subtle case issue
Are any errors showing up in your web server's logs (not Laravel's)
Can you create a text/html file in the images folder and serve it? If not, then you may not be pointing your web server at the folder you think you are.
Something like
http://localhost:8000/images/test.txt
http://localhost:8000/images/test.html
Some first principles debugging like that should point you in the right direction.
rm public/storage
php artisan optimize:clear
php artisan storage:link
This worked for me.
The problem is you haven't generated a url for your uploaded image
Try accessing your url like this
http://localhost:8000/storage/images/j249ae747ce28c317e02f1fb6d0a10c3.jpg
To generate the above url
Add this method \Storage::disk('public')->url(); method in your controller.This method accesses the public disk array which is found in Config\filesystems.php and it generates a url in the following format
http://localhost:8000/storage/images/j249ae747ce28c317e02f1fb6d0a10c3.jpg
For example the method below stores the image in the image folder and generates the url of the image path.
public function uploadImage(Request $request)
{
$request->validate(['image'=>'file|image|max:5000']);
$imageProfile = new ImageProfile();
if($request->hasFile('image') && $request->file('image')->isValid())
{
$image = $request->file('image')->store('images');
$imageProfile->image_profile_url = \Storage::disk('public')->url($image);
$imageProfile->save()
}
return response()->json($imageProfile,200);
}
The code returns a Json response below
{
"id": 13,
"image_profile_url ": "http://127.0.0.1:8000/storage/images/cxlogqdI8aodERsmw74nmEx7BkxkWrnyJLMH7sFj.jpeg",
"updated_at": "2020-01-13 16:27:37",
"created_at": "2020-01-13 16:27:37",
}
Try to copy the url and test it in postman.
Visit the link to learn more about Laravel file storage
Laravel File Storage
Hope it helps.
laravel 8
Controler function
public function store(Request $request)
{
$this->validate($request, [
'site_title' => 'required',
'logo_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$input['logo_image'] = time().'.'.$request->logo_image->getClientOriginalExtension();
$request->logo_image->move(public_path('images'), $input['logo_image']);
$input['site_title'] = $request->site_title;
//dd($input);
Site_settings::create($input);
return back()->with('success','Image Uploaded successfully.');
}
blade view
<td>
<img src="{{ url('/images/').'/'.$site_settings->logo_image ?? '' }}" alt="" width="250px" height="auto">
</td>

Resources