Unverified Commit 36b81f98 authored by Paweł Tymczuk's avatar Paweł Tymczuk Committed by GitHub

Fix: Added the content-disposition header (#27521)

In this PR I've added the `Content-Disposition` header to the response of the image `/_next/image` route. That header is used by the browser when showing the dialog for the `Save image as...` action. 

There are some differences between the browsers, ex:
When requesting the image `test.jpg`,  the response header `Content-Type: image/webp` - in FF the filename from the `Save image as...` dialog would be `test.webp` but in Chrome `test.jpg` even if the `Content-Disposition: inline; filename="test.webp"` is present in the headers.  The same about png images, the rest types are fine.  It looks like FF is checking the `Content-Type` for the extension but the Chrome does not and is doing another type of check.

Fixes #26737

## Bug

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

## Documentation / Examples

- [x] Make sure the linting passes
parent 1af78928
...@@ -194,6 +194,7 @@ export async function imageOptimizer( ...@@ -194,6 +194,7 @@ export async function imageOptimizer(
const result = setResponseHeaders( const result = setResponseHeaders(
req, req,
res, res,
url,
etag, etag,
maxAge, maxAge,
contentType, contentType,
...@@ -310,6 +311,7 @@ export async function imageOptimizer( ...@@ -310,6 +311,7 @@ export async function imageOptimizer(
sendResponse( sendResponse(
req, req,
res, res,
url,
maxAge, maxAge,
upstreamType, upstreamType,
upstreamBuffer, upstreamBuffer,
...@@ -430,6 +432,7 @@ export async function imageOptimizer( ...@@ -430,6 +432,7 @@ export async function imageOptimizer(
sendResponse( sendResponse(
req, req,
res, res,
url,
maxAge, maxAge,
contentType, contentType,
optimizedBuffer, optimizedBuffer,
...@@ -443,6 +446,7 @@ export async function imageOptimizer( ...@@ -443,6 +446,7 @@ export async function imageOptimizer(
sendResponse( sendResponse(
req, req,
res, res,
url,
maxAge, maxAge,
upstreamType, upstreamType,
upstreamBuffer, upstreamBuffer,
...@@ -473,9 +477,25 @@ async function writeToCacheDir( ...@@ -473,9 +477,25 @@ async function writeToCacheDir(
await promises.writeFile(filename, buffer) await promises.writeFile(filename, buffer)
} }
function getFileNameWithExtension(
url: string,
contentType: string | null
): string | void {
const [urlWithoutQueryParams] = url.split('?')
const fileNameWithExtension = urlWithoutQueryParams.split('/').pop()
if (!contentType || !fileNameWithExtension) {
return
}
const [fileName] = fileNameWithExtension.split('.')
const extension = getExtension(contentType)
return `${fileName}.${extension}`
}
function setResponseHeaders( function setResponseHeaders(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
url: string,
etag: string, etag: string,
maxAge: number, maxAge: number,
contentType: string | null, contentType: string | null,
...@@ -496,12 +516,19 @@ function setResponseHeaders( ...@@ -496,12 +516,19 @@ function setResponseHeaders(
if (contentType) { if (contentType) {
res.setHeader('Content-Type', contentType) res.setHeader('Content-Type', contentType)
} }
const fileName = getFileNameWithExtension(url, contentType)
if (fileName) {
res.setHeader('Content-Disposition', `inline; filename="${fileName}"`)
}
return { finished: false } return { finished: false }
} }
function sendResponse( function sendResponse(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
url: string,
maxAge: number, maxAge: number,
contentType: string | null, contentType: string | null,
buffer: Buffer, buffer: Buffer,
...@@ -512,6 +539,7 @@ function sendResponse( ...@@ -512,6 +539,7 @@ function sendResponse(
const result = setResponseHeaders( const result = setResponseHeaders(
req, req,
res, res,
url,
etag, etag,
maxAge, maxAge,
contentType, contentType,
......
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