Skip to content
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

Imageloader: collect images serverside to include images from staticp… #41554

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/next/build/webpack/loaders/next-image-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ function nextImageLoader(content) {
})
)

if (!isServer) {
this.emitFile(interpolatedName, content, null)
if (isServer) {
this.emitFile(
`../${isDev ? '' : '../'}${interpolatedName}`,
content,
null
)
}

return `export default ${stringifiedData};`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ const TRACE_IGNORES = [
'**/*/next/dist/bin/next',
]

const NOT_TRACEABLE = [
'.wasm',
'.png',
'.jpg',
'.jpeg',
'.gif',
'.webp',
'.avif',
'.ico',
'.bmp',
'.svg',
]

const TURBO_TRACE_DEFAULT_MAX_FILES = 128

function getModuleFromDependency(
Expand Down Expand Up @@ -136,7 +149,11 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
await span.traceChild('create-trace-assets').traceAsyncFn(async () => {
const entryFilesMap = new Map<any, Set<string>>()
const chunksToTrace = new Set<string>()
const isTraceable = (file: string) => !file.endsWith('.wasm')

const isTraceable = (file: string) =>
!NOT_TRACEABLE.some((suffix) => {
return file.endsWith(suffix)
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed otherwise the build will error, since the image files are not traceable. See issue #39812.

In a seperate PR this plugin should be fixed.


for (const entrypoint of compilation.entrypoints.values()) {
const entryFiles = new Set<string>()
Expand Down
13 changes: 12 additions & 1 deletion test/integration/next-image-legacy/default/pages/static-img.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import testImg from '../public/foo/test-rect.jpg'
import testImgProp from '../public/exif-rotation.jpg'
import Image from 'next/legacy/image'

import testJPG from '../public/test.jpg'
Expand All @@ -13,7 +14,11 @@ import testICO from '../public/test.ico'

import TallImage from '../components/TallImage'

const Page = () => {
export const getStaticProps = () => ({
props: { testImgProp },
})

const Page = ({ testImgProp }) => {
return (
<div>
<h1 id="page-header">Static Image</h1>
Expand All @@ -23,6 +28,12 @@ const Page = () => {
layout="fixed"
placeholder="blur"
/>
<Image
id="basic-staticprop"
src={testImgProp}
layout="fixed"
placeholder="blur"
/>
<TallImage />
<Image
id="defined-size-static"
Expand Down
22 changes: 22 additions & 0 deletions test/integration/next-image-legacy/default/test/static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ const runTests = () => {
`style="position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;background-size:cover;background-position:0% 0%;filter:blur(20px);background-image:url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAAOklEQVR42iWGsQkAIBDE0iuIdiLOJjiGIzjiL/Meb4okiNYIlLjK3hJMzCQG1/0qmXXOUkjAV+m9wAMe3QiV6Ne8VgAAAABJRU5ErkJggg==&quot;)`
)
})

it('should load direct imported image', async () => {
const src = await browser.elementById('basic-static').getAttribute('src')
expect(src).toMatch(
/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest-rect(.+)\.jpg&w=828&q=75/
)
const fullSrc = new URL(src, `http://localhost:${appPort}`)
const res = await fetch(fullSrc)
expect(res.status).toBe(200)
})

it('should load staticprops imported image', async () => {
const src = await browser
.elementById('basic-staticprop')
.getAttribute('src')
expect(src).toMatch(
/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Fexif-rotation(.+)\.jpg&w=256&q=75/
)
const fullSrc = new URL(src, `http://localhost:${appPort}`)
const res = await fetch(fullSrc)
expect(res.status).toBe(200)
})
}

describe('Build Error Tests', () => {
Expand Down
8 changes: 7 additions & 1 deletion test/integration/next-image-new/default/pages/static-img.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import testImg from '../public/foo/test-rect.jpg'
import testImgProp from '../public/exif-rotation.jpg'
import Image from 'next/image'

import testJPG from '../public/test.jpg'
Expand All @@ -19,11 +20,16 @@ import TallImage from '../components/TallImage'
const blurDataURL =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNM/s/wBwAFjwJgf8HDLgAAAABJRU5ErkJggg=='

const Page = () => {
export const getStaticProps = () => ({
props: { testImgProp },
})

const Page = ({ testImgProp }) => {
return (
<div>
<h1 id="page-header">Static Image</h1>
<Image id="basic-static" src={testImg} placeholder="blur" />
<Image id="basic-staticprop" src={testImgProp} placeholder="blur" />
<TallImage />
<Image
id="defined-width-and-height"
Expand Down
22 changes: 22 additions & 0 deletions test/integration/next-image-new/default/test/static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ const runTests = (isDev) => {
`color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 100 200'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20'/%3E%3C/filter%3E%3Cimage preserveAspectRatio='none' filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNM/s/wBwAFjwJgf8HDLgAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")`
)
})

it('should load direct imported image', async () => {
const src = await browser.elementById('basic-static').getAttribute('src')
expect(src).toMatch(
/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest-rect(.+)\.jpg&w=828&q=75/
)
const fullSrc = new URL(src, `http://localhost:${appPort}`)
const res = await fetch(fullSrc)
expect(res.status).toBe(200)
})

it('should load staticprops imported image', async () => {
const src = await browser
.elementById('basic-staticprop')
.getAttribute('src')
expect(src).toMatch(
/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Fexif-rotation(.+)\.jpg&w=256&q=75/
)
const fullSrc = new URL(src, `http://localhost:${appPort}`)
const res = await fetch(fullSrc)
expect(res.status).toBe(200)
})
}

describe('Build Error Tests', () => {
Expand Down