Skip to main content

Fetching data in the frontend

Now it’s time to get the data into the frontend. If you wish you can copy the demo project below, or simply follow along.

Setting up the demo project

This project uses Netlify functions to securely fetch data from Enterspeed. If you don’t wish to use Netlify functions simply move the enterspeed.js to another folder and remove the netlify-cli dependency.
Go to our GitHub-repo (https://github.com/enterspeedhq/enterspeed-demos/tree/master/vanilla-js) and clone the project. cd into the vanilla-js folder and run:
Create a file called .env.local and insert your environment API key generated in the Enterspeed-app under Environments like this:
.env.local
For a production environment, your API key should be injected on build time or via a serverless function.

Ways of fetching data

Fetching data from Enterspeed can be done in three different ways:
  • By using Handle
  • By using URL
  • By using ID
On our demo site, we’ve used handle and URL. Handle and ID are good ways of fetching data for “non-site-specific data”, e.g. the navigation. In this demo, we’re using the handle method to fetch the blog list. We’re using the URL method to fetch all of the individual blog posts. When the data is fetched you can choose to either generate it client-side, server-side, or at build time. In this demo we’re generating it all client-side. Data can be fetched using the Fetch API or a library like Axios. In our demo, we have made a component that fetches the data. You’ll find it in the project under “netlify/functions/enterspeed.js”. The components look like this:
enterspeed.js

Creating the website

Demo website example

Setting up the HTML

We start by setting up all the HTML that we need. We’re including two libraries:
The HTML

Initializing the router

Now that we got the structure in place, let’s look at the JavaScript needed. First, let’s Initialize our router, which takes one mandatory argument - the root path of our site.
Initializing router

Creating the renderHomepage function

Next, let’s create a function that renders our homepage.
renderHomePage function
The first thing we do in the function is to fetch content via our enterspeed.js function. We include the query ?handle=blogList since we want to map out the blog posts in a nice grid overview. The content is stored in data.data.views.blogList.content, which we assign to a const called posts.
renderHomePage function -- fetch
Next, we iterate over each of these posts. We create the HTML for the “card” and map the data appropriately. Then we take each card and add it to the posts HTML element:
The posts HTML element
renderHomePage function -- mapping posts
Notice that we have inserted data-navigo in the a tag. This is so Navigo attaches a click handler which fires the router’s navigate method.
Finally, to get Navigo to recognize these links we just created, we call the updatePageLinks method. When any of the link which has the data-navigo attribute gets clicked, it should call the renderPostPage function, which we will create next, and give it the post link as an argument.
renderHomePage function -- updatePageLinks
Now, before moving on to the renderPostPage function, let’s remember to initialize our renderHomepage function by calling it.
Initializing renderHomePage()

Creating the renderPostPage function

renderPostPage function
Just like before, we fetch the content via our enterspeed.js function. We use url= instead of handle=, since we set the route to url in our blog posts schema. The content here is stored in data.data.route.content, which we assign to a const called post. We then create the HTML for the “post” and map the data appropriately. Lastly, we take our postPage and add it to our main HTML element:
The main HTML element
…as well as the article itself and add to the article HTML element:
The article HTML element
Now when navigating to one of the blog posts, we will have a nice, simple article page. Demo website blog post example The finished results should look something like this:
The complete index.html