Skip to content

Commit

Permalink
fix: use correct protocol for image source
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jul 1, 2021
1 parent 98e5bfb commit cee4182
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions demo/pages/image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Image from 'next/image'
import img from './unsplash.jpg'
import logo from './logomark.svg'

const Images = () => (
<div>
Expand All @@ -11,6 +12,9 @@ const Images = () => (
</a>{' '}
on <a href="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
</p>
<p>
<Image src={logo} alt="netlify logomark" />
</p>
</div>
)

Expand Down
9 changes: 9 additions & 0 deletions demo/pages/logomark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions src/lib/templates/imageFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ const handler = async (event) => {
const quality = parseInt(q) || 60

let imageUrl
let isRemoteImage = false
// Relative image
if (parsedUrl.startsWith('/')) {
imageUrl = `${process.env.DEPLOY_URL || `http://${event.headers.host}`}${parsedUrl}`
const protocol = event.headers['x-forwarded-proto'] || 'http'
imageUrl = `${protocol}://${event.headers.host || event.hostname}${parsedUrl}`
} else {
isRemoteImage = true
// Remote images need to be in the allowlist
const allowedDomains = process.env.NEXT_IMAGE_ALLOWED_DOMAINS
? process.env.NEXT_IMAGE_ALLOWED_DOMAINS.split(',').map((domain) => domain.trim())
Expand Down Expand Up @@ -96,7 +99,7 @@ const handler = async (event) => {
return {
statusCode: 302,
headers: {
Location: imageUrl,
Location: isRemoteImage ? imageUrl : parsedUrl,
},
}
}
Expand Down

0 comments on commit cee4182

Please sign in to comment.