Next.js & Contentful Site Build Tutorial #5 - Using Images from Contentful
Skills:
API Design60%
Key Takeaways
Uses images from Contentful in a Next.js application
Full Transcript
all right then so we're outputting all of this recipe data now on the home page and that's being driven by this recipe card component which is embedded into the homepage right here and this recipe card outputs a lot of information but we don't output the image yet the thumbnail so that's what i'm going to do now and to do this we're going to use the built-in image components that comes with next and this auto optimizes our image for us so let me import that up here to use it import image from next forward slash image like so now we need to pass in a source property into this image so source and we'll set that equal to something it's going to be dynamic now we can get this source from the data if we take a look at that in the console over here i'm going to take a look at the data open one of these then go to fields and then look at thumbnail right here so if we open this up we can see this also has a fields property we need to go into because the several things that we need or that we can get rather from this thumbnail so one of those is the file that's the thing that we need to output and we want the url property of that so that's the source right here and also inside this file property we have details and we have the image property with the width and height we will be using that in a minute as well so we have to kind of drill down into these different properties a fair way to get this url and these different height and width properties now also notice we don't have the https or http in front of this thing here so we have to manually add that ourselves so what i'm going to do is come over here and inside source i'm going to concatenate some parts of a string so i'm going to say https colon and that's all we need because we have the double forward slash right here and then i'm going to concatenate with that the thumbnail which we destructed right here and from the thumbnail we want the fields property because remember we have to go into fields again once we're inside thumbnail so dot fields then we want the file property then we want the url now then this is not going to work out of the box and let me show you this i'm going to save it and come over here and you can already see this big fat error and it says right here image with source and this is the source and it says must use width and height properties so let's do that first of all let's add in a width property so we'll set that equal to thumbnail dot fields dot file then we want dot details then we want dot image then dot width so we'll do the same for the height and this time it's going to be height over here and height right here so if we save this again still not going to work we're going to get a different error this time and it says we have an invalid source prop and hostname images.ctf assets.net is not configured under images in your next.config.js file so basically in order to use this image component which auto optimizes our images with images from an external domain we have to kind of white list that domain and that domain is this thing right here if we take a look if we just comment this out and take a look at the data again save this i'm going to clear all this is this working okay let me just refresh okay so if we take a look at this data again and i'm going to go into the thumbnail and into fields then we want the file and we want the url this is the thing we need to white list right here now it shows you how to do that on the next documentation all we have to do is make a next config file we export an object inside that and we whitelist our image domains like this so i'm going to copy all of this chunk right here and then i'm going to go make a config file in the root of the project because we don't have one yet so we call this next dot config dot js and i'm going to paste in this thing over here now we want to replace this with the source that we need to white whitelist so if i was to save this and then come back to the recipe card i'm going to uncomment this right here save that and come over here this is the host name we need to white list so i'm going to grab that and i'm going to come back to the config file and i want to paste it inside here like so that's all we need to do now i'm going to restart the server for this to take effect so let me cancel out of this process and let me run npm run dev again and then once that's done i'm going to come over here i'm going to hard refresh and i'm going to cross my fingers and hope this works all right voila now we can see all of these images right here and notice how they only load in as we scroll down to them that's the images lazily loading and all of that behavior is taken care of for us by this image component so now we have this template pretty much complete but it still looks crap to be honest so what i'd like to do is add some styles and we'll do that in the next video
Original Description
In this Next.js & contentful tutorial I'll show you how we can whitelist images from Contentful and use them in our Next.js application.
🐱💻 🐱💻 Course Files:
+ https://github.com/iamshaunjp/next-contentful
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Useful playlists:
+ Next.js Tutorial for Beginners - https://www.youtube.com/watch?v=A63UxsQsEbU&list=PL4cUxeGkcC9g9gP2onazU5-2M-AzA8eBw
+ Full React tutorial - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d
🐱💻 🐱💻 Other links:
+ Get VS Code - https://code.visualstudio.com/
🐱💻 🐱💻 Social Links:
Facebook - https://www.facebook.com/thenetninjauk
Twitter - https://twitter.com/thenetninjauk
Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: API Design
View skill →Related Reads
📰
📰
📰
📰
Memoization useMemo()Explained: Why React Doesn’t Need to Repeat the Same Work
Medium · JavaScript
Why Dark Mode Should Not Be a Second CSS File
Dev.to · Hasan Sarwer
Frontend-Only SaaS: The Rise of Static Utility Sites
Dev.to · yobox
Why I Built Yet Another JavaScript Date Picker
Dev.to · RollDate
🎓
Tutor Explanation
DeepCamp AI