Skip to content

Instantly share code, notes, and snippets.

@yogain123
Last active February 24, 2024 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogain123/3fb9fc6c035f1f540e11c33d442c683a to your computer and use it in GitHub Desktop.
Save yogain123/3fb9fc6c035f1f540e11c33d442c683a to your computer and use it in GitHub Desktop.
nextjs
@yogain123
Copy link
Author

yogain123 commented Oct 22, 2022

fallback key

Screen Shot 2022-10-22 at 6 28 22 PM


Screen Shot 2022-10-22 at 6 29 18 PM


Screen Shot 2022-10-22 at 6 29 48 PM


Screen Shot 2022-10-22 at 6 31 31 PM


Screen Shot 2022-10-22 at 6 32 59 PM


Screen Shot 2022-10-22 at 6 36 55 PM


Screen Shot 2022-10-22 at 6 39 49 PM


Screen Shot 2022-10-22 at 6 43 40 PM


Screen Shot 2022-10-22 at 6 43 35 PM



fallback "blocking" is same as fallback:false , only differenece is that instead of showing fallback page we will not see any new content on UI when page is generated on the server


Screen Shot 2022-10-22 at 6 44 10 PM


Screen Shot 2022-10-22 at 6 45 25 PM

@yogain123
Copy link
Author

Incremental Static Regeneration

Screen Shot 2022-10-22 at 6 46 36 PM
Screen Shot 2022-10-22 at 6 47 05 PM
Screen Shot 2022-10-22 at 6 48 38 PM


Screen Shot 2022-10-22 at 6 59 53 PM

@yogain123
Copy link
Author

Inspecting ISR build

Screen Shot 2022-10-22 at 7 07 41 PM
Screen Shot 2022-10-22 at 7 11 42 PM

@yogain123
Copy link
Author

yogain123 commented Oct 22, 2022

Server Side rendering

Screen Shot 2022-10-22 at 7 13 42 PM


Screen Shot 2022-10-22 at 7 14 41 PM


Screen Shot 2022-10-22 at 7 15 42 PM


Screen Shot 2022-10-22 at 7 15 56 PM


Screen Shot 2022-10-22 at 7 17 04 PM


Screen Shot 2022-10-22 at 7 18 30 PM


Screen Shot 2022-10-22 at 7 18 41 PM


Screen Shot 2022-10-22 at 7 19 36 PM


Screen Shot 2022-10-22 at 7 19 56 PM

@yogain123
Copy link
Author

SSR with dynamic parameters

Screen Shot 2022-10-22 at 7 22 07 PM

@yogain123
Copy link
Author

getServerSideProps context

one of the limitation of static generation is that you don't get access of incoming request (request, cookie, header etc), that prevent you from fetching data that is user specific
but SSR will have access to incoming request

Screen Shot 2022-10-22 at 7 26 25 PM

@yogain123
Copy link
Author

yogain123 commented Oct 22, 2022

Client side data fetching

Screen Shot 2022-10-22 at 7 28 00 PM


Screen Shot 2022-10-22 at 7 31 02 PM


Screen Shot 2022-10-22 at 7 31 10 PM

@yogain123
Copy link
Author

yogain123 commented Oct 22, 2022

Pre-rendering + client side data fetching

Screen Shot 2022-10-22 at 7 41 06 PM


Screen Shot 2022-10-22 at 7 41 14 PM


if you want to bookmark the page:

Screen Shot 2022-10-22 at 7 42 14 PM


Screen Shot 2022-10-22 at 7 42 05 PM

@yogain123
Copy link
Author

yogain123 commented Oct 22, 2022

Pre rendering and Data fetching summary

Screen Shot 2022-10-22 at 7 44 29 PM


Screen Shot 2022-10-22 at 7 45 21 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

API Routes

Screen Shot 2022-10-23 at 3 11 41 PM

API routes are also file/folder baised


Screen Shot 2022-10-23 at 3 55 48 PM


Screen Shot 2022-10-23 at 3 56 13 PM


Screen Shot 2022-10-23 at 3 56 21 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

dynamic api routes

Screen Shot 2022-10-23 at 3 59 45 PM

Catch all api routes

Screen Shot 2022-10-23 at 4 01 26 PM

@yogain123
Copy link
Author

API route summary

Screen Shot 2022-10-23 at 4 08 47 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

Styling section

Screen Shot 2022-10-23 at 4 11 54 PM


Screen Shot 2022-10-23 at 4 17 05 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

App Layout

Screen Shot 2022-10-23 at 4 21 13 PM

but if you want only Footer for some page (let say About page) then:

Screen Shot 2022-10-23 at 4 22 04 PM


Screen Shot 2022-10-23 at 4 22 36 PM

@yogain123
Copy link
Author

Head component

Screen Shot 2022-10-23 at 4 26 36 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

Image component

next js optimize images as well

Screen Shot 2022-10-23 at 4 32 12 PM


Screen Shot 2022-10-23 at 4 32 24 PM

@yogain123
Copy link
Author

Absolute import

Screen Shot 2022-10-23 at 6 08 02 PM


Screen Shot 2022-10-23 at 6 08 08 PM


module paths

Screen Shot 2022-10-23 at 6 08 40 PM


Screen Shot 2022-10-23 at 6 08 47 PM

@yogain123
Copy link
Author

static html export

Screen Shot 2022-10-23 at 6 37 25 PM


Screen Shot 2022-10-23 at 6 37 53 PM

"next export" will generate "out" dir with all static html in it, then we can put it in CDN (static server)
for this we cannot use next/image and fallback should always be "false"

@yogain123
Copy link
Author

typescript support

tsconfig.json is the config file for typescript

Screen Shot 2022-10-23 at 6 43 03 PM


Screen Shot 2022-10-23 at 6 43 22 PM

@yogain123
Copy link
Author

redirect

maybe that page is under maintainenece or maybe it is permanent shifted to other page (but user might have bookmarked old page)

Screen Shot 2022-10-23 at 6 50 37 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

AUTHENTICATION IN NEXTJS

Screen Shot 2022-10-23 at 7 47 17 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

signin and signout from client

Screen Shot 2022-10-23 at 8 27 04 PM


Screen Shot 2022-10-23 at 8 27 13 PM


actually <Link href="" /> is not needed, you can use either Link or signIn function, or both
you can check cookie, you will see next auth session token
if you pass signIn("github") , github string inside then if you are already loggedin in github then it will auto login you on click of signin

@yogain123
Copy link
Author

yogain123 commented Oct 25, 2022

Client side authentication

Screen Shot 2022-10-25 at 3 08 58 PM


Screen Shot 2022-10-25 at 3 09 09 PM


Screen Shot 2022-10-25 at 3 09 16 PM



above code is deplricated, new one is

in header.jsx

import classes from "../../../styles/Header.module.css";
import { signIn, signOut, useSession } from "next-auth/react";

function Header() {
  const sessionDetails = useSession();
  const { data, status } = sessionDetails;
  console.log({ data });
  return (
    <div className={classes.header}>
      This is header
      <hr />
      {status === "authenticated" && status !== "loading" && (
        <button
          onClick={() => {
            signIn("github");
          }}
          type="button"
        >
          SIGN OUT
        </button>
      )}
      {status === "unauthenticated" && status !== "loading" && (
        <button
          onClick={() => {
            signOut();
          }}
          type="button"
        >
          SIGN IN
        </button>
      )}
      {status === "loading" && <div>Loading...</div>}
    </div>
  );
}

export default Header;

in _app.js

import { SessionProvider } from "next-auth/react";
import "../styles/globals.css";

function MyApp({ Component, pageProps }) {
  return (
    <SessionProvider>
      <Component {...pageProps} />
    </SessionProvider>
  );
}

export default MyApp;



in [...nextauth].js

import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";

export default NextAuth({
  providers: [
    GithubProvider({
      clientId: process.env.GITHUB_ID,
      clientSecret: process.env.GITHUB_SECRET,
    }),
  ],
});



Screen Shot 2022-10-25 at 3 40 21 PM

@yogain123
Copy link
Author

Securing pages client side

Screen Shot 2022-10-25 at 3 39 09 PM

@yogain123
Copy link
Author

yogain123 commented Oct 25, 2022

Server side authentication

authentication inside getServerSideProps function

Screen Shot 2022-10-25 at 3 44 27 PM


Screen Shot 2022-10-25 at 3 44 42 PM

@yogain123
Copy link
Author

Securing pages server side

Screen Shot 2022-10-25 at 3 46 09 PM

@yogain123
Copy link
Author

securing api routes

Screen Shot 2022-10-25 at 3 47 02 PM

@yogain123
Copy link
Author

yogain123 commented Oct 25, 2022

@yogain123
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment