🚑️ Add <Image /> + `next export` improvements

The <Image /> component makes prepends the basePath to the image src.
This makes all <img /> work with `next export` correctly.
This commit is contained in:
Aditya Thakral 2021-05-20 23:30:52 -04:00
parent 4393037e94
commit 8a9935ba27
7 changed files with 36 additions and 19 deletions

View File

@ -2,7 +2,6 @@
"recommendations": [ "recommendations": [
"dbaeumer.vscode-eslint", "dbaeumer.vscode-eslint",
"humao.rest-client", "humao.rest-client",
"silvenon.mdx", "silvenon.mdx"
"xyc.vscode-mdx-preview"
] ]
} }

View File

@ -1,5 +1,6 @@
import React, { ReactNode } from "react"; import React, { ReactNode } from "react";
// import { Button } from "./Button"; // import { Button } from "./Button";
import { Image } from "./Image";
import { EventSetting } from "./EventSetting"; import { EventSetting } from "./EventSetting";
import styles from "./EventDescriptionCard.module.css"; import styles from "./EventDescriptionCard.module.css";
@ -37,7 +38,7 @@ const links = {
export function EventDescriptionCard(props: Props) { export function EventDescriptionCard(props: Props) {
return ( return (
<article className={styles.card}> <article className={styles.card}>
<img className={styles.poster} src={props.poster} alt={props.name} /> <Image className={styles.poster} src={props.poster} alt={props.name} />
<div className={styles.details}> <div className={styles.details}>
<h3 className={styles.name}>{props.name}</h3> <h3 className={styles.name}>{props.name}</h3>
@ -60,7 +61,7 @@ export function EventDescriptionCard(props: Props) {
)} )}
{props.online && ( {props.online && (
<a target="_blank" href={links[props.location]} rel="noreferrer"> <a target="_blank" href={links[props.location]} rel="noreferrer">
<img <Image
className={styles.logo} className={styles.logo}
alt={props.location} alt={props.location}
src={"logos/" + props.location + ".png"} src={"logos/" + props.location + ".png"}

16
components/Image.tsx Normal file
View File

@ -0,0 +1,16 @@
import React, { ImgHTMLAttributes } from "react";
export function Image(props: ImgHTMLAttributes<HTMLImageElement>) {
const { src: relativeSrc = "" } = props;
let absoluteSrc = process.env.BASE_PATH ?? "/";
if (absoluteSrc.endsWith("/") && relativeSrc.startsWith("/")) {
absoluteSrc += relativeSrc.slice(1);
} else if (absoluteSrc.endsWith("/") || relativeSrc.startsWith("/")) {
absoluteSrc += relativeSrc;
} else {
absoluteSrc += "/" + relativeSrc;
}
return <img {...props} src={absoluteSrc} />;
}

View File

@ -29,6 +29,10 @@
cursor: pointer; cursor: pointer;
} }
.logo img {
width: 96px;
}
.navMenu { .navMenu {
display: inline-flex; display: inline-flex;
flex-direction: row; flex-direction: row;

View File

@ -1,7 +1,7 @@
import React from "react"; import React from "react";
import Image from "next/image";
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { Image } from "./Image";
import styles from "./Navbar.module.css"; import styles from "./Navbar.module.css";
interface NavLink { interface NavLink {
@ -98,12 +98,7 @@ export function Navbar() {
<div className={styles.navContent}> <div className={styles.navContent}>
<Link href="/"> <Link href="/">
<a className={styles.logo}> <a className={styles.logo}>
<Image <Image src="/images/logo-icon.png" alt="CSC Logo" />
src="/images/logo-icon.png"
width={96}
height={42}
alt="CSC Logo"
/>
</a> </a>
</Link> </Link>
<ul className={styles.navMenu}> <ul className={styles.navMenu}>

View File

@ -83,7 +83,7 @@ export function EventDescriptionCardDemo() {
date={date} date={date}
online={true} online={true}
location="Twitch" location="Twitch"
poster="images/playground/intro-ootb.jpg" poster="/images/playground/intro-ootb.jpg"
registerLink="/" registerLink="/"
> >
<p>{short}</p> <p>{short}</p>
@ -93,7 +93,7 @@ export function EventDescriptionCardDemo() {
date={date} date={date}
online={true} online={true}
location="Twitch" location="Twitch"
poster="images/playground/alt-tab.jpg" poster="/images/playground/alt-tab.jpg"
registerLink="/" registerLink="/"
> >
<p>{short}</p> <p>{short}</p>
@ -103,7 +103,7 @@ export function EventDescriptionCardDemo() {
date={date} date={date}
online={true} online={true}
location="Twitch" location="Twitch"
poster="images/playground/intro-ootb.jpg" poster="/images/playground/intro-ootb.jpg"
registerLink="/" registerLink="/"
> >
<p>{short}</p> <p>{short}</p>

View File

@ -1,7 +1,9 @@
const withMDX = require('@next/mdx')({ const withMDX = require("@next/mdx")({
extension: /\.mdx$/ extension: /\.mdx$/,
}) });
module.exports = withMDX({ module.exports = withMDX({
pageExtensions: ['ts', 'tsx', 'mdx'] pageExtensions: ["ts", "tsx", "mdx"],
}) trailingSlash: true,
basePath: process.env.BASE_PATH,
});