Skip to content

Commit 15be0d1

Browse files
Merge pull request #1085 from CodeForAfrica/fix/fix-image-paths
Transform Media paths
2 parents 71e19cd + 47184e6 commit 15be0d1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

apps/climatemappedafrica/src/payload/collections/Media.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import transformDocMediaUrl from "../utils/transformDocMediaUrl";
2+
13
const Media = {
24
slug: "media",
35
admin: {
@@ -22,7 +24,7 @@ const Media = {
2224
},
2325
],
2426
hooks: {
25-
afterRead: [({ doc }) => ({ ...doc, src: doc.url })],
27+
afterRead: [({ doc }) => transformDocMediaUrl(doc)],
2628
},
2729
};
2830

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const transformDocMediaUrl = (doc) => {
2+
// Can't use absolute URLs in src because we set PAYLOAD_PUBLIC_APP_URL to localhost at build-time & will lead to 404 at runtime
3+
// Solution: use relative URLs
4+
const { pathname } = new URL(doc.url);
5+
return {
6+
...doc,
7+
alt: doc.alt ?? null,
8+
src: pathname,
9+
url: pathname,
10+
};
11+
};
12+
13+
export default transformDocMediaUrl;

0 commit comments

Comments
 (0)