@@ -2,46 +2,37 @@ import { getImgResponse } from 'openimg/node'
22import { type Route } from './+types/images.ts'
33import { getDomainUrl } from '#app/utils/misc.tsx'
44
5- function isUrl ( src : string ) {
6- try {
7- new URL ( src )
8- return true
9- } catch {
10- return false
11- }
12- }
13-
145export async function loader ( { request } : Route . LoaderArgs ) {
156 const domain = getDomainUrl ( request )
167 const headers = new Headers ( )
178 headers . set ( 'Cache-Control' , 'public, max-age=31536000, immutable' )
189 return getImgResponse ( request , {
1910 headers,
20- allowlistedOrigins : [ domain ] ,
11+ allowlistedOrigins : [ domain ] ,
2112 getImgSource : ( { params } ) => {
2213 if ( params . src . startsWith ( '/resources' ) ) {
23- // Fetch image from resource endpoint
14+ // Fetch image from resource endpoint
2415 return {
2516 type : 'fetch' ,
2617 url : domain + params . src ,
2718 }
2819 }
29- if ( isUrl ( params . src ) ) {
30- // Fetch image from external URL; will be matched against allowlist
20+ if ( URL . canParse ( params . src ) ) {
21+ // Fetch image from external URL; will be matched against allowlist
3122 return {
3223 type : 'fetch' ,
3324 url : params . src ,
3425 }
3526 }
36- // Retrieve image from filesystem (public folder)
37- if ( params . src . startsWith ( '/assets' ) ) {
38- // Files managed by Vite
39- return {
40- type : 'fs' ,
41- path : '.' + params . src ,
42- }
43- }
44- // Fallback to files in public folder
27+ // Retrieve image from filesystem (public folder)
28+ if ( params . src . startsWith ( '/assets' ) ) {
29+ // Files managed by Vite
30+ return {
31+ type : 'fs' ,
32+ path : '.' + params . src ,
33+ }
34+ }
35+ // Fallback to files in public folder
4536 return {
4637 type : 'fs' ,
4738 path : './public' + params . src ,
0 commit comments