Giter VIP home page Giter VIP logo

blogit's Introduction

Notes

  1. tailwind is mobile first
  2. Taking Image from outside url (remotePatterns)
const nextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "pexel.com",
      },
    ],
  },
};
  1. copyright can be get as ©
  2. A great approach to make container
    • at layout we make container so that there will be a max-w for not getting strech
  3. nextjs default behaviour is server components(many benifit like fetching)
  • usually in react we use useeffect to fetch but in next we can directly do it by declaring the function async usually used for only GET
  1. the pathname logic : if href match pathname then change colour
  2. crtl+P to shortcut open file
  3. SERVER ACTION IS USED FOR POST/PUT/DELETE
  4. so the traditional route handler(api) is used for webhook only in next
  5. in dynamic route = when we want to have access of url param we can pass params(predefine)like children to get access of it(see post [id])
  6. the loading.tsx file will auto make the suspense for you (in next.js)
  • but sometime we dont use loading.tsx but we use suspense itself with its fallback as we dont want whole file to wait while we call a api , we only want that part to be in loading and not all the static.
  1. becarefull while client and server. all the interactivitly are client and if you make the page.tsx client they you will loose the server functionalities like fetch and all. so best is to use seperate components for it and import it seperately

  2. for form and input, a good trick is make the input and buttons hfull and then at the form we can give the height like h10

  3. name is important in input for backend related fetch

form action(server action)

  • traditionally what we used to do is submit the form and create a onsubmit and call fetch for the api route
  • but in nextjs we have a new way of doing it call server action.
  • in form we use action={afuncname} that function will only run on server

creating a seperate folder for actions

  • it is recommended to put all the server action function at a one place , "we only have to use "use server" one time that way.
  • remember the function need to be async
  • then next js gives you a special prop formData which you can use to extract the detial we do it through name that we gvie to the input
const title=formData.get("title") as string;
console.log(title)
  • you can look at the network(in browser) when we submit (a fetch call will happen) nextjs made everything so abstract

remember

  • get - can be directly done at server componenet (like fetching data , findmany())
  • post/put/del - uses server action (actions.ts)

database (PRISMA ORM)

  • so we dont directly talk with db, orm does that (drizzle,prisma,mongoose)
  • we will use prisma

    npm i prisma -save-dev npx prisma init --datasource-provider sqlite we go with sqlite but you can choose any a folder will generate

  1. this is how we create schema
model Post {
  id    Int    @id @default(autoincrement())
  title String
  body  String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
  1. when ever prisma schema and all change we need to run this code

    npx prisma db push this will process the changes in db this will create a dev.db

to view the schema

npx prisma studio

instantiating prisma

  • currently you cant use prisma like

    prisma.post.create({})

  • create a lib and db.ts go to the docs docs and copy the db.ts
  • now you can import this in your file where you are using prisma variable

WRITING

//adding to db or updating for db
await prisma.post.create({
  data: {
    title,
    body,
  },
});

while deploying

make sure in package.json script you have

"postinstall": "prisma generate"

  1. there is a function in nextjs notFound() from next/navigation . used in server comp where if there is no data show this.
  2. we can modify this file by not-found.tsx

blogit's People

Contributors

tenzdelek avatar

Watchers

 avatar  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.