1. Next js setup
a. Create a nextJS application using “npx create-next-app@latest” and then you need to answer the following
b. Once you’ve answered the prompts, a new project will be created with the correct configuration depending on your answers.
c. Note: For more information regarding nextJS you can refer to the official documentation.
https://nextjs.org/docs/pages/api-reference/create-next-app
d. To start the Next Js application we can run the following commands.
e. Then, Open http://localhost:3000 with your browser to see the result.
2. Create a Strapi application
a. To start the strapi application in development mode, we need to cd to the project directory and then
b. This will start the strapi application development server.
c. The admin panel of Strapi runs at http://localhost:1337/admin. This is where we will spend most of your time creating and updating content.
d. Once we are logged in to the Strapi dashboard we can start building collections from the content-type-builder. A collection literally is a schema for a table in the database.
e. When we create a collection in the content-type-builder it will automatically create some crud APIs(get, post, put, delete) for the collection with all the possible filters and pagination.
f. There is a swagger documentation plugin, we can install inside our Strapi application and it will give us a documentation section in the admin dashboard. This swagger documentation will contain the list of APIs that are available for the Strapi application.
g. For more information regarding the Strapi setup we can view the documentation of Strapi.
https://docs.strapi.io/dev-docs/quick-start
3. How to use Next JS for building a static website.
a. Export to static website:- First we need to build the application
b. This allows you to export your Next.js application to static HTML, which can be run standalone without the need for a Node.js server. All the static HTML files will be generated inside the out directory at the root of the application.
c. Dynamic page generation is possible in NextJS, i.e. we can generate static pages based on the data that we are getting from the backend via API call. But we should be able to pre-render a page at build time.
d. To make network calls in the Next application during the build we can use getStaticProps, we can use to make the network call during the build time itself and we can pass the data to the page component as params and we can decide which custom components to render inside the page based on the data that we got from the backend.
Example: -
export async function getStaticProps({ params }: any) {
const siteConfig = await getSiteConfig();
return {
props: {configs: siteConfig },
};
}
export default function Home({ config}: any) {
const router = useRouter();
if (!router.isFallback && !config) {
return <ErrorPage statusCode={404} />;
}
// We can use the config here inside the
return (
// UI code here ..
)}4. How to do dynamic page generation
In the above example, we learned how to get data during build so that when we export the code for a static build it will have the required values for the screen.
// This is the method to get the data from the backend during the build time. Returns props.
export async function getStaticProps({ params }: any) {
const config = await getSiteConfig(params.locale);
return {
props: {configs: config },
};
}
// This is the method that gets the locale data from the backend and returns paths,
// the list of paths to be created dynamically.
export async function getStaticPaths() {
const localeConfig = await getLocaleConfig();
return {
paths: localeConfig?.map((item) => `/dynamic/${item.attributes.locale}`) || [],
fallback: false,
};
}
export default function Page({ config, pages }: any) {
const router = useRouter();
if (!router.isFallback && !config[0]) {
return <ErrorPage statusCode={404} />;
}
const defalut: Config = config[0];
return (
// UI code for the
)
}In the above example, we have used the getStaticPaths method to define the dynamic routes. In this method, we are getting all the locale information from the backend and then we are returning an object containing “paths”, “paths“ contains an array of the paths.
Note: Here in the example we can use APIs created on Strapi and we can have a dynamic page and content setup inside our next application. As Strapi is a headless CMS, we can edit our website config and contents from Strapi dashboard.
Share: