1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2026-01-14 10:42:56 -05:00

only reveal a db record via /api/hits if it matches a real page

This commit is contained in:
2022-07-06 11:49:41 -04:00
parent 8d47958473
commit 155c6cacd9
9 changed files with 82 additions and 76 deletions

View File

@@ -1,9 +1,7 @@
// Next.js constants (not needed in frontend)
import path from "path";
// directory containing .mdx files relative to project root
export const NOTES_DIR = path.join(process.cwd(), "notes");
export const NOTES_DIR = "notes";
// normalize the timestamp saved when building/deploying (see next.config.js) and fall back to right now:
export const RELEASE_DATE = new Date(process.env.RELEASE_DATE || Date.now()).toISOString();

View File

@@ -3,8 +3,7 @@ import { getAllNotes } from "./parse-notes";
import * as config from "../config";
import { RELEASE_DATE } from "../config/constants";
import { favicons } from "../config/seo";
import type { GetServerSidePropsContext, GetServerSidePropsResult, PreviewData } from "next";
import type { ParsedUrlQuery } from "querystring";
import type { GetServerSideProps } from "next";
export type BuildFeedOptions = {
edgeCacheAge?: number; // in seconds, defaults to 43200 (12 hours)
@@ -13,10 +12,10 @@ export type BuildFeedOptions = {
// handles literally *everything* about building the server-side rss/atom feeds and writing the response.
// all the page needs to do is `return buildFeed(context, "rss")` from getServerSideProps.
export const buildFeed = async (
context: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>,
context: Parameters<GetServerSideProps>[0],
type: "rss" | "atom" | "json",
options?: BuildFeedOptions
): Promise<GetServerSidePropsResult<Record<string, never>>> => {
): Promise<ReturnType<GetServerSideProps<Record<string, never>>>> => {
const { res } = context;
// https://github.com/jpmonette/feed#example

View File

@@ -21,13 +21,13 @@ const IsomorphicDayJs = (date?: dayjs.ConfigType): dayjs.Dayjs => {
// simple wrapper around dayjs.format() to normalize timezone across the site, both server and client side, to prevent
// hydration errors by returning an instance of dayjs with these defaults set.
// date defaults to now, format defaults to ISO 8601 (e.g. 2022-04-07T21:53:33-04:00)
export const formatDate = (date?: dayjs.ConfigType, formatStr?: string) => {
export const formatDate = (date?: dayjs.ConfigType, formatStr?: string): string => {
return IsomorphicDayJs(date).tz(timeZone).format(formatStr);
};
// returns the human-friendly difference between now and given date (e.g. "5 minutes", "9 months", etc.)
// set `{ suffix: true }` to include the "... ago" or "in ..." for past/future
export const formatTimeAgo = (date: dayjs.ConfigType, options?: { suffix?: boolean }) => {
export const formatTimeAgo = (date: dayjs.ConfigType, options?: { suffix?: boolean }): string => {
return IsomorphicDayJs().isBefore(date)
? IsomorphicDayJs(date).toNow(!options?.suffix)
: IsomorphicDayJs(date).fromNow(!options?.suffix);

View File

@@ -14,7 +14,7 @@ import type { NoteFrontMatter } from "../../types";
export const getNoteSlugs = async (): Promise<string[]> => {
// list all .mdx files in NOTES_DIR
const mdxFiles = await glob("*.mdx", {
cwd: NOTES_DIR,
cwd: path.join(process.cwd(), NOTES_DIR),
dot: false,
});
@@ -31,7 +31,7 @@ export const getNoteData = async (
frontMatter: NoteFrontMatter;
content: string;
}> => {
const fullPath = path.join(NOTES_DIR, `${slug}.mdx`);
const fullPath = path.join(process.cwd(), NOTES_DIR, `${slug}.mdx`);
const rawContent = await fs.readFile(fullPath, "utf8");
const { data, content } = matter(rawContent);
@@ -47,7 +47,7 @@ export const getNoteData = async (
smartypants: true,
}),
slug,
permalink: `${baseUrl}/notes/${slug}/`,
permalink: `${baseUrl}/${NOTES_DIR}/${slug}/`,
date: formatDate(data.date), // validate/normalize the date string provided from front matter
},
content,