Skip to content

Instantly share code, notes, and snippets.

View Wanuja97's full-sized avatar
🏠
Working from home

Wanuja Ranasinghe Wanuja97

🏠
Working from home
View GitHub Profile
@Wanuja97
Wanuja97 / SSR.tsx
Created September 22, 2023 13:58
Server Side Rendering in Next.js
import React from 'react'
import TimeCard from './TimeCard'
async function getDateTime() {
const res = await fetch(`https://worldtimeapi.org/api/ip`, { cache: 'no-store' })
return res.json()
}
export default async function SSR() {
const result = await getDateTime()
@Wanuja97
Wanuja97 / SSG.tsx
Created September 22, 2023 14:00
Static Site Generation in Next.js
import React from 'react'
import TimeCard from './TimeCard'
async function getDateTime() {
const res = await fetch(`https://worldtimeapi.org/api/ip`,{cache: 'force-cache'})
return res.json()
}
export default async function SSG() {
const result = await getDateTime()
const dateTime = result.datetime
@Wanuja97
Wanuja97 / ISR.tsx
Created September 22, 2023 14:01
Incremental Static Regeneration in Next.js
import React from 'react'
import TimeCard from './TimeCard'
async function getDateTime() {
const res = await fetch(`https://worldtimeapi.org/api/ip`, { next: { revalidate: 20 } })
return res.json()
}
export default async function ISR() {
const result = await getDateTime()
@Wanuja97
Wanuja97 / userposts.controller.ts
Last active February 24, 2024 10:31
Non-SRP Approach: UserPosts Controller
import { Controller, Get, Post, Body } from '@nestjs/common';
import { UserpostsService } from './userposts.service';
import { CreateUserDto } from 'src/users/dto/create-user.dto';
import { CreatePostDto } from 'src/posts/dto/create-post.dto';
/*
This approach is not recommended because this controller class is
responsible for handling users and posts endpoints.............
Violates Single Responsibility Principle.....................
*/
@Wanuja97
Wanuja97 / users.controller.ts
Created February 24, 2024 10:40
SRP-compliant Approach: UsersController
import { Controller, Get, Post, Body} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
// endpoint for create new user
@Post()
@Wanuja97
Wanuja97 / post.controller.ts
Created February 24, 2024 10:43
SRP-compliant Approach: PostController
import { Controller, Get, Post, Body} from '@nestjs/common';
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto/create-post.dto';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
// endpoint for create new post
@Post()