Append bypass secret to internal OG image requests

This commit is contained in:
Sam Becker 2025-01-02 14:39:56 -05:00
parent 5ee98aaeab
commit a3a620d04d
5 changed files with 32 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import {
NextImageSize, NextImageSize,
getNextImageUrlForRequest, getNextImageUrlForRequest,
} from '@/services/next-image'; } from '@/services/next-image';
import { IS_PREVIEW } from '@/site/config';
export default function ImagePhotoGrid({ export default function ImagePhotoGrid({
photos, photos,
@ -64,7 +65,12 @@ export default function ImagePhotoGrid({
}} }}
> >
<img {...{ <img {...{
src: getNextImageUrlForRequest(url, nextImageWidth), src: getNextImageUrlForRequest(
url, nextImageWidth,
undefined,
undefined,
IS_PREVIEW,
),
style: { style: {
width: '100%', width: '100%',
...imagePosition === 'center' && { ...imagePosition === 'center' && {

View File

@ -1,4 +1,8 @@
import { BASE_URL } from '@/site/config'; import {
BASE_URL,
VERCEL_BYPASS_KEY,
VERCEL_BYPASS_SECRET,
} from '@/site/config';
// Explicity defined next.config.js `imageSizes` // Explicity defined next.config.js `imageSizes`
type NextCustomSize = 200; type NextCustomSize = 200;
@ -14,6 +18,7 @@ export const getNextImageUrlForRequest = (
size: NextImageSize, size: NextImageSize,
quality = 75, quality = 75,
baseUrl = BASE_URL, baseUrl = BASE_URL,
addBypassSecret = false,
) => { ) => {
const url = new URL(`${baseUrl}/_next/image`); const url = new URL(`${baseUrl}/_next/image`);
@ -21,6 +26,10 @@ export const getNextImageUrlForRequest = (
url.searchParams.append('w', size.toString()); url.searchParams.append('w', size.toString());
url.searchParams.append('q', quality.toString()); url.searchParams.append('q', quality.toString());
if (addBypassSecret && VERCEL_BYPASS_SECRET) {
url.searchParams.append(VERCEL_BYPASS_KEY, VERCEL_BYPASS_SECRET);
}
return url.toString(); return url.toString();
}; };

View File

@ -37,6 +37,7 @@ export const IS_PRODUCTION = process.env.NODE_ENV === 'production' && (
export const IS_PREVIEW = VERCEL_ENV === 'preview'; export const IS_PREVIEW = VERCEL_ENV === 'preview';
export const VERCEL_BYPASS_KEY = 'x-vercel-protection-bypass';
export const VERCEL_BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET; export const VERCEL_BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET;
// User-facing domain, potential site title // User-facing domain, potential site title

View File

@ -1,7 +1,7 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
export default function useOnVisible( export default function useOnVisible(
ref: React.RefObject<HTMLElement>, ref: React.RefObject<HTMLElement | null>,
onVisible?: () => void onVisible?: () => void
) { ) {
useEffect(() => { useEffect(() => {

View File

@ -1,12 +1,16 @@
import { IS_PREVIEW, VERCEL_BYPASS_SECRET } from "@/site/config"; import {
IS_PREVIEW,
VERCEL_BYPASS_KEY,
VERCEL_BYPASS_SECRET,
} from '@/site/config';
export const fetchBypass: typeof fetch = (url, options) => export const fetchBypass: typeof fetch = (url, options) =>
IS_PREVIEW && VERCEL_BYPASS_SECRET IS_PREVIEW && VERCEL_BYPASS_SECRET
? fetch(url, { ? fetch(url, {
...options, ...options,
headers: { headers: {
...options?.headers, ...options?.headers,
'x-vercel-protection-bypass': VERCEL_BYPASS_SECRET [VERCEL_BYPASS_KEY]: VERCEL_BYPASS_SECRET,
}, },
}) })
: fetch(url, options); : fetch(url, options);