1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-21 07:21:17 -04:00

test new next/link behavior

This commit is contained in:
2022-05-17 18:31:58 -04:00
parent a2f4112e06
commit d1f0030d3c
8 changed files with 37 additions and 60 deletions

View File

@@ -35,7 +35,7 @@ const Row = styled("div", {
}, },
}); });
const Link = styled("a", { const Link = styled(NextLink, {
color: "$mediumDark", color: "$mediumDark",
textDecoration: "none", textDecoration: "none",
}); });
@@ -88,13 +88,13 @@ const Footer = ({ ...rest }: FooterProps) => (
<Row> <Row>
<div> <div>
Content{" "} Content{" "}
<NextLink href="/license/" prefetch={false} passHref={true}> <Link href="/license/" prefetch={false} title="Creative Commons Attribution 4.0 International">
<Link title="Creative Commons Attribution 4.0 International">licensed under CC-BY-4.0</Link> licensed under CC-BY-4.0
</NextLink> </Link>
,{" "} ,{" "}
<NextLink href="/previously/" prefetch={false} passHref={true}> <Link href="/previously/" prefetch={false} title="Previously on...">
<Link title="Previously on...">2001</Link> 2001
</NextLink>{" "} </Link>{" "}
{new Date(process.env.NEXT_PUBLIC_RELEASE_DATE).getUTCFullYear()}. {new Date(process.env.NEXT_PUBLIC_RELEASE_DATE).getUTCFullYear()}.
</div> </div>

View File

@@ -4,7 +4,7 @@ import { styled } from "../../lib/styles/stitches.config";
import type { ComponentProps } from "react"; import type { ComponentProps } from "react";
import type { LinkProps as NextLinkProps } from "next/link"; import type { LinkProps as NextLinkProps } from "next/link";
const FancyLink = styled("a", { const StyledLink = styled(NextLink, {
color: "$link", color: "$link",
textDecoration: "none", textDecoration: "none",
@@ -34,29 +34,20 @@ const FancyLink = styled("a", {
}, },
}); });
export type LinkProps = Omit<ComponentProps<typeof FancyLink>, "href"> & export type LinkProps = Omit<ComponentProps<typeof StyledLink>, "href"> &
NextLinkProps & { NextLinkProps & {
underline?: boolean; underline?: boolean;
forceNewWindow?: boolean; forceNewWindow?: boolean;
}; };
const Link = ({ const Link = ({ href, prefetch = false, target, rel, underline = true, forceNewWindow, ...rest }: LinkProps) => {
href,
prefetch = false,
passHref = true,
target,
rel,
underline = true,
forceNewWindow,
...rest
}: LinkProps) => {
// this component auto-detects whether or not we should use a normal HTML anchor externally or next/link internally, // this component auto-detects whether or not we should use a normal HTML anchor externally or next/link internally,
// can be overridden with `forceNewWindow={true}`. // can be overridden with `forceNewWindow={true}`.
const isExternal = isAbsoluteUrl(href.toString()); const isExternal = isAbsoluteUrl(href.toString());
if (forceNewWindow || isExternal) { if (forceNewWindow || isExternal) {
return ( return (
<FancyLink <StyledLink
href={href.toString()} href={href.toString()}
target={target ?? "_blank"} target={target ?? "_blank"}
rel={[rel, "noopener", isExternal ? "noreferrer" : ""].join(" ").trim()} rel={[rel, "noopener", isExternal ? "noreferrer" : ""].join(" ").trim()}
@@ -64,13 +55,9 @@ const Link = ({
{...rest} {...rest}
/> />
); );
} else {
return (
<NextLink href={href} prefetch={prefetch} passHref={passHref}>
<FancyLink target={target} rel={rel} underline={underline} {...rest} />
</NextLink>
);
} }
return <StyledLink {...{ href, prefetch, target, rel, underline, ...rest }} />;
}; };
export default Link; export default Link;

View File

@@ -1,7 +1,7 @@
import NextLink from "next/link"; import NextLink from "next/link";
import { styled } from "../../lib/styles/stitches.config"; import { styled } from "../../lib/styles/stitches.config";
const Link = styled("a", { const Link = styled(NextLink, {
display: "inline-block", display: "inline-block",
color: "$mediumDark", color: "$mediumDark",
textDecoration: "none", textDecoration: "none",
@@ -67,11 +67,9 @@ const MenuItem = ({ icon: ItemIcon, href, text, current, className }: MenuItemPr
// allow both navigational links and/or other interactive react components (e.g. the theme toggle) // allow both navigational links and/or other interactive react components (e.g. the theme toggle)
if (href) { if (href) {
return ( return (
<NextLink href={href} prefetch={false} passHref={true}> <Link href={href} prefetch={false} className={className} current={current} title={text} aria-label={text}>
<Link className={className} current={current} title={text} aria-label={text}> {linkContent}
{linkContent} </Link>
</Link>
</NextLink>
); );
} else { } else {
return linkContent; return linkContent;

View File

@@ -22,7 +22,7 @@ const MetaItem = styled("div", {
whiteSpace: "nowrap", whiteSpace: "nowrap",
}); });
const MetaLink = styled("a", { const MetaLink = styled(NextLink, {
color: "inherit", color: "inherit",
textDecoration: "none", textDecoration: "none",
}); });
@@ -62,20 +62,17 @@ const NoteMeta = ({ slug, date, title, htmlTitle, tags = [] }: NoteMetaProps) =>
<> <>
<Wrapper> <Wrapper>
<MetaItem> <MetaItem>
<NextLink <MetaLink
href={{ href={{
pathname: "/notes/[slug]/", pathname: "/notes/[slug]/",
query: { slug }, query: { slug },
}} }}
passHref={true}
> >
<MetaLink> <span>
<span> <Icon as={DateIcon} />
<Icon as={DateIcon} /> </span>
</span> <Time date={date} format="MMMM D, YYYY" />
<Time date={date} format="MMMM D, YYYY" /> </MetaLink>
</MetaLink>
</NextLink>
</MetaItem> </MetaItem>
{tags.length > 0 && ( {tags.length > 0 && (

View File

@@ -18,7 +18,7 @@ const Title = styled("h1", {
}, },
}); });
const Link = styled("a", { const Link = styled(NextLink, {
color: "$text", color: "$text",
textDecoration: "none", textDecoration: "none",
}); });
@@ -27,15 +27,13 @@ export type NoteTitleProps = Pick<NoteFrontMatter, "slug" | "htmlTitle"> & Compo
const NoteTitle = ({ slug, htmlTitle, ...rest }: NoteTitleProps) => ( const NoteTitle = ({ slug, htmlTitle, ...rest }: NoteTitleProps) => (
<Title {...rest}> <Title {...rest}>
<NextLink <Link
href={{ href={{
pathname: "/notes/[slug]/", pathname: "/notes/[slug]/",
query: { slug }, query: { slug },
}} }}
passHref={true} dangerouslySetInnerHTML={{ __html: htmlTitle }}
> />
<Link dangerouslySetInnerHTML={{ __html: htmlTitle }} />
</NextLink>
</Title> </Title>
); );

View File

@@ -14,7 +14,7 @@ const Title = styled("h1", {
}, },
}); });
const Link = styled("a", { const Link = styled(NextLink, {
color: "$text", color: "$text",
textDecoration: "none", textDecoration: "none",
}); });
@@ -26,9 +26,7 @@ const PageTitle = ({ children, ...rest }: PageTitleProps) => {
return ( return (
<Title {...rest}> <Title {...rest}>
<NextLink href={router.pathname} passHref={true}> <Link href={router.pathname}>{children}</Link>
<Link>{children}</Link>
</NextLink>
</Title> </Title>
); );
}; };

View File

@@ -20,7 +20,7 @@ const Image = styled(NextImage, {
}, },
}); });
const Link = styled("a", { const Link = styled(NextLink, {
display: "inline-flex", display: "inline-flex",
alignItems: "center", alignItems: "center",
color: "$mediumDark", color: "$mediumDark",
@@ -48,15 +48,13 @@ const Name = styled("span", {
}, },
}); });
export type SelfieProps = ComponentProps<typeof Link>; export type SelfieProps = Omit<ComponentProps<typeof Link>, "href">;
const Selfie = ({ ...rest }: SelfieProps) => ( const Selfie = ({ ...rest }: SelfieProps) => (
<NextLink href="/" passHref={true}> <Link href="/" rel="author" title={authorName} {...rest}>
<Link rel="author" title={authorName} {...rest}> <Image src={selfieJpg} alt={`Photo of ${authorName}`} width={50} height={50} quality={60} layout="raw" priority />
<Image src={selfieJpg} alt={`Photo of ${authorName}`} width={50} height={50} quality={60} layout="raw" priority /> <Name>{authorName}</Name>
<Name>{authorName}</Name> </Link>
</Link>
</NextLink>
); );
export default Selfie; export default Selfie;

View File

@@ -43,6 +43,7 @@ module.exports = (phase, { defaultConfig }) => {
// allow forgoing the mess of `<span>`s around statically imported images // allow forgoing the mess of `<span>`s around statically imported images
layoutRaw: true, layoutRaw: true,
}, },
newNextLinkBehavior: true, // https://github.com/vercel/next.js/pull/36436
}, },
webpack: (config) => { webpack: (config) => {
// this lets us statically import webfonts like we would images, allowing cool things like preloading them // this lets us statically import webfonts like we would images, allowing cool things like preloading them