Skip to content

Commit bcd7c11

Browse files
authored
chore: replace next/legacy/image with next/image (#1722)
* chore: removed unused import * chore: removed broken image that was no longer broken from main images demo page * test: add e2e test to test broken image error * chore: fixed image import * test: updated test validating image config * test: fixed broken image test * test: fixed broken image test as it's different when deployed * test: used a real image to test an image not on the Next.js allow list
1 parent e813101 commit bcd7c11

File tree

6 files changed

+34
-44
lines changed

6 files changed

+34
-44
lines changed

cypress/integration/default/images.spec.ts

+25-23
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
describe('next/images', () => {
55
it('should show static image from /public', () => {
66
cy.visit('/')
7-
cy.findByRole('img').should('be.visible').and(($img) => {
8-
// "naturalWidth" and "naturalHeight" are set when the image loads
9-
expect(
10-
$img[0].naturalWidth,
11-
'image has natural width'
12-
).to.be.greaterThan(0)
13-
})
7+
cy.findByRole('img')
8+
.should('be.visible')
9+
.and(($img) => {
10+
// "naturalWidth" and "naturalHeight" are set when the image loads
11+
expect($img[0].naturalWidth, 'image has natural width').to.be.greaterThan(0)
12+
})
1413
})
1514

1615
it('should show image using next/image', () => {
@@ -20,24 +19,27 @@ describe('next/images', () => {
2019

2120
it('should show image allow-listed with remotePatterns', () => {
2221
cy.visit('/image')
23-
cy.findByRole('img',{ name: /tawny frogmouth/i }).should('be.visible').and(($img) => {
24-
// "naturalWidth" and "naturalHeight" are set when the image loads
25-
expect(
26-
$img[0].naturalWidth,
27-
'image has natural width'
28-
).to.be.greaterThan(0)
29-
})
22+
cy.findByRole('img', { name: /shiba inu dog looks through a window/i })
23+
.should('be.visible')
24+
.and(($img) => {
25+
// "naturalWidth" and "naturalHeight" are set when the image loads
26+
expect($img[0].naturalWidth, 'image has natural width').to.be.greaterThan(0)
27+
})
3028
})
3129

32-
it('should show a broken image if it is not on domains or remotePatterns allowlist', () => {
33-
cy.visit('/image')
34-
cy.findByRole('img',{ name: /jellybeans/i }).should('be.visible').and(($img) => {
35-
expect(
36-
$img[0].style.height
37-
).to.equal('0px')
38-
expect(
39-
$img[0].style.width
40-
).to.equal('0px')
30+
it('should show throw if an image is not on the domains or remotePatterns allowlist', () => {
31+
cy.visit('/broken-image')
32+
33+
// The image renders broken on the site
34+
cy.findByRole('img', { name: /picture of the author/i }).then(($img) => {
35+
// eslint-disable-next-line promise/no-nesting
36+
cy.request({ url: $img[0].src, failOnStatusCode: false }).then((response) => {
37+
// Navigating to the image itself give a forbidden error with a message explaining why.
38+
expect(response.status).to.eq(403)
39+
expect(response.body).to.include(
40+
'URL not on allowlist: https://netlify-plugin-nextjs-demo.netlify.app/next-on-netlify.png',
41+
)
42+
})
4143
})
4244
})
4345
})

demos/default/next.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = {
7171
},
7272
// https://nextjs.org/docs/basic-features/image-optimization#domains
7373
images: {
74-
domains: ['raw.githubusercontent.com', 'upload.wikimedia.org'],
74+
domains: ['raw.githubusercontent.com'],
7575
remotePatterns: [
7676
{
7777
hostname: '*.imgur.com',

demos/default/pages/broken-image.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import Image from 'next/legacy/image'
1+
import Image from 'next/image'
22

33
// This should cause an error, because broken-domain is not part of the configured next.config.js image domains
44
const Images = () => (
55
<Image
6-
src="https://broken-domain/netlify/next-runtime/main/next-on-netlify.png"
6+
src="https://netlify-plugin-nextjs-demo.netlify.app/next-on-netlify.png"
77
alt="Picture of the author"
88
width={500}
99
height={500}

demos/default/pages/image.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Image from 'next/legacy/image'
1+
import Image from 'next/image'
22
import img from './unsplash.jpg'
33
import logo from './logomark.svg'
44

@@ -22,16 +22,6 @@ const Images = () => (
2222
/>
2323
<Image src="https://i.imgur.com/bxSRS3Jb.png" alt="Tawny Frogmouth" width={160} height={160} />
2424
</p>
25-
26-
<p>
27-
The following image should be broken as the domain is not added to domains or remotePatterns in next.config.js:
28-
</p>
29-
<Image
30-
src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-256px-SIPI_Jelly_Beans_4.1.07.tiff.jpg"
31-
alt="Jellybeans"
32-
width={146}
33-
height={32}
34-
/>
3525
</div>
3626
)
3727

demos/static-root/pages/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Head from 'next/head'
2-
import Image from 'next/legacy/image'
32
import { useRouter } from 'next/router'
43
import styles from '../styles/Home.module.css'
54

test/index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ describe('onBuild()', () => {
255255
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
256256
})
257257

258-
test('sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not \'production\'', async () => {
258+
test("sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not 'production'", async () => {
259259
const mockUserDefinedSiteUrl = chance.url()
260260
process.env.DEPLOY_PRIME_URL = mockUserDefinedSiteUrl
261261
process.env.URL = chance.url()
262-
262+
263263
// See https://docs.netlify.com/configure-builds/environment-variables/#build-metadata for all possible values
264264
process.env.CONTEXT = 'deploy-preview'
265265

@@ -277,8 +277,8 @@ describe('onBuild()', () => {
277277

278278
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
279279
})
280-
281-
test('sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is \'production\'', async () => {
280+
281+
test("sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is 'production'", async () => {
282282
const mockUserDefinedSiteUrl = chance.url()
283283
process.env.DEPLOY_PRIME_URL = chance.url()
284284
process.env.URL = mockUserDefinedSiteUrl
@@ -300,7 +300,6 @@ describe('onBuild()', () => {
300300

301301
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
302302
})
303-
304303

305304
test('sets the NEXTAUTH_URL specified in the netlify.toml or in the Netlify UI', async () => {
306305
const mockSiteUrl = chance.url()
@@ -615,7 +614,7 @@ describe('onBuild()', () => {
615614
const imageConfigPath = path.join(constants.INTERNAL_FUNCTIONS_SRC, IMAGE_FUNCTION_NAME, 'imageconfig.json')
616615
const imageConfigJson = await readJson(imageConfigPath)
617616

618-
expect(imageConfigJson.domains.length).toBe(2)
617+
expect(imageConfigJson.domains.length).toBe(1)
619618
expect(imageConfigJson.remotePatterns.length).toBe(1)
620619
expect(imageConfigJson.responseHeaders).toStrictEqual({
621620
'X-Foo': mockHeaderValue,

0 commit comments

Comments
 (0)