Skip to content

Instantly share code, notes, and snippets.

View sand97's full-sized avatar

Bruce Guenkam sand97

View GitHub Profile
@sand97
sand97 / server.js
Last active January 3, 2022 00:31
Custom Next.js server.js with purge cache handler.
// server.js
const { createServer } = require('http');
const next = require('next');
const { promises, existsSync } = require('fs');
const dev = (process.env.NODE_ENV || '').indexOf('production') === -1;
const app = next({ dev });
const handle = app.getRequestHandler();
@sand97
sand97 / gist:ad66d3573a3a1ad5e3a1ec0473db50ee
Created January 3, 2022 00:28
Package.json script for custom server of Next.js application
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
}
@sand97
sand97 / cats-fact.js
Created January 3, 2022 00:51
Next.js static file optimization
import { useRouter } from 'next/router'
import Head from 'next/head'
export default function Fact({ fact }) {
const router = useRouter()
return (
<div style={{ textAlign: 'center' }}>
<h1>A fact about cat</h1>
<br/>
<p>{fact}</p>
@sand97
sand97 / product.js
Created January 7, 2022 08:35
nextjs get server side props example
const Product = (props) => {
return <div>
{ props.data.map(p => <div key={p.id}>{p.name}</div>) }
</div>
}
export const getServerSideProps = async (context) => {
const {name} = context.query
//you can fetch data from your api like this
// const res = await
@sand97
sand97 / link_exemple.js
Last active January 7, 2022 08:41
Nextjs Link exampple
@sand97
sand97 / link_shame.js
Last active January 7, 2022 08:40
Link nextjs
@sand97
sand97 / use_effect_refirect.js
Created January 7, 2022 08:40
Next redirection on client side
//...
const {name} = router.query;
React.useEffect(() => {
router.replace(`/products/${name}`);
})
//...
@sand97
sand97 / usestate.js
Created January 10, 2022 16:08
useState example
//...
const [steep, setSteep] = useState<number>(0);
//...
@sand97
sand97 / state_up.js
Last active January 10, 2022 16:49
Handle state up example
//inside the parent of carousel Dialog
const [dialogOpen, setDialogOpen] = useState<number>(0);
//inside a carousel Dialog
const [steep, setSteep] = useState<number>(0);
@sand97
sand97 / url.js
Last active January 10, 2022 16:47
state inside urls
import {useRouter} from "next/router";
//...
const router = useRouter();
const {router_steep} = router.query
const steep = (isNaN(+router_steep) || +router_steep < 0) ? 0
: +router_steep;