Skip to content

chore: replace next/legacy/image with next/image #1722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 28, 2022
48 changes: 25 additions & 23 deletions cypress/integration/default/images.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
describe('next/images', () => {
it('should show static image from /public', () => {
cy.visit('/')
cy.findByRole('img').should('be.visible').and(($img) => {
// "naturalWidth" and "naturalHeight" are set when the image loads
expect(
$img[0].naturalWidth,
'image has natural width'
).to.be.greaterThan(0)
})
cy.findByRole('img')
.should('be.visible')
.and(($img) => {
// "naturalWidth" and "naturalHeight" are set when the image loads
expect($img[0].naturalWidth, 'image has natural width').to.be.greaterThan(0)
})
})

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

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

it('should show a broken image if it is not on domains or remotePatterns allowlist', () => {
cy.visit('/image')
cy.findByRole('img',{ name: /jellybeans/i }).should('be.visible').and(($img) => {
expect(
$img[0].style.height
).to.equal('0px')
expect(
$img[0].style.width
).to.equal('0px')
it('should show throw if an image is not on the domains or remotePatterns allowlist', () => {
cy.visit('/broken-image')

// The image renders broken on the site
cy.findByRole('img', { name: /picture of the author/i }).then(($img) => {
// eslint-disable-next-line promise/no-nesting
cy.request({ url: $img[0].src, failOnStatusCode: false }).then((response) => {
// Navigating to the image itself give a forbidden error with a message explaining why.
expect(response.status).to.eq(403)
expect(response.body).to.include(
'URL not on allowlist: https://netlify-plugin-nextjs-demo.netlify.app/next-on-netlify.png',
)
})
})
})
})
2 changes: 1 addition & 1 deletion demos/default/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
},
// https://nextjs.org/docs/basic-features/image-optimization#domains
images: {
domains: ['raw.githubusercontent.com', 'upload.wikimedia.org'],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 'upload.wikimedia.org', as it was added a few weeks ago to prevent the /images page in the default demo from breaking due to Next.js throwing when the host is not supported for an image.

domains: ['raw.githubusercontent.com'],
remotePatterns: [
{
hostname: '*.imgur.com',
Expand Down
4 changes: 2 additions & 2 deletions demos/default/pages/broken-image.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Image from 'next/legacy/image'
import Image from 'next/image'

// This should cause an error, because broken-domain is not part of the configured next.config.js image domains
const Images = () => (
<Image
src="https://broken-domain/netlify/next-runtime/main/next-on-netlify.png"
src="https://netlify-plugin-nextjs-demo.netlify.app/next-on-netlify.png"
alt="Picture of the author"
width={500}
height={500}
Expand Down
12 changes: 1 addition & 11 deletions demos/default/pages/image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Image from 'next/legacy/image'
import Image from 'next/image'
import img from './unsplash.jpg'
import logo from './logomark.svg'

Expand All @@ -22,16 +22,6 @@ const Images = () => (
/>
<Image src="https://i.imgur.com/bxSRS3Jb.png" alt="Tawny Frogmouth" width={160} height={160} />
</p>

<p>
The following image should be broken as the domain is not added to domains or remotePatterns in next.config.js:
</p>
<Image
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"
alt="Jellybeans"
width={146}
height={32}
/>
</div>
)

Expand Down
1 change: 0 additions & 1 deletion demos/static-root/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Head from 'next/head'
import Image from 'next/legacy/image'
import { useRouter } from 'next/router'
import styles from '../styles/Home.module.css'

Expand Down
11 changes: 5 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ describe('onBuild()', () => {
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
})

test('sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not \'production\'', async () => {
test("sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not 'production'", async () => {
const mockUserDefinedSiteUrl = chance.url()
process.env.DEPLOY_PRIME_URL = mockUserDefinedSiteUrl
process.env.URL = chance.url()

// See https://docs.netlify.com/configure-builds/environment-variables/#build-metadata for all possible values
process.env.CONTEXT = 'deploy-preview'

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

expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
})
test('sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is \'production\'', async () => {

test("sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is 'production'", async () => {
const mockUserDefinedSiteUrl = chance.url()
process.env.DEPLOY_PRIME_URL = chance.url()
process.env.URL = mockUserDefinedSiteUrl
Expand All @@ -300,7 +300,6 @@ describe('onBuild()', () => {

expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
})


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

expect(imageConfigJson.domains.length).toBe(2)
expect(imageConfigJson.domains.length).toBe(1)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(imageConfigJson.remotePatterns.length).toBe(1)
expect(imageConfigJson.responseHeaders).toStrictEqual({
'X-Foo': mockHeaderValue,
Expand Down