Nextjs 13 comes with lots of experimental features, one of them was the appDir. Let’s talk about the loading UI.
Suppose we want to show some UI, may be some cool spinner while data is fetching from the server.
Usually, such UI can be implemented by checking the loading status or wrapping the JSX within a suspense block.
In Next’s latest version they have provided a loading UI, which handle the task.
Create loading.jsx file in the route and export a default component. The loading component will render while data is fetching. No additional coding is required.
//posts/loading.jsx
export default function Loading() {
return(
<div>
Data is loading
</div>
)
}
I tried to directly using the client in app, but it doesn’t work for me. So have to create a internal API.
Under pages/api/posts create two route, one for all posts and another for dynamic / individual posts.
// index.js
import {client} from "../../../contentful";
export default async function index(req, res) {
const r = await client.getEntries();
return res.status(200).json(r)
}
import {client} from "../../../contentful";
export default async function id(req, res) {
const
{
id
} = req.query;
const r = await client.getEntry(id);
return res.status(200).json(r)
}
try to access the the API https:// localhost:3000/api/posts
In the route and components
In the Nextjs route / component, as usual we can fetch data in getStaticProps as follows and pass it to the component. The fetching will run on the top.
We nee a set of pages and dynamic route for posts and category posts. In Nextjs we can easily setup pages/route under pages directory.
404
The 404 page can be setup as a route that match all, any non existing route/page.
Under the page directory create […].js with default export
import React from 'react'
import PNF from '../Components/PNF'
export default function ErrorPage() {
return (
<PNF/>
)
}
Here PNF is beautiful animated PageNotFound component, we just created in the component directory.
Using useRouter/next/router, we can access the params of dynamic route.
Dynamic route
Layout
Our blog uses single layout, so we go with a Layout component which define Navbar and footer and a content area and implement the layout in the _app.js as follows.
If you are building content based application such as blog, may want featured editor, such as Post editor or comment box. Making such an editor with all formatting capabilities would be tiresome.
Editor frameworks and wrapper libraries help us achieve this goal with ease and peace. I don’t intended to review each of these , instead I would to list best of them and in the up coming posts we will learn using these awesome editor frameworks.
My Picks
I like block editor mostly for content management, in my react app I will choose the Editor.js . It is easy to configure and use.
Second most useful framework for content management is Drftjs , which is maintained by Facebook itself. It is a complete Editor framework featuring mention, hashtag etc and it’s flexible too.
Here is complete list of framework available for building a full featured editor in your next JS / React/Vue/Angular app