Fetching Enterspeed-data in Next.js
For this tutorial, we assume you already have some knowledge of setting up a Next.js-project. If not, you can read the getting started guide here: https://nextjs.org/docs/getting-started.Setting up the demo project
If you don’t wish to set up the demo project, simply skip this step. Go to GitHub (https://github.com/enterspeedhq/enterspeed-demos/tree/master/umbraco-next) and clone the project. cd into the next folder and run:.env.local
For a production environment, your API key should be injected on build time.
Ways of fetching data
Fetching data from Enterspeed can be done in three different ways:- By using Handle
- By using URL
- By using ID
- The navigation is generated at build time.
- The products are generated client-side.
- Our homepage is generated server-side.
You can read more about fetching data in Next.js here: https://nextjs.org/docs/basic-features/data-fetching.
Example: enterspeed.js
How to generate content dynamically
In order to generate our content dynamically (this can be pages, blog posts, products, etc.) we need to create a component that does this for us. Luckily Next.js supports Dynamic Routes, meaning our slugs can get generated dynamically. In our demo project, we have created a file called [..slug].js. You will find it in the pages folder (you can also view it below). In the file, you’ll find the following functions:- getStaticPaths: Fetches the routes that need to get build.
- getStaticProps: Fetches the data for each route that needs to get build.
- buildPaths: Sends the navigation paths to the generateNavBarPaths function.
- generateNavBarPaths: Generates the navigation.
- generateSubMenu: Generates the submenus.
- Content: Inserts the data in a JSX-component.
[...slug].js