1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 15:16:40 -04:00

remove dependency on uglify-js

This commit is contained in:
2024-02-26 10:49:14 -05:00
parent ace50ff2c9
commit 955cfe421f
19 changed files with 239 additions and 418 deletions

View File

@ -23,7 +23,7 @@ const App = ({ Component, pageProps }: AppProps) => {
// get this page's URL with full domain, and hack around query parameters and anchors
// NOTE: this assumes trailing slashes are enabled in next.config.js
const canonical = `${process.env.BASE_URL}${router.pathname === "/" ? "" : router.pathname}/`;
const canonical = `${process.env.NEXT_PUBLIC_BASE_URL || ""}${router.pathname === "/" ? "" : router.pathname}/`;
useEffect(() => {
// don't track pageviews on branch/deploy previews and localhost

View File

@ -1,10 +1,10 @@
import { NextResponse } from "next/server";
import queryString from "query-string";
import { siteDomain, hcaptchaSiteKey } from "../../lib/config";
import type { NextRequest } from "next/server";
// fallback to dummy secret for testing: https://docs.hcaptcha.com/#integration-testing-test-keys
const HCAPTCHA_SITE_KEY =
process.env.HCAPTCHA_SITE_KEY || process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY || "10000000-ffff-ffff-ffff-000000000001";
const HCAPTCHA_SITE_KEY = hcaptchaSiteKey || "10000000-ffff-ffff-ffff-000000000001";
const HCAPTCHA_SECRET_KEY = process.env.HCAPTCHA_SECRET_KEY || "0x0000000000000000000000000000000000000000";
const HCAPTCHA_API_ENDPOINT = "https://hcaptcha.com/siteverify";
@ -19,7 +19,7 @@ export const config = {
export default async (req: NextRequest) => {
// redirect GET requests to this endpoint to the contact form itself
if (req.method === "GET") {
return NextResponse.redirect(`${process.env.BASE_URL}/contact/`);
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL || `https://${siteDomain}`}/contact/`);
}
// possible weirdness? https://github.com/orgs/vercel/discussions/78#discussioncomment-5089059

View File

@ -1,117 +0,0 @@
// Fetches my Spotify most-played tracks or currently playing track.
// Heavily inspired by @leerob: https://leerob.io/snippets/spotify
import queryString from "query-string";
import { NextResponse } from "next/server";
import type { Track } from "../../types";
const { SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, SPOTIFY_REFRESH_TOKEN } = process.env;
// https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
const TOKEN_ENDPOINT = "https://accounts.spotify.com/api/token";
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-the-users-currently-playing-track
const NOW_PLAYING_ENDPOINT = "https://api.spotify.com/v1/me/player/currently-playing";
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-top-artists-and-tracks
const TOP_TRACKS_ENDPOINT = "https://api.spotify.com/v1/me/top/tracks?time_range=long_term&limit=10";
type SpotifyTrackSchema = {
name: string;
artists: Array<{
name: string;
}>;
album: {
name: string;
images?: Array<{
url: URL | string;
}>;
};
imageUrl?: URL | string;
external_urls: {
spotify: URL | string;
};
};
export const config = {
runtime: "edge",
};
// eslint-disable-next-line import/no-anonymous-default-export
export default async () => {
const token = await getAccessToken();
const [playing, top] = await Promise.all([getNowPlaying(token), getTopTracks(token)]);
return NextResponse.json({ playing, top }, { status: 200 });
};
const getAccessToken = async () => {
const basic = Buffer.from(`${SPOTIFY_CLIENT_ID}:${SPOTIFY_CLIENT_SECRET}`).toString("base64");
const response = await fetch(TOKEN_ENDPOINT, {
method: "POST",
headers: {
Authorization: `Basic ${basic}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: queryString.stringify({
grant_type: "refresh_token",
refresh_token: SPOTIFY_REFRESH_TOKEN,
}),
});
const { access_token: token } = await response.json();
return token;
};
const getNowPlaying = async (token: string): Promise<Track | false> => {
const response = await fetch(NOW_PLAYING_ENDPOINT, {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
"Content-Type": "application/json",
},
});
if (response.status === 204 || response.status > 400) {
return false;
}
const active = (await response.json()) as {
is_playing: boolean;
item?: SpotifyTrackSchema;
};
if (active?.is_playing === true && active?.item) {
return {
artist: active.item.artists.map((artist) => artist.name).join(", "),
title: active.item.name,
album: active.item.album.name,
image: active.item.album.images ? active.item.album.images[0].url : undefined,
url: active.item.external_urls.spotify,
};
}
return false;
};
const getTopTracks = async (token: string): Promise<Track[]> => {
const response = await fetch(TOP_TRACKS_ENDPOINT, {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
"Content-Type": "application/json",
},
});
const { items } = (await response.json()) as { items: SpotifyTrackSchema[] };
const tracks = items.map<Track>((track) => ({
artist: track.artists.map((artist) => artist.name).join(", "),
title: track.name,
album: track.album.name,
image: track.album.images ? track.album.images[0].url : undefined,
url: track.external_urls.spotify,
}));
return tracks;
};

View File

@ -32,7 +32,7 @@ const Note = ({ frontMatter, source }: InferGetStaticPropsType<typeof getStaticP
},
images: [
{
url: `${process.env.BASE_URL}${frontMatter.image || meJpg.src}`,
url: `${process.env.NEXT_PUBLIC_BASE_URL || ""}${frontMatter.image || meJpg.src}`,
alt: frontMatter.title,
},
],
@ -47,7 +47,7 @@ const Note = ({ frontMatter, source }: InferGetStaticPropsType<typeof getStaticP
description={frontMatter.description || config.longDescription}
datePublished={frontMatter.date}
dateModified={frontMatter.date}
images={[`${process.env.BASE_URL}${frontMatter.image || meJpg.src}`]}
images={[`${process.env.NEXT_PUBLIC_BASE_URL || ""}${frontMatter.image || meJpg.src}`]}
{...articleJsonLd}
/>

View File

@ -11,7 +11,7 @@ ${
? `Disallow: /`
: `Allow: /
Sitemap: ${process.env.BASE_URL}/sitemap.xml`
Sitemap: ${process.env.NEXT_PUBLIC_BASE_URL || ""}/sitemap.xml`
}
`;

View File

@ -1,11 +1,12 @@
import { SitemapStream, EnumChangefreq } from "sitemap";
import { getAllNotes } from "../lib/helpers/parse-notes";
import { siteDomain } from "../lib/config";
import type { GetServerSideProps } from "next";
import type { SitemapItemLoose } from "sitemap";
export const getServerSideProps: GetServerSideProps<Record<string, never>> = async (context) => {
const { res } = context;
const stream = new SitemapStream({ hostname: process.env.BASE_URL });
const stream = new SitemapStream({ hostname: process.env.NEXT_PUBLIC_BASE_URL || `https://${siteDomain}` });
// cache on edge for 12 hours
res.setHeader("cache-control", "public, max-age=0, s-maxage=43200, stale-while-revalidate");