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

Fix: Added the content-disposition header #27521

26 changes: 26 additions & 0 deletions packages/next/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export async function imageOptimizer(
const result = setResponseHeaders(
req,
res,
url,
etag,
maxAge,
contentType,
Expand Down Expand Up @@ -310,6 +311,7 @@ export async function imageOptimizer(
sendResponse(
req,
res,
url,
maxAge,
upstreamType,
upstreamBuffer,
Expand Down Expand Up @@ -430,6 +432,7 @@ export async function imageOptimizer(
sendResponse(
req,
res,
url,
maxAge,
contentType,
optimizedBuffer,
Expand All @@ -443,6 +446,7 @@ export async function imageOptimizer(
sendResponse(
req,
res,
url,
maxAge,
upstreamType,
upstreamBuffer,
Expand Down Expand Up @@ -473,15 +477,35 @@ async function writeToCacheDir(
await promises.writeFile(filename, buffer)
}

function getFileNameWithExtension(
url: string,
contentType: string | null
): string | void {
const [urlWithoutQueryParams] = url.split('?')
styfle marked this conversation as resolved.
Show resolved Hide resolved
const fileNameWithExtension = urlWithoutQueryParams.split('/').pop()
if (!contentType || !fileNameWithExtension) {
return
}

const [fileName] = fileNameWithExtension.split('.')
const extension = getExtension(contentType)
return `${fileName}.${extension}`
styfle marked this conversation as resolved.
Show resolved Hide resolved
}

function setResponseHeaders(
req: IncomingMessage,
res: ServerResponse,
url: string,
etag: string,
maxAge: number,
contentType: string | null,
isStatic: boolean,
isDev: boolean
) {
const fileName = getFileNameWithExtension(url, contentType)
if (fileName) {
res.setHeader('Content-Disposition', `inline; filename="${fileName}"`)
}
styfle marked this conversation as resolved.
Show resolved Hide resolved
res.setHeader('Vary', 'Accept')
res.setHeader(
'Cache-Control',
Expand All @@ -502,6 +526,7 @@ function setResponseHeaders(
function sendResponse(
req: IncomingMessage,
res: ServerResponse,
url: string,
maxAge: number,
contentType: string | null,
buffer: Buffer,
Expand All @@ -512,6 +537,7 @@ function sendResponse(
const result = setResponseHeaders(
req,
res,
url,
etag,
maxAge,
contentType,
Expand Down
Loading