Skip to content

Commit c9ab983

Browse files
zachleatLekoArts
andauthored
fix: strip domain from ipx edge functions path (#2098) (#2099)
* fix: strip domain from ipx edge functions path (#2098) * chore: add unit tests for sanitizer for #2098 --------- Co-authored-by: Lennart <[email protected]>
1 parent dc7c479 commit c9ab983

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/runtime/src/helpers/edge.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ export const loadPrerenderManifest = (netlifyConfig: NetlifyConfig): Promise<Pre
9393
*/
9494
const sanitizeName = (name: string) => `next_${name.replace(/\W/g, '_')}`
9595

96+
/**
97+
* Convert the images path to strip the origin (until domain-level Edge functions are supported)
98+
*/
99+
export const sanitizeEdgePath = (imagesPath: string) => new URL(imagesPath, process.env.URL || 'http://n').pathname
100+
96101
// Slightly different spacing in different versions!
97102
const IMPORT_UNSUPPORTED = [
98103
`Object.defineProperty(globalThis,"__import_unsupported"`,
@@ -485,10 +490,11 @@ export const writeEdgeFunctions = async ({
485490
join('.netlify', 'functions-internal', IMAGE_FUNCTION_NAME, 'imageconfig.json'),
486491
join(edgeFunctionDir, 'imageconfig.json'),
487492
)
493+
488494
manifest.functions.push({
489495
function: 'ipx',
490496
name: 'next/image handler',
491-
path: nextConfig.images.path || '/_next/image',
497+
path: nextConfig.images.path ? sanitizeEdgePath(nextConfig.images.path) : '/_next/image',
492498
generator,
493499
})
494500

test/helpers/matchers.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getEdgeFunctionPatternForPage } from '../../packages/runtime/src/helpers/edge'
1+
import { getEdgeFunctionPatternForPage, sanitizeEdgePath } from '../../packages/runtime/src/helpers/edge'
22
import { makeLocaleOptional, stripLookahead } from '../../packages/runtime/src/helpers/matchers'
33

44
const makeDataPath = (path: string) => `/_next/data/build-id${path === '/' ? '/index' : path}.json`
@@ -183,3 +183,24 @@ describe('the edge function matcher helpers', () => {
183183
expect('/edge/1/').toMatch(new RegExp(regex))
184184
})
185185
})
186+
187+
describe('the images path is sanitized', () => {
188+
it('passes through a path', () => {
189+
expect(sanitizeEdgePath('/_next/image')).toBe('/_next/image')
190+
})
191+
192+
it('strips domains', () => {
193+
expect(sanitizeEdgePath('http://example.com/_next/image')).toBe('/_next/image')
194+
expect(sanitizeEdgePath('https://example.com/_next/image')).toBe('/_next/image')
195+
})
196+
197+
it('strips domains with globs', () => {
198+
expect(sanitizeEdgePath('http://example.com/_next/image/*')).toBe('/_next/image/*')
199+
expect(sanitizeEdgePath('https://example.com/_next/image/*')).toBe('/_next/image/*')
200+
})
201+
202+
it('strips domains with tokens', () => {
203+
expect(sanitizeEdgePath('http://example.com/_next/image/:slug/')).toBe('/_next/image/:slug/')
204+
expect(sanitizeEdgePath('https://example.com/_next/image/:slug/')).toBe('/_next/image/:slug/')
205+
})
206+
})

0 commit comments

Comments
 (0)