-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix next/image noscript src path with loaders (#24011)
In the `noscript` img version the correct `src` and `sizes` attributes are overwritten by not necessary inline declaration; in particular using the loaders the `src` attribute not take the right absolute path. I found this issue using a custom loader and because my site didn't indexing any images on the Google image search. Fixes #24277
- Loading branch information
Showing
3 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react' | ||
import Image from 'next/image' | ||
|
||
const myLoader = ({ src, width, quality }) => { | ||
return `https://customresolver.com${src}?w~~${width},q~~${quality}` | ||
} | ||
|
||
const Page = () => { | ||
return ( | ||
<div> | ||
<p>noscript images</p> | ||
<Image id="basic-image" src="/basic-image.jpg" width={640} height={360} /> | ||
<Image | ||
loader={myLoader} | ||
id="image-with-loader" | ||
src="/remote-image.jpg" | ||
width={640} | ||
height={360} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |
44 changes: 44 additions & 0 deletions
44
test/integration/image-component/noscript/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import cheerio from 'cheerio' | ||
import { | ||
killApp, | ||
findPort, | ||
nextStart, | ||
nextBuild, | ||
renderViaHTTP, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 30) | ||
|
||
const appDir = join(__dirname, '../') | ||
let appPort | ||
let app | ||
|
||
describe('Noscript Tests', () => { | ||
beforeAll(async () => { | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
describe('Noscript page source tests', () => { | ||
it('should use local API for noscript img#basic-image src attribute', async () => { | ||
const html = await renderViaHTTP(appPort, '/') | ||
const $ = cheerio.load(html) | ||
|
||
expect($('noscript > img#basic-image').attr('src')).toMatch( | ||
/^\/_next\/image/ | ||
) | ||
}) | ||
it('should use loader url for noscript img#image-with-loader src attribute', async () => { | ||
const html = await renderViaHTTP(appPort, '/') | ||
const $ = cheerio.load(html) | ||
|
||
expect($('noscript > img#image-with-loader').attr('src')).toMatch( | ||
/^https:\/\/customresolver.com/ | ||
) | ||
}) | ||
}) | ||
}) |