Skip to content

Commit

Permalink
fix: performance fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartek532 committed Jul 26, 2024
1 parent 883f0e2 commit db7dfee
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 28 deletions.
6 changes: 3 additions & 3 deletions components/blog/featured/post/FeaturedPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import dayjs from "dayjs";
import advancedFormat from "dayjs/plugin/advancedFormat";
import customParseFormat from "dayjs/plugin/customParseFormat";
import { motion } from "framer-motion";
import Link from "next/link";
import { memo } from "react";

import { Image } from "components/common/image/Image";
import { SuperLink } from "components/common/link/SuperLink";

import styles from "./featuredPost.module.scss";

Expand All @@ -27,7 +27,7 @@ interface FeaturedPostProps {
}

export const FeaturedPost = memo<FeaturedPostProps>(({ post }) => (
<Link href={`/blog/${post.slug}`} className={styles.link}>
<SuperLink href={`/blog/${post.slug}`} className={styles.link}>
<motion.div className={styles.post} whileHover="hover">
<motion.div className={styles.image} variants={imageVariants}>
<Image
Expand All @@ -45,7 +45,7 @@ export const FeaturedPost = memo<FeaturedPostProps>(({ post }) => (
</div>
<h3 className={styles.title}>{post.title}</h3>
</motion.div>
</Link>
</SuperLink>
));

FeaturedPost.displayName = "FeaturedPost";
6 changes: 3 additions & 3 deletions components/blog/listing/thumbnail/PostThumbnail.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { motion } from "framer-motion";
import Link from "next/link";
import { memo } from "react";

import { SuperLink } from "components/common/link/SuperLink";
import { Skeleton } from "components/common/skeleton/Skeleton";
import { allCategories } from "data/categories";
import Arrow from "public/svg/right-top-arrow.svg";
Expand All @@ -15,7 +15,7 @@ interface PostThumbnailProps {
}

export const PostThumbnail = memo<PostThumbnailProps>(({ post }) => (
<Link href={`/blog/${post.slug}`}>
<SuperLink href={`/blog/${post.slug}`}>
<motion.article className={styles.post} layout>
<h2 className={styles.title}>{post.title}</h2>
<p className={styles.excerpt}>{post.excerpt}</p>
Expand All @@ -34,7 +34,7 @@ export const PostThumbnail = memo<PostThumbnailProps>(({ post }) => (
</div>
</div>
</motion.article>
</Link>
</SuperLink>
));

export const PostThumbnailSkeleton = () => <Skeleton h={17} />;
Expand Down
6 changes: 3 additions & 3 deletions components/blog/popular/PopularPosts.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import clsx from "clsx";
import Link from "next/link";
import { memo } from "react";

import { SuperLink } from "components/common/link/SuperLink";
import { Skeleton } from "components/common/skeleton/Skeleton";
import Arrow from "public/svg/right-top-arrow.svg";

Expand All @@ -22,9 +22,9 @@ export const PopularPosts = memo<PopularPostsProps>(({ posts }) => (
<span className={styles.icon}>
<Arrow />
</span>
<Link href={`/blog/${post.slug}`} target="blank">
<SuperLink href={`/blog/${post.slug}`} target="blank">
{post.title}
</Link>
</SuperLink>
</li>
))}
</ul>
Expand Down
40 changes: 40 additions & 0 deletions components/common/link/SuperLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use client";

import Link from "next/link";
import { useRouter } from "next/navigation";
import { type ComponentPropsWithRef } from "react";

export const SuperLink = (props: ComponentPropsWithRef<typeof Link>) => {
const router = useRouter();
const strHref = typeof props.href === "string" ? props.href : props.href.href;

const conditionalPrefetch = () => {
const hasFastInternet = !navigator.connection || navigator.connection.effectiveType === "4g";
if (strHref && hasFastInternet) {
void router.prefetch(strHref);
}
};

return (
<Link
{...props}
prefetch={false}
onMouseEnter={(e) => {
conditionalPrefetch();
return props.onMouseEnter?.(e);
}}
onPointerEnter={(e) => {
conditionalPrefetch();
return props.onPointerEnter?.(e);
}}
onTouchStart={(e) => {
conditionalPrefetch();
return props.onTouchStart?.(e);
}}
onFocus={(e) => {
conditionalPrefetch();
return props.onFocus?.(e);
}}
/>
);
};
7 changes: 4 additions & 3 deletions components/layout/header/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";

import { motion } from "framer-motion";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { memo } from "react";

import { SuperLink } from "components/common/link/SuperLink";

import styles from "./navbar.module.scss";

interface Route {
Expand All @@ -26,9 +27,9 @@ export const Navbar = memo<NavbarProps>(({ routes }) => {
<ul className={styles.list}>
{routes.map((route) => (
<li key={route.path} className={styles.link}>
<Link href={route.path} className={styles.label}>
<SuperLink href={route.path} className={styles.label}>
{route.label}
</Link>
</SuperLink>
{pathname === route.path || (pathname.startsWith(route.path) && route.path !== "/") ? (
<motion.div
className={styles.active}
Expand Down
6 changes: 3 additions & 3 deletions components/resource/layout/info/breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from "next/link";
import { memo, Fragment } from "react";

import { SuperLink } from "components/common/link/SuperLink";
import RightArrow from "public/svg/right-arrow.svg";

import styles from "./breadcrumbs.module.scss";
Expand All @@ -13,9 +13,9 @@ export const Breadcrumbs = memo<BreadcrumbsProps>(({ routes }) => (
<div className={styles.breadcrumbs}>
{routes.map((route, index) => (
<Fragment key={route.path}>
<Link href={route.path} className={styles.link}>
<SuperLink href={route.path} className={styles.link}>
{route.name}
</Link>
</SuperLink>
{index !== routes.length - 1 ? (
<div className={styles.separator}>
<RightArrow />
Expand Down
6 changes: 3 additions & 3 deletions components/tile/about/AboutTile.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Image from "next/image";
import Link from "next/link";

import { SuperLink } from "components/common/link/SuperLink";
import WinkingAvatar from "public/img/avatars/winking.png";
import Arrow from "public/svg/right-top-arrow.svg";

import styles from "./aboutTile.module.scss";

export const AboutTile = () => (
<Link href="/about" className={styles.tile}>
<SuperLink href="/about" className={styles.tile}>
<div className={styles.avatar}>
<Image src={WinkingAvatar} alt="winking memoji" />
</div>
Expand All @@ -22,5 +22,5 @@ export const AboutTile = () => (
<Arrow />
</span>
</div>
</Link>
</SuperLink>
);
6 changes: 3 additions & 3 deletions components/tile/latestPost/LatestPostTile.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from "next/link";
import { memo } from "react";

import { Image } from "components/common/image/Image";
import { SuperLink } from "components/common/link/SuperLink";
import Arrow from "public/svg/right-top-arrow.svg";

import styles from "./latestPostTile.module.scss";
Expand All @@ -13,7 +13,7 @@ interface LatestPostTileProps {
}

export const LatestPostTile = memo<LatestPostTileProps>(({ post }) => (
<Link href={`/blog/${post.slug}`}>
<SuperLink href={`/blog/${post.slug}`}>
<article className={styles.article}>
<div className={styles.info}>
<h2 className={styles.title}>{post.title}</h2>
Expand All @@ -38,7 +38,7 @@ export const LatestPostTile = memo<LatestPostTileProps>(({ post }) => (
/>
</div>
</article>
</Link>
</SuperLink>
));

LatestPostTile.displayName = "LatestPostTile";
6 changes: 3 additions & 3 deletions components/work/listing/thumbnail/ProjectThumbnail.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import clsx from "clsx";
import { motion } from "framer-motion";
import Link from "next/link";
import { memo } from "react";

import { Image } from "components/common/image/Image";
import { SuperLink } from "components/common/link/SuperLink";
import { Skeleton } from "components/common/skeleton/Skeleton";
import { Project } from "types";

Expand All @@ -21,7 +21,7 @@ interface ProjectThumbnailProps {
}

export const ProjectThumbnail = memo<ProjectThumbnailProps>(({ project, featured = false }) => (
<Link href={`/work/${project.slug}`}>
<SuperLink href={`/work/${project.slug}`}>
<motion.article
className={clsx(
styles.thumbnail,
Expand Down Expand Up @@ -61,7 +61,7 @@ export const ProjectThumbnail = memo<ProjectThumbnailProps>(({ project, featured
</div>
</div>
</motion.article>
</Link>
</SuperLink>
));

export const ProjectThumbnailSkeleton = () => (
Expand Down
6 changes: 3 additions & 3 deletions components/work/tile/ProjectTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import clsx from "clsx";
import { motion } from "framer-motion";
import Link from "next/link";
import { memo } from "react";

import { Image } from "components/common/image/Image";
import { SuperLink } from "components/common/link/SuperLink";
import { useTheme } from "providers/ThemeProvider";
import Arrow from "public/svg/right-top-arrow.svg";

Expand All @@ -27,7 +27,7 @@ export const ProjectTile = memo<ProjectTileProps>(({ project, mockupPosition })
};

return (
<Link href={`/work/${project.slug}`} className={styles.link}>
<SuperLink href={`/work/${project.slug}`} className={styles.link}>
<motion.div
whileHover="hover"
className={styles.project}
Expand Down Expand Up @@ -55,7 +55,7 @@ export const ProjectTile = memo<ProjectTileProps>(({ project, mockupPosition })
<Arrow />
</div>
</motion.div>
</Link>
</SuperLink>
);
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"eslint-plugin-unused-imports": "2.0.0",
"husky": "8.0.0",
"lint-staged": "12.3.2",
"network-information-types": "0.1.1",
"prettier": "2.5.1",
"rehype-stringify": "8",
"remark-parse": "8.0.3",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
{
"name": "next"
}
]
],
"types": ["./node_modules/network-information-types"]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
Expand Down

0 comments on commit db7dfee

Please sign in to comment.