Unverified Commit 7bbaff74 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure blurDataURL is correct in dev with basePath (#29436)

Follow-up to https://github.com/vercel/next.js/pull/29307 this ensures the `blurDataURL` is correctly prefixed with the `basePath` in development since we use the `_next/image` endpoint to generate the placeholder in dev mode. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/29289#issuecomment-927758204
parent bc66f55d
...@@ -1157,6 +1157,7 @@ export default async function getBaseWebpackConfig( ...@@ -1157,6 +1157,7 @@ export default async function getBaseWebpackConfig(
options: { options: {
isServer, isServer,
isDev: dev, isDev: dev,
basePath: config.basePath,
assetPrefix: config.assetPrefix, assetPrefix: config.assetPrefix,
}, },
}, },
......
...@@ -9,7 +9,8 @@ const VALID_BLUR_EXT = ['jpeg', 'png', 'webp'] ...@@ -9,7 +9,8 @@ const VALID_BLUR_EXT = ['jpeg', 'png', 'webp']
function nextImageLoader(content) { function nextImageLoader(content) {
const imageLoaderSpan = this.currentTraceSpan.traceChild('next-image-loader') const imageLoaderSpan = this.currentTraceSpan.traceChild('next-image-loader')
return imageLoaderSpan.traceAsyncFn(async () => { return imageLoaderSpan.traceAsyncFn(async () => {
const { isServer, isDev, assetPrefix } = loaderUtils.getOptions(this) const { isServer, isDev, assetPrefix, basePath } =
loaderUtils.getOptions(this)
const context = this.rootContext const context = this.rootContext
const opts = { context, content } const opts = { context, content }
const interpolatedName = loaderUtils.interpolateName( const interpolatedName = loaderUtils.interpolateName(
...@@ -31,7 +32,7 @@ function nextImageLoader(content) { ...@@ -31,7 +32,7 @@ function nextImageLoader(content) {
if (VALID_BLUR_EXT.includes(extension)) { if (VALID_BLUR_EXT.includes(extension)) {
if (isDev) { if (isDev) {
const prefix = 'http://localhost' const prefix = 'http://localhost'
const url = new URL('/_next/image', prefix) const url = new URL(`${basePath || ''}/_next/image`, prefix)
url.searchParams.set('url', outputPath) url.searchParams.set('url', outputPath)
url.searchParams.set('w', BLUR_IMG_SIZE) url.searchParams.set('w', BLUR_IMG_SIZE)
url.searchParams.set('q', BLUR_QUALITY) url.searchParams.set('q', BLUR_QUALITY)
......
...@@ -6,6 +6,7 @@ import { ...@@ -6,6 +6,7 @@ import {
renderViaHTTP, renderViaHTTP,
File, File,
waitFor, waitFor,
launchApp,
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
...@@ -18,7 +19,7 @@ let html ...@@ -18,7 +19,7 @@ let html
const indexPage = new File(join(appDir, 'pages/static-img.js')) const indexPage = new File(join(appDir, 'pages/static-img.js'))
const runTests = () => { const runTests = (isDev = false) => {
it('Should allow an image with a static src to omit height and width', async () => { it('Should allow an image with a static src to omit height and width', async () => {
expect(await browser.elementById('basic-static')).toBeTruthy() expect(await browser.elementById('basic-static')).toBeTruthy()
expect(await browser.elementById('blur-png')).toBeTruthy() expect(await browser.elementById('blur-png')).toBeTruthy()
...@@ -43,19 +44,22 @@ const runTests = () => { ...@@ -43,19 +44,22 @@ const runTests = () => {
'public, max-age=315360000, immutable' 'public, max-age=315360000, immutable'
) )
}) })
it('Should use immutable cache-control header even when unoptimized', async () => {
await browser.eval( if (!isDev) {
`document.getElementById("static-unoptimized").scrollIntoView()` it('Should use immutable cache-control header even when unoptimized', async () => {
) await browser.eval(
await waitFor(1000) `document.getElementById("static-unoptimized").scrollIntoView()`
const url = await browser.eval( )
`document.getElementById("static-unoptimized").src` await waitFor(1000)
) const url = await browser.eval(
const res = await fetch(url) `document.getElementById("static-unoptimized").src`
expect(res.headers.get('cache-control')).toBe( )
'public, max-age=31536000, immutable' const res = await fetch(url)
) expect(res.headers.get('cache-control')).toBe(
}) 'public, max-age=31536000, immutable'
)
})
}
it('Should automatically provide an image height and width', async () => { it('Should automatically provide an image height and width', async () => {
expect(html).toContain('width:400px;height:300px') expect(html).toContain('width:400px;height:300px')
}) })
...@@ -65,12 +69,20 @@ const runTests = () => { ...@@ -65,12 +69,20 @@ const runTests = () => {
}) })
it('Should add a blurry placeholder to statically imported jpg', async () => { it('Should add a blurry placeholder to statically imported jpg', async () => {
expect(html).toContain( expect(html).toContain(
`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%;filter:blur(20px);background-size:cover;background-image:url("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoKCgoKCgsMDAsPEA4QDxYUExMUFiIYGhgaGCIzICUgICUgMy03LCksNy1RQDg4QFFeT0pPXnFlZXGPiI+7u/sBCgoKCgoKCwwMCw8QDhAPFhQTExQWIhgaGBoYIjMgJSAgJSAzLTcsKSw3LVFAODhAUV5PSk9ecWVlcY+Ij7u7+//CABEIAAgACAMBIgACEQEDEQH/xAAUAAEAAAAAAAAAAAAAAAAAAAAH/9oACAEBAAAAADX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oACAECEAAAAH//xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oACAEDEAAAAH//xAAdEAABAgcAAAAAAAAAAAAAAAATEhUAAwUUIzLS/9oACAEBAAE/AB0ZlUac43GqMYuo/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAgEBPwB//8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAwEBPwB//9k=");background-position:0% 0%"` `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%;filter:blur(20px);background-size:cover;background-image:url(${
isDev
? '"/docs/_next/image?url=%2Fdocs%2F_next%2Fstatic%2Fimage%2Fpublic%2Ftest.480a01e5ea850d0231aec0fa94bd23a0.jpg&w=8&q=70"'
: '"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoKCgoKCgsMDAsPEA4QDxYUExMUFiIYGhgaGCIzICUgICUgMy03LCksNy1RQDg4QFFeT0pPXnFlZXGPiI+7u/sBCgoKCgoKCwwMCw8QDhAPFhQTExQWIhgaGBoYIjMgJSAgJSAzLTcsKSw3LVFAODhAUV5PSk9ecWVlcY+Ij7u7+//CABEIAAgACAMBIgACEQEDEQH/xAAUAAEAAAAAAAAAAAAAAAAAAAAH/9oACAEBAAAAADX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oACAECEAAAAH//xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oACAEDEAAAAH//xAAdEAABAgcAAAAAAAAAAAAAAAATEhUAAwUUIzLS/9oACAEBAAE/AB0ZlUac43GqMYuo/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAgEBPwB//8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAwEBPwB//9k="'
});background-position:0% 0%"`
) )
}) })
it('Should add a blurry placeholder to statically imported png', async () => { it('Should add a blurry placeholder to statically imported png', async () => {
expect(html).toContain( expect(html).toContain(
`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%;filter:blur(20px);background-size:cover;background-image:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAQAAABuBnYAAAAATklEQVR42i2I0QmAMBQD869Q9K+IsxU6RkfoiA6T55VXDpJLJC9uUJIzcx+XFd2dXMbx8n+QpoeYDpgY66RaDA83jCUfVpK2pER1dcEUP+KfSBtXK+BpAAAAAElFTkSuQmCC");background-position:0% 0%"` `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%;filter:blur(20px);background-size:cover;background-image:url(${
isDev
? '"/docs/_next/image?url=%2Fdocs%2F_next%2Fstatic%2Fimage%2Fpublic%2Ftest.eba16dd4cf631c11162a5e9faeb5415b.png&w=8&q=70"'
: '"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAQAAABuBnYAAAAATklEQVR42i2I0QmAMBQD869Q9K+IsxU6RkfoiA6T55VXDpJLJC9uUJIzcx+XFd2dXMbx8n+QpoeYDpgY66RaDA83jCUfVpK2pER1dcEUP+KfSBtXK+BpAAAAAElFTkSuQmCC"'
});background-position:0% 0%"`
) )
}) })
} }
...@@ -91,15 +103,30 @@ describe('Build Error Tests for basePath', () => { ...@@ -91,15 +103,30 @@ describe('Build Error Tests for basePath', () => {
}) })
}) })
describe('Static Image Component Tests for basePath', () => { describe('Static Image Component Tests for basePath', () => {
beforeAll(async () => { describe('production mode', () => {
await nextBuild(appDir) beforeAll(async () => {
appPort = await findPort() await nextBuild(appDir)
app = await nextStart(appDir, appPort) appPort = await findPort()
html = await renderViaHTTP(appPort, '/docs/static-img') app = await nextStart(appDir, appPort)
browser = await webdriver(appPort, '/docs/static-img') html = await renderViaHTTP(appPort, '/docs/static-img')
browser = await webdriver(appPort, '/docs/static-img')
})
afterAll(() => {
killApp(app)
})
runTests()
}) })
afterAll(() => {
killApp(app) describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
html = await renderViaHTTP(appPort, '/docs/static-img')
browser = await webdriver(appPort, '/docs/static-img')
})
afterAll(() => {
killApp(app)
})
runTests(true)
}) })
runTests()
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment