Print error when Image src is protocol-relative (#18809)
This PR prints a pretty error when the Image `src` property is a [protocol-relative URL](https://www.paulirish.com/2010/the-protocol-relative-url/). > Update 2014.12.17: > Now that SSL is encouraged for everyone and doesn’t have performance concerns, this technique is now an anti-pattern. If the asset you need is available on SSL, then always use the https:// asset. Fixes #18536
This commit is contained in:
parent
cdaf837f7c
commit
80fd3d5d17
3 changed files with 30 additions and 2 deletions
|
@ -572,14 +572,20 @@ function defaultLoader({ root, src, width, quality }: LoaderProps): string {
|
|||
)
|
||||
}
|
||||
|
||||
if (src && !src.startsWith('/') && configDomains) {
|
||||
if (src.startsWith('//')) {
|
||||
throw new Error(
|
||||
`Failed to parse src "${src}" on \`next/image\`, protocol-relative URL (//) must be changed to an absolute URL (http:// or https://)`
|
||||
)
|
||||
}
|
||||
|
||||
if (!src.startsWith('/') && configDomains) {
|
||||
let parsedSrc: URL
|
||||
try {
|
||||
parsedSrc = new URL(src)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
throw new Error(
|
||||
`Failed to parse "${src}" in "next/image", if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)`
|
||||
`Failed to parse src "${src}" on \`next/image\`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)`
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import React from 'react'
|
||||
import Image from 'next/image'
|
||||
|
||||
const Page = () => {
|
||||
return (
|
||||
<div>
|
||||
<p>Invalid Protocol Relative Source</p>
|
||||
<Image src="//assets.example.com/img.jpg" width="10" height="10" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Page
|
|
@ -311,6 +311,15 @@ function runTests(mode) {
|
|||
)
|
||||
})
|
||||
|
||||
it('should show invalid src error when protocol-relative', async () => {
|
||||
const browser = await webdriver(appPort, '/invalid-src-proto-relative')
|
||||
|
||||
await hasRedbox(browser)
|
||||
expect(await getRedboxHeader(browser)).toContain(
|
||||
'Failed to parse src "//assets.example.com/img.jpg" on `next/image`, protocol-relative URL (//) must be changed to an absolute URL (http:// or https://)'
|
||||
)
|
||||
})
|
||||
|
||||
it('should show invalid unsized error', async () => {
|
||||
const browser = await webdriver(appPort, '/invalid-unsized')
|
||||
|
||||
|
|
Loading…
Reference in a new issue