Move from /photos/[:id] to /p/[:id]

This commit is contained in:
Sam Becker 2023-09-10 14:02:39 -05:00
parent 6766bf6a7f
commit aacee2f63f
9 changed files with 24 additions and 18 deletions

View File

@ -5,7 +5,6 @@ import SiteGrid from '@/components/SiteGrid';
import { import {
PHOTOS_PER_REQUEST, PHOTOS_PER_REQUEST,
ogImageDescriptionForPhoto, ogImageDescriptionForPhoto,
ogImageUrlForPhoto,
titleForPhoto, titleForPhoto,
} from '@/photo'; } from '@/photo';
import PhotoGrid from '@/photo/PhotoGrid'; import PhotoGrid from '@/photo/PhotoGrid';
@ -19,6 +18,7 @@ import {
getPhotosTakenBeforePhoto, getPhotosTakenBeforePhoto,
} from '@/services/postgres'; } from '@/services/postgres';
import { redirect } from 'next/navigation'; import { redirect } from 'next/navigation';
import { absoluteRouteForPhotoImage } from '@/site/routes';
export const runtime = 'edge'; export const runtime = 'edge';
@ -35,7 +35,7 @@ export async function generateMetadata(
const title = titleForPhoto(photo); const title = titleForPhoto(photo);
const description = ogImageDescriptionForPhoto(photo); const description = ogImageDescriptionForPhoto(photo);
const images = ogImageUrlForPhoto(photo); const images = absoluteRouteForPhotoImage(photo);
return { return {
title, title,
@ -44,7 +44,7 @@ export async function generateMetadata(
title, title,
images, images,
description, description,
url: `${BASE_URL}/photos/${photo.idShort}`, url: `${BASE_URL}/p/${photo.idShort}`,
}, },
twitter: { twitter: {
title, title,

4
src/cache/index.ts vendored
View File

@ -7,8 +7,8 @@ const TAG_PHOTOS = 'photos';
const PHOTO_PATHS = [ const PHOTO_PATHS = [
'/', '/',
'/grid', '/grid',
'/photos/[photoId]', '/p/[photoId]',
'/photos/[photoId]/image', '/p/[photoId]/image',
'/admin/photos', '/admin/photos',
'/admin/photos/[photoId]', '/admin/photos/[photoId]',
'/admin/photos/[photoId]/edit', '/admin/photos/[photoId]/edit',

View File

@ -7,6 +7,13 @@ export default function middleware(req: NextRequest, res:NextResponse) {
if (pathname === '/admin') { if (pathname === '/admin') {
return NextResponse.redirect(new URL('/admin/photos', req.url)); return NextResponse.redirect(new URL('/admin/photos', req.url));
} else if (/^\/photos\/(.)+$/.test(pathname)) {
// Accept full /photos/* paths, but redirect to /p/*
const matches = pathname.match(/^\/photos\/(.+)$/);
return NextResponse.redirect(new URL(
`/p/${matches?.[1]}`,
req.url,
));
} }
return auth( return auth(

View File

@ -4,7 +4,6 @@ import { useEffect, useState } from 'react';
import { import {
Photo, Photo,
ogImageDescriptionForPhoto, ogImageDescriptionForPhoto,
ogImageUrlForPhoto,
titleForPhoto, titleForPhoto,
} from '@/photo'; } from '@/photo';
import { cc } from '@/utility/css'; import { cc } from '@/utility/css';
@ -12,7 +11,7 @@ import Link from 'next/link';
import { BiError } from 'react-icons/bi'; import { BiError } from 'react-icons/bi';
import Spinner from '@/components/Spinner'; import Spinner from '@/components/Spinner';
import { IMAGE_OG_HEIGHT, IMAGE_OG_RATIO, IMAGE_OG_WIDTH } from '@/site'; import { IMAGE_OG_HEIGHT, IMAGE_OG_RATIO, IMAGE_OG_WIDTH } from '@/site';
import { routeForPhoto } from '@/site/routes'; import { absoluteRouteForPhotoImage, routeForPhoto } from '@/site/routes';
export type OGLoadingState = 'unloaded' | 'loading' | 'loaded' | 'failed'; export type OGLoadingState = 'unloaded' | 'loading' | 'loaded' | 'failed';
@ -84,7 +83,7 @@ export default function PhotoOGTile({
loadingState === 'loading' && 'opacity-0', loadingState === 'loading' && 'opacity-0',
'transition-opacity', 'transition-opacity',
)} )}
src={ogImageUrlForPhoto(photo)} src={absoluteRouteForPhotoImage(photo)}
width={IMAGE_OG_WIDTH} width={IMAGE_OG_WIDTH}
height={IMAGE_OG_HEIGHT} height={IMAGE_OG_HEIGHT}
onLoad={() => { onLoad={() => {

View File

@ -1,4 +1,4 @@
import { BASE_URL } from '@/site/config'; import { absoluteRouteForPhotoImage } from '@/site/routes';
import { formatDateFromPostgresString } from '@/utility/date'; import { formatDateFromPostgresString } from '@/utility/date';
import { import {
formatAperture, formatAperture,
@ -102,9 +102,6 @@ export const photoStatsAsString = (photo: Photo) => [
photo.isoFormatted, photo.isoFormatted,
].join(' '); ].join(' ');
export const ogImageUrlForPhoto = (photo: Photo) =>
`${BASE_URL}/photos/${photo.idShort}/image`;
export const ogImageDescriptionForPhoto = (photo: Photo) => export const ogImageDescriptionForPhoto = (photo: Photo) =>
photo.takenAtNaiveFormatted?.toUpperCase(); photo.takenAtNaiveFormatted?.toUpperCase();
@ -137,11 +134,11 @@ export const getPhotosLimitForQuery = (
export const generateImageMetaForPhoto = (photo?: Photo): Metadata => photo export const generateImageMetaForPhoto = (photo?: Photo): Metadata => photo
? { ? {
openGraph: { openGraph: {
images: ogImageUrlForPhoto(photo), images: absoluteRouteForPhotoImage(photo),
}, },
twitter: { twitter: {
card: 'summary_large_image', card: 'summary_large_image',
images: ogImageUrlForPhoto(photo), images: absoluteRouteForPhotoImage(photo),
}, },
} }
: {}; : {};

View File

@ -7,17 +7,20 @@ export const ROUTE_ADMIN_UPLOAD_BLOB_HANDLER = '/admin/uploads/blob';
export const routeForPhoto = (photo: Photo, share?: boolean) => export const routeForPhoto = (photo: Photo, share?: boolean) =>
share share
? `/photos/${photo.idShort}/share` ? `/p/${photo.idShort}/share`
: `/photos/${photo.idShort}`; : `/p/${photo.idShort}`;
export const absoluteRouteForPhoto = (photo: Photo) => export const absoluteRouteForPhoto = (photo: Photo) =>
`${BASE_URL}${routeForPhoto(photo)}`; `${BASE_URL}${routeForPhoto(photo)}`;
export const absoluteRouteForPhotoImage = (photo: Photo) =>
`${absoluteRouteForPhoto(photo)}/image`;
export const isRoutePhoto = (pathname = '') => export const isRoutePhoto = (pathname = '') =>
/^\/photos\/[^/]+\/?$/.test(pathname); /^\/p\/[^/]+\/?$/.test(pathname);
export const isRoutePhotoShare = (pathname = '') => export const isRoutePhotoShare = (pathname = '') =>
/^\/photos\/[^/]+\/share\/?$/.test(pathname); /^\/p\/[^/]+\/share\/?$/.test(pathname);
export const isRouteSignIn = (pathname = '') => export const isRouteSignIn = (pathname = '') =>
pathname.startsWith('/sign-in'); pathname.startsWith('/sign-in');