Skip to content

Commit a493a2f

Browse files
chore: refactor source image URL construction (#66)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent b7551c2 commit a493a2f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

example/public/index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ <h1 class="text-4xl mb-4">Netlify Optimized Images</h1>
1919
<figcaption>/.netlify/builders/ipx/f_webp,w_450/img/https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba
2020
<figcaption>
2121
</figure>
22+
<figure>
23+
<img
24+
src="/.netlify/builders/ipx/f_webp,w_450/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1514888286974-6c03e2ca1dba%3Fheight%3D500%26width%3D500%26fit%3Dcrop" />
25+
<figcaption>
26+
/.netlify/builders/ipx/f_webp,w_450/img/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1514888286974-6c03e2ca1dba%3Fheight%3D500%26width%3D500%26fit%3Dcrop
27+
With query string in source
28+
<figcaption>
29+
</figure>
2230
<figure>
2331
<img src="/.netlify/builders/ipx/f_webp,w_146/https://www.netlify.com/img/deploy/button.svg" />
2432
<figcaption>/.netlify/builders/ipx/f_webp,w_146/img/https://www.netlify.com/img/deploy/button.svg
@@ -49,4 +57,4 @@ <h1 class="text-4xl mb-4">Netlify Optimized Images</h1>
4957

5058
</body>
5159

52-
</html>
60+
</html>

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ export function createIPXHandler ({
2929
basePath = `${basePath}/`
3030
}
3131
const handler: Handler = async (event, _context) => {
32-
const host = event.headers.host
33-
const protocol = event.headers['x-forwarded-proto'] === 'https' ? 'https' : 'http'
3432
let domains = (opts as IPXOptions).domains || []
3533
const remoteURLPatterns = remotePatterns || []
3634
const requestEtag = event.headers['if-none-match']
37-
const url = event.path.replace(basePath, '')
35+
const eventPath = event.path.replace(basePath, '')
3836

3937
// eslint-disable-next-line prefer-const
40-
let [modifiers = '_', ...segments] = url.split('/')
38+
let [modifiers = '_', ...segments] = eventPath.split('/')
4139
let id = decodeURIComponent(segments.join('/'))
4240

4341
if (propsEncoding === 'base64') {
44-
const params = decodeBase64Params(url)
42+
const params = decodeBase64Params(eventPath)
4543
if (params.error) {
4644
return {
4745
statusCode: 400,
@@ -53,9 +51,11 @@ export function createIPXHandler ({
5351
}
5452

5553
const requestHeaders: Record<string, string> = {}
56-
const isLocal = !id.startsWith('http')
54+
const isLocal = !id.startsWith('http://') && !id.startsWith('https://')
5755
if (isLocal) {
58-
id = `${protocol}://${host}${id.startsWith('/') ? '' : '/'}${id}`
56+
const url = new URL(event.rawUrl)
57+
url.pathname = id
58+
id = url.toString()
5959
if (event.headers.cookie) {
6060
requestHeaders.cookie = event.headers.cookie
6161
}

0 commit comments

Comments
 (0)