Skip to content

Commit 03e8b67

Browse files
committed
fix: use correct protocol for image source
1 parent 98e5bfb commit 03e8b67

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

demo/pages/image.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Image from 'next/image'
22
import img from './unsplash.jpg'
3+
import logo from './logomark.svg'
34

45
const Images = () => (
56
<div>
@@ -11,6 +12,9 @@ const Images = () => (
1112
</a>{' '}
1213
on <a href="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
1314
</p>
15+
<p>
16+
<Image src={logo} alt="netlify logomark" />
17+
</p>
1418
</div>
1519
)
1620

demo/pages/logomark.svg

+9
Loading

src/lib/templates/imageFunction.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ const handler = async (event) => {
3939
const quality = parseInt(q) || 60
4040

4141
let imageUrl
42+
let isRemoteImage = false
4243
// Relative image
4344
if (parsedUrl.startsWith('/')) {
44-
imageUrl = `${process.env.DEPLOY_URL || `http://${event.headers.host}`}${parsedUrl}`
45+
imageUrl = `${event.protocol || (event.headers.scheme && `${event.headers.scheme}:`) || 'http:'}//${
46+
event.headers.host || event.hostname
47+
}${parsedUrl}`
48+
console.log({ imageUrl })
4549
} else {
50+
isRemoteImage = true
4651
// Remote images need to be in the allowlist
4752
const allowedDomains = process.env.NEXT_IMAGE_ALLOWED_DOMAINS
4853
? process.env.NEXT_IMAGE_ALLOWED_DOMAINS.split(',').map((domain) => domain.trim())
@@ -96,7 +101,8 @@ const handler = async (event) => {
96101
return {
97102
statusCode: 302,
98103
headers: {
99-
Location: imageUrl,
104+
Location: isRemoteImage ? imageUrl : parsedUrl,
105+
'X-Event': JSON.stringify(event),
100106
},
101107
}
102108
}

0 commit comments

Comments
 (0)