Skip to content

Commit

Permalink
fix(image): always proxy images through API route
Browse files Browse the repository at this point in the history
This patch updates the front-end to always proxy `<Image>` images
through a resource route to circumvent `cross-origin` policies.
  • Loading branch information
nicholaschiang committed Dec 21, 2023
1 parent b7c0492 commit ca027a1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
4 changes: 0 additions & 4 deletions app/components/image.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { ComponentPropsWithoutRef } from 'react'

import { OPTIMIZE_IMAGES } from 'utils/general'

export function Image({
optimizerUrl = '/image',
responsive,
Expand All @@ -15,8 +13,6 @@ export function Image({
responsive?: { maxWidth: number; size: { width: number } }[]
preload?: boolean
}) {
if (!OPTIMIZE_IMAGES) return <img alt={alt ?? ''} src={src} {...etc} />

const url = `${optimizerUrl}?url=${encodeURIComponent(src)}`
const props: ComponentPropsWithoutRef<'img'> = {
src: `${url}&w=${etc.width ?? ''}&q=75`,
Expand Down
4 changes: 3 additions & 1 deletion app/routes/image.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { LoaderFunction } from '@vercel/remix'

import { OPTIMIZE_IMAGES } from 'utils/general'

export const loader: LoaderFunction = async ({ request }) => {
const url = new URL(request.url)
const route = '_vercel/image'
return fetch(
process.env.VERCEL
OPTIMIZE_IMAGES && process.env.VERCEL
? `${url.protocol}//${url.host}/${route}?${url.searchParams.toString()}`
: decodeURIComponent(url.searchParams.get('url') ?? ''),
)
Expand Down

0 comments on commit ca027a1

Please sign in to comment.