Code Monkey home page Code Monkey logo

nextjs-blog's Introduction

Update 1/24

  • Updated UI

screenshots

screen


Update 12/23

  • Used MDX instead of MD for blog content
  • Implemented Pagination

This is developed from the starter template for Learn Next.js.

Convert to Typescript - main branch

  1. Create tsconfig.json under root
  2. Run dev server: npm run dev Next will install required packages for typescript usage
  3. Convert app to use Typescript

Starter code(javascript) - see starter branch

  1. Create next app
npx create-next-app@latest nextjs-blog --use-npm --example "https://github.com/vercel/next-learn/tree/master/basics/learn-starter"
  1. Navigate pages
    Created pages/posts/first-post.js file (need to export default component)
    Route: /posts/first-post
  • Code spliting
    Each page only loads what’s necessary for that page.
  • Client-side navigation
    • Page transition happens using JavaScript
    • Create routes as files under pages and use the built-in Link component
  • Prefetching (in production)
    Whenever Link components appear in the browser’s viewport, Next.js automatically prefetches the code for the linked page in the background.
  1. Assets, Metadata, and CSS
  • Image component
    Added image under public directory
    • Image resizing and optimization (on-demand)
    • Lazy load
    • Avoid Cumulative Layout Shift
  • Head component - modify <head> of a page
    Added Head component to first-post
  • Script Component - Add third-party JS
    Exampe:
    <Script
          src="https://connect.facebook.net/en_US/sdk.js"
          strategy="lazyOnload"
          onLoad={() => {
            console.log(`script loaded correctly, window.FB has been populated`)
          }}
        />
    
  • CSS modules - component level style
    Added layout.js
    • Generates unique class names
    • Code splitting ensures minimal amount of css is loaded for each page
  • Gloabal styles
    • Added pages/_app.js - need to restart server
    • import global styles in _app.js
  • Styling tips
    • toggle classes - clsx library
    • customize PostCSS config
    • Add Tailwind CSS
    • Using Sass
  1. Pre-rendering and data fetching
  • Pre-rendering
  • static generation - html generated at build time and reused for each request(except for dev mode)
  • server side rendering - html generated on each request
    • can be used when data needs to be up to date with each request
  • data fetching
    • This example used file system to store data in markdown files
    • create utility functions(in /lib directory) to parse data files
    • can also fetch data from other sources, like external API, or query DB directly
  • Implement getStaticProps - pre-render with data
    • this method returns props that pass to the exported component
    • this method only runs on server-side
    • run at build time(in prod)
  • fetch data at request time - server side rendering
    • export getServerSideProps
  • In the case of not needing to pre-render data (eg: Private, user-specific pages where SEO is not relevant)
    • client side rendering
    • SWR - react hook for data fetching
  1. Dynamic routes
  • create [id].js inside pages/posts
    • this is like accessing values of the object returned by getStaticPaths
  • implement getStaticPaths method - returns an array of possible id values
    • need to return an array of objects with params key, and contain an object with the file name key, in this case, it is id.
    • can fetch data from external source
  • implement getStaticProps to fetch data with params id
  • catch all route /posts/[...id].js - id value will be an array
  • custom 404 page pages/404.js - statically generated at build time
  1. API routes
  • Do Not Fetch an API Route from getStaticProps or getStaticPaths
    • write your server-side code directly in getStaticProps or getStaticPaths (or call a helper function)
  • preview/draft mode - bypass static generation
    draft mode
  • dynamic API route
  1. Deploy app

nextjs-blog's People

Contributors

zhna123 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.