Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

159 upgrade to nextjs 13 #169

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions web/.babelrc

This file was deleted.

1 change: 1 addition & 0 deletions web/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
3 changes: 3 additions & 0 deletions web/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
compiler: {
emotion: true,
},
pageExtensions: ["page.tsx", "page.ts", "page.jsx", "page.js"],
reactStrictMode: true,
swcMinify: true,
Expand Down
1,988 changes: 1,399 additions & 589 deletions web/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"lodash": "^4.17.21",
"material-react-table": "^1.7.3",
"nanoevents": "^7.0.1",
"next": "^12.2.5",
"next": "^13.4.4",
"nextjs-google-analytics": "^2.3.0",
"pluralize": "^8.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-minimal-pie-chart": "^8.4.0",
"reactflow": "^11.6.1",
"web-worker": "^1.2.0",
Expand Down Expand Up @@ -55,7 +55,7 @@
"@typescript-eslint/parser": "^5.38.1",
"babel-loader": "^8.3.0",
"eslint": "8.21.0",
"eslint-config-next": "12.2.3",
"eslint-config-next": "^13.4.4",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-functional": "^4.4.1",
"eslint-plugin-import": "^2.26.0",
Expand Down
2 changes: 1 addition & 1 deletion web/src/common/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextPage } from "next";
import Image from "next/image";
import { ReactNode } from "react";

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

interface NavLinkProps {
href: string;
Expand Down
118 changes: 10 additions & 108 deletions web/src/common/components/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1,15 @@
// Combine Material Link with Nextjs Link
// Stolen from https://github.com/mui/material-ui/blob/a7dbc3c61013941cb331ae01fffb9620088b6cfa/examples/nextjs-with-typescript/src/Link.tsx
// Combine look of Material Link with functionality of Nextjs Link.

import MuiLink, { type LinkProps as MuiLinkProps } from "@mui/material/Link";
import { styled } from "@mui/material/styles";
import clsx from "clsx";
import NextLink, { LinkProps as NextLinkProps } from "next/link";
import { useRouter } from "next/router";
import * as React from "react";

// Add support for the sx prop for consistency with the other branches.
const Anchor = styled("a")({});

interface NextLinkComposedProps
extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, "href">,
Omit<NextLinkProps, "href" | "as" | "onClick" | "onMouseEnter" | "onTouchStart"> {
to: NextLinkProps["href"];
linkAs?: NextLinkProps["as"];
}

export const NextLinkComposed = React.forwardRef<HTMLAnchorElement, NextLinkComposedProps>(
function NextLinkComposed(props, ref) {
const { to, linkAs, replace, scroll, shallow, prefetch, locale, ...other } = props;

return (
<NextLink
href={to}
prefetch={prefetch}
as={linkAs}
replace={replace}
scroll={scroll}
shallow={shallow}
passHref
locale={locale}
>
<Anchor ref={ref} {...other} />
</NextLink>
);
}
);

export type LinkProps = {
activeClassName?: string;
as?: NextLinkProps["as"];
href: NextLinkProps["href"];
linkAs?: NextLinkProps["as"]; // Useful when the as prop is shallow by styled().
noLinkStyle?: boolean;
} & Omit<NextLinkComposedProps, "to" | "linkAs" | "href"> &
Omit<MuiLinkProps, "href">;

// A styled version of the Next.js Link component:
// https://nextjs.org/docs/api-reference/next/link
const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(function Link(props, ref) {
const {
activeClassName = "active",
as,
className: classNameProps,
href,
linkAs: linkAsProp,
locale,
noLinkStyle,
prefetch,
replace,
// role, // Link don't have roles.
scroll,
shallow,
...other
} = props;
// Tried setting NextLink as the default component of MuiLink via MUI themeing (https://stackoverflow.com/a/74419666),
// but it would hit an infinite recursion issue without making a custom component anyways.

const router = useRouter();
const pathname = typeof href === "string" ? href : href.pathname;
const className = clsx(classNameProps, {
[activeClassName]: router.pathname === pathname && activeClassName,
});
// TODO #167: for some reason, upgrading to Next 13 caused MuiLink to throw a server-client class
// mismatch error if any decoration props are set.

const isExternal =
typeof href === "string" && (href.startsWith("http") || href.startsWith("mailto:"));

if (isExternal) {
if (noLinkStyle) {
return <Anchor className={className} href={href} ref={ref} {...other} />;
}

return <MuiLink className={className} href={href} ref={ref} {...other} />;
}

const linkAs = linkAsProp ?? as;
const nextjsProps = {
to: href,
linkAs,
replace,
scroll,
shallow,
prefetch,
locale,
};

if (noLinkStyle) {
return <NextLinkComposed className={className} ref={ref} {...nextjsProps} {...other} />;
}
import MuiLink, { type LinkProps as MuiLinkProps } from "@mui/material/Link";
import NextLink from "next/link";
import { forwardRef } from "react";

return (
<MuiLink
component={NextLinkComposed}
className={className}
ref={ref}
{...nextjsProps}
{...other}
/>
);
export const Link = forwardRef<HTMLAnchorElement, MuiLinkProps>(function Link(props, ref) {
return <MuiLink component={NextLink} ref={ref} {...props} />;
});

export default Link;
8 changes: 4 additions & 4 deletions web/src/pages/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ArrowDownward } from "@mui/icons-material";
import { Box, Button, Divider, Typography } from "@mui/material";
import type { NextPage } from "next";
import Head from "next/head";
import Link from "next/link";
import NextLink from "next/link";

import { YoutubeEmbed } from "../common/components/YoutubeEmbed/YoutubeEmbed";
import { StyledBox } from "./index.style";
Expand Down Expand Up @@ -32,9 +32,9 @@ const Home: NextPage = () => {
</Typography>

<Box display="flex" margin="0.75rem">
<Link href="/solve" passHref>
<Button variant="contained">Solve</Button>
</Link>
<Button variant="contained" LinkComponent={NextLink} href="/solve">
Solve
</Button>
<Button
variant="outlined"
endIcon={<ArrowDownward />}
Expand Down
9 changes: 7 additions & 2 deletions web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
"incremental": true,
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}