Gatsby Tutorial #19 - Generating Pages

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Orders the results of a query by title, date, etc. using Gatsby

Full Transcript

all right then so now we have this template file for the project details we want to tell gatsby to fetch the data for each project that we have and then we want to pump it into our template to generate a page for it so by the end of the process we have six pages one for each project that we have right here inside the projects folder all right so how do we do this well to do this we use a file called gatsby node now it doesn't exist yet so we're going to need to create it and we do that in the root folder not inside source but over here so i'm going to right click and create a new file and that is called gatsby hyphen node dot js so this gatsby node file is a file that will run a build time in a node environment so we can run certain functions inside this file to do things like fetch data and then generate pages with that data based on a template file which we already have so the first step inside this file is to create a property called create pages on the exports object so i'm going to say exports dot create pages like so now this is how we export things in a node environment it's a little bit different from es6 modules but i'm going to set this equal to an asynchronous function right now and the reason it's going to be asynchronous is because we're going to fetch the data that we need to pump into the template so it's asynchronous and it means we can use the await keyword inside this function now as an argument we get an object inside this function and we can destructure a couple of things from this object the first thing is going to be graphql so much like we used graphql and we imported it in our page components to make queries we're destructuring it from this object right here so we can use it to make queries inside this function the second thing we're going to grab from here is something called actions and actions contains a few different methods that we can use inside this function and one of those is a method to generate pages so the first thing we need to do inside this function now is to grab the data that we need to make pages for and that is all the markdown files so what i'm going to do is say right here const and we're going to destructure the data from the response here because it gives us a response object we're just destructing the data property on that object and we're going to set that equal to a weight and graph ql like so now this time this is a function so we don't just place our back ticks at the end over here we invoke the function and then we pass in whatever the query is so we do our backticks now and then we pass in the query right here now what do we need we need all the markdown files and what we'll do is grab them first of all from graphical so let me just bring this back up so we can start from scratch like so so all markdown remark we don't need to sort them but from here all we need is the slug so let me get rid of the id as well and the reason we need the slug is because when we're generating pages we want to know what is the route going to be for that page now remember if we take a look inside our projects let me close the images take a look inside one of these we see a slog property now that is going to be the route or part of the route for this page and we're going to tell gatsby that that is the case right because we need to generate as well as a page a route for that page all right so what i'm going to do is grab this query that we have right here in fact we don't need to name it so i'm going to get rid of that bit i'm going to grab it and i'm going to paste it right here like so now the query right here is not for the projects page so i'm going to get rid of that and i'll just call it project like this so we get all the markdown files as nodes we go into the front matter and we grab the slug for each one so now we have that data we can do something with it we want to generate a page for each one of those items in the data so then how do we do this well it's pretty simple first of all we want to cycle through all of the items in our data now to do that we need to say data then we want to go into all markdown remark this is much like we drilled into the objects before once we get that data back then we want the nodes right here now remember this nodes this is going to be an array right here of all of the different items that we get back and on each one of those items we're going to have access to the front matter and then on that the slug so what i want to do is cycle through these nodes and for each one we want to generate a page based on our template and in each case we want to pass in the data for that template somehow now the only thing we have is the slug right here but we can still pass in the slug to the template and then from the template we can use that slug to make another query to get the data for the project with that slug i hope that makes sense so to do this i'm going to use the for each method on this nodes array and for each node what we'll do is fire a function and then the way we create a page oops this is all wrong so let me just change this and change this back to lowercase okay so then the way we create a page is by using this actions thing right here so i'm going to take that and i'm going to use a method called create page so this is a method and we're passing an object so that gatsby knows how to create the page what template or component do we want to use to create this page what is going to be the path the route for this page and what data do we want to pass into the template for this page well we can set all of that up inside this object so first of all the path what is going to be the path for this page well i want it to be forward slash projects and then forward slash whatever the slug is going to be now i can access this log from the node so i'll say plus node which is the thing we take in to the function and then i want to access the front matter then the slug property on it so that is the route or the path for that page forward slash project forward slash whatever the slug is next we'll say what components do we want to use to generate this page now this is just going to be the template page and it needs to be an absolute path to that so in order to create this absolute path i'm going to import something so at the top oops i don't want to close that down at the top i'm going to say const path is equal to require this is how we import something in a node environment we use require and we want to require the path module now we don't need to install anything for that it's automatically built into node so don't worry about anything like that but down here we can use that path now to resolve a path so we're going to say path dot resolve and then inside here we're going to say dot forward slash to say the current directory then into source then templates then project hyphen details dot js and this is going to generate an absolute path for us not a relative one so this requires that absolute path remember okay so that's the second property the third one is going to be context and basically this is going to be what do we want to pass what variable do we want to pass in to the template when we're generating this page now i want to pass in the slug so first of all we pass in an object and then whatever variables we want to add we create a property for them so the slug in our case is going to be node dot front matter dot slug now this slug right here is passed in as a query variable for us now we've not learned about query variables yet we're going to talk about those in the next video but it means that when we're making a query down here for this template page we have access to that slug and we're going to use that query variable to make sure we get the data for whatever project we need then once we have that data we can output it right here so that's going to come in the next video but for now what i want to do is make sure that we've actually created these pages and we should just have a blank project details page using this template for each of these different routes right here so it's still going to generate those even though we're not using this inside the template page yet it's still going to generate them based on this component and we should be able to at least go to those pages okay so now what we need to do is cancel out of our process down here so ctrl c and then i'm going to run gatsby develop again and then i'm going to go over here to the browser and i'm going to try clicking on one of these things and notice now it's no longer a 404 we get a page for it and it uses this template over here project details currently it just says title and stack but once we use that query variable that we passed into it right here using the oops that should be context not context once we use that then we're going to be able to pass in the data or query the data for that project and pass that data into the template instead but at least now for each one of these we have the page set up and you can see the path for those pages at the top so this is all working so next up what we're going to do is we'll see how to use that query variable the slog right here that we pass in in our query in this template so we can get that data and actually use it in the template

Original Description

🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/gatsby-tutorial 🐱‍👤🐱‍👤 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: + Full React tutorial - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d + GraphQL tutorial - https://www.youtube.com/watch?v=Y0lDGjwRYKw&list=PL4cUxeGkcC9iK6Qhn-QLcXCXPQUov1U7f 🐱‍💻 🐱‍💻 Other links: + Gatsby docs - https://www.gatsbyjs.com/docs/ + Markdown guide - https://guides.github.com/features/mastering-markdown/ + Download Git - https://git-scm.com/downloads + 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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
React Introduction
Learn the basics of ReactJS and how to build dynamic user interfaces with this popular JavaScript library
Dev.to · Karthick (k)
📰
Why SnapDOM Beats html2canvas for DOM-to-Image Capture
Learn why SnapDOM outperforms html2canvas for DOM-to-image capture and how to use it in your frontend projects
Dev.to · Juan Martin
📰
I built 42 landing page templates as single HTML files (no npm, no build step)
Learn how to create simple landing page templates as single HTML files without relying on npm or build steps, and why this approach matters for efficient web development
Dev.to · Segcam spa
📰
Part 7B — Section 2 — React Event Handling Explained: Forms, Event Object & User Input.
Learn React event handling for forms and user input to improve your frontend skills
Medium · JavaScript
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →