Song Turbo Developer Certification - Foundations
Part 3: NextJS pages
This part deals specifically with creating couple of pages in NextJS which is rendered at build time (Static site generation).
Song Turbo uses React meta-framework "Next.js" for its web-application. Next is capable of creating normal and dynamic pages. They can be rendered at build time (SSG) or at runtime (SSR / Client).
Concepts to be grasped
- NextJS Pages
- Difference between SSG, SSR, CSR, and ISR and how NextJs supports it
Reading material
- NextJS (opens in a new tab)
- NextJS pages (opens in a new tab)
- NextJS SSG (opens in a new tab)
- NextJS Link (opens in a new tab)
- NextJS dynamic route (opens in a new tab)
- Using Typescript with NextJS (opens in a new tab)
Tasks
1. Create a new turbo-posts page/route
- The
turbo-postsroute should be accessible athttp://localhost:3000/turbo-postswhen the app in running in development mode. - The route should be rendered on build-time (SSG).
- The page should receive a list of posts as props (
ITurboPost[], required, refer references). - The page should contain a Next Head component setting page title to "Turbo Posts".
- The page should render a Heading (h1) - "Turbo Posts".
- The page should render the posts using the
TurboPostsListcomponent (created in previous task). Each list item should be a link to the/turbo-posts/[id]route (created in next step). Use a sensible empty-message. - The list of posts will be later fetched from an API but for now, we'll use a dummy list of posts, refer references.
Explain the different modes of building in NextJs, SSR, SSG, ISR etc
Explain the getStaticProps, getStaticPaths and getServerSideProps functions.
Using a getServerSideProps function put specific requirements on the server
that hosts the web app. Why?
2. Create a new turbo-posts/[id] page/route
- The
turbo-posts/[id]route should be accessible athttp://localhost:3000/turbo-posts/[id]when the app in running in development mode. - The page should be rendered on build-time (SSG).
- The static paths should be created using dummy-post list.
- The page should receive following as props:
- post: Post data (
ITurboPost, required, refer references)
- post: Post data (
- The page should contain a Next Head component setting page title to
[Post title]. - The page should render a link to
/turbo-postsroute (eg. a back-button) - The page should render the Post using the
TurboPostcomponent (created in previous tasks).
In this task we solve this by creating a file called [id].tsx. What is an
alternative way and when would you use that?
3. Link to it from the start page
- Create a button or a link so you can navigate from the start page of the app to the list of blog posts
There are two ways (at least) to do this. Either using the <Link> component or programmatically using the router. What are pros and cons of either?
Bonus: What do you get automatically when using Link that you have to do manually using the router. (hint: it increases perceived performance)
4. Commit your changes
Evaluation
To verify the page, in the terminal, run the following command:
yarn dev:web- The server should start at localhost:3000 (opens in a new tab) and the Posts page should be accessible at posts page (opens in a new tab).
- The Posts page should have the list of posts linking to their respective post page.
- The Post page should be accessible at
/turbo-posts/[id]. Eg: Post 1 (opens in a new tab). - The post page should have a link to
/turbo-postsroute - The post page should have the post name as a heading, followed by post body.