Skip to content

Commit

Permalink
fix(client): lazy load some default components
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo committed Apr 22, 2023
1 parent 5bfcca8 commit 4de545a
Show file tree
Hide file tree
Showing 10 changed files with 569 additions and 366 deletions.
7 changes: 5 additions & 2 deletions apps/client/next.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { withContentlayer } = require("next-contentlayer");
const { i18n } = require("./next-i18next.config");
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true"
});

/** @type {import('next').NextConfig} */
const nextConfig = {
const nextConfig = withBundleAnalyzer({
i18n,
images: {
domains: [
Expand All @@ -12,6 +15,6 @@ const nextConfig = {
"images.unsplash.com"
]
}
};
});

module.exports = withContentlayer(nextConfig);
16 changes: 8 additions & 8 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,30 @@
"postbuild": "next-sitemap"
},
"dependencies": {
"@hookform/resolvers": "^3.0.0",
"@hookform/resolvers": "^3.1.0",
"@next-auth/prisma-adapter": "^1.0.6",
"@next/bundle-analyzer": "^13.3.1",
"@prisma/client": "^4.13.0",
"@radix-ui/react-dropdown-menu": "^2.0.4",
"@radix-ui/react-select": "^1.2.1",
"@radix-ui/react-switch": "^1.0.2",
"@tanstack/react-query": "^4.29.1",
"@tanstack/react-query-devtools": "^4.29.1",
"@tanstack/react-query": "^4.29.3",
"@tanstack/react-query-devtools": "^4.29.3",
"argon2": "^0.30.3",
"axios": "^1.3.4",
"axios": "^1.3.6",
"clsx": "^1.2.1",
"contentlayer": "^0.3.1",
"cookies-next": "^2.1.1",
"date-fns": "^2.29.3",
"github-slugger": "^2.0.0",
"i18next": "^22.4.14",
"lucide-react": "^0.127.0",
"lucide-react": "^0.172.0",
"next": "13.3.0",
"next-auth": "^4.22.1",
"next-contentlayer": "^0.3.1",
"next-i18next": "^13.2.2",
"next-sitemap": "^4.0.6",
"next-sitemap": "^4.0.7",
"nextjs-progressbar": "^0.0.16",
"nookies": "^2.5.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.43.9",
Expand Down Expand Up @@ -66,7 +67,6 @@
"eslint-plugin-react": "7.32.2",
"eslint-plugin-react-hooks": "4.6.0",
"eslint-plugin-simple-import-sort": "10.0.0",
"identity-obj-proxy": "^3.0.0",
"postcss": "^8.4.19",
"prettier": "^2.8.4",
"prisma": "^4.13.0",
Expand Down
1 change: 1 addition & 0 deletions apps/client/src/@types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { User as PUser } from "@prisma/client";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import NextAuth, { DefaultSession } from "next-auth";

declare module "next-auth" {
Expand Down
16 changes: 15 additions & 1 deletion apps/client/src/components/Container/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { useContext } from "react";
import dynamic from "next/dynamic";

import { Main } from "@app/components/Container/Main";
import { Header } from "@app/components/Header";
import { Sidebar } from "@app/components/Sidebar";
import { SidebarContext, SidebarProvider } from "@app/context/SidebarContext";
import { useTheme } from "@app/context/ThemeContext";

import { Spinner } from "../Spinner";

const Sidebar = dynamic(
() => import("@app/components/Sidebar").then((mod) => mod.Sidebar),
{
loading: () => (
<div className="flex items-center justify-center w-64">
<Spinner className="h-20 w-20" />
</div>
),
ssr: false
}
);

interface ILayout {
children: React.ReactNode;
}
Expand Down
31 changes: 31 additions & 0 deletions apps/client/src/components/Spinner/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { SVGProps } from "react";

type SpinnerProps = SVGProps<SVGSVGElement>;

export function Spinner({ className, ...others }: SpinnerProps) {
return (
<div
className="my-auto z-0"
role="status"
>
<svg
aria-hidden="true"
className={`w-5 h-5 text-primary-100 animate-spin fill-primary-950 ${className}`}
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
{...others}
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
}
27 changes: 10 additions & 17 deletions apps/client/src/context/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {
createContext,
ReactNode,
useContext,
useLayoutEffect,
useEffect,
useState
} from "react";
import { parseCookies, setCookie } from "nookies";
import { getCookie, hasCookie, setCookie } from "cookies-next";

export type Theme = "light" | "dark";

Expand All @@ -22,26 +22,19 @@ const ThemeContext = createContext({} as ContextProps);

export function ThemeProvider({ children }: ProviderProps) {
const [theme, setTheme] = useState<Theme>("light");
const themeCookieKey = "VISUALDYNAMICS_THEME";

useLayoutEffect(() => {
const cookies = parseCookies();

const colorSchemeCookie = cookies["visualdynamics-color-scheme"];
if (colorSchemeCookie) {
setTheme(colorSchemeCookie as Theme);
useEffect(() => {
if (hasCookie(themeCookieKey)) {
setTheme(getCookie(themeCookieKey) as Theme);
}
}, []);

function toggleTheme() {
setCookie(
undefined,
"visualdynamics-color-scheme",
theme === "light" ? "dark" : "light",
{
path: "/",
maxAge: 60 * 60 * 24 * 31 * 12
}
);
setCookie(themeCookieKey, theme === "light" ? "dark" : "light", {
path: "/",
maxAge: 60 * 60 * 24 * 31 * 12
});
setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
}

Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/hocs/withSSRGuest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function withSSRGuest(
): Promise<WithAuthServerSidePropsResult> => {
const session = await getServerSession(ctx.req, ctx.res, authOptions);

if (session !== null) {
if (session) {
return {
redirect: {
destination: "/my-dynamics",
Expand Down
68 changes: 38 additions & 30 deletions apps/client/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
import React from "react";
import { QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { AppProps } from "next/app";
import { Inter } from "next/font/google";
import Script from "next/script";
import { type AppProps } from "next/app";
import dynamic from "next/dynamic";
import { SessionProvider } from "next-auth/react";
import { appWithTranslation } from "next-i18next";
import NextNProgress from "nextjs-progressbar";

import { Layout } from "@app/components/Container/Layout";
import { ThemeProvider } from "@app/context/ThemeContext";
import { queryClient } from "@app/lib/query-client";

import "@app/styles/globals.css";

const inter = Inter({
variable: "--font-inter",
weight: ["400", "900"],
preload: false
const Layout = dynamic(
() => import("@app/components/Container/Layout").then((mod) => mod.Layout),
{
ssr: true
}
);

const NextNProgress = dynamic(() => import("nextjs-progressbar"), {
ssr: false
});

const ReactQueryDevtools = dynamic(
() =>
import("@tanstack/react-query-devtools").then(
(mod) => mod.ReactQueryDevtools
),
{ ssr: false }
);

const Script = dynamic(() => import("next/script"), {
ssr: false
});

function App({ Component, pageProps: { session, ...pageProps } }: AppProps) {
if (typeof window === "undefined") React.useLayoutEffect = React.useEffect;

return (
<main className={`${inter.className}`}>
<SessionProvider session={session}>
<QueryClientProvider client={queryClient}>
<ThemeProvider>
<NextNProgress
height={5}
color="#22c55e"
options={{ showSpinner: false }}
/>
<Layout>
<Component {...pageProps} />
</Layout>
</ThemeProvider>
{process.env.NODE_ENV === "development" ? (
<ReactQueryDevtools />
) : null}
</QueryClientProvider>
</SessionProvider>

<SessionProvider session={session}>
<QueryClientProvider client={queryClient}>
<ThemeProvider>
<NextNProgress
height={5}
color="#22c55e"
options={{ showSpinner: false }}
/>
<Layout>
<Component {...pageProps} />
</Layout>
</ThemeProvider>
{process.env.NODE_ENV === "development" ? <ReactQueryDevtools /> : null}
</QueryClientProvider>
{process.env.NODE_ENV === "production" ? (
<>
{/* Google tag (gtag.js) */}
Expand All @@ -64,7 +72,7 @@ function App({ Component, pageProps: { session, ...pageProps } }: AppProps) {
</Script>
</>
) : null}
</main>
</SessionProvider>
);
}

Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
AuthFormSchemaType
} from "@app/schemas/components/auth/auth.zod";

export const getStaticProps = withSSRTranslations(withSSRGuest(), {
export const getServerSideProps = withSSRTranslations(withSSRGuest(), {
namespaces: ["signin"]
});

Expand Down

0 comments on commit 4de545a

Please sign in to comment.