fix: grafikProxy gibt 404 statt HTML zurück für JS/CSS-Assets bei Upstream-Fehler
Verhindert MIME-Type-Fehler im Browser wenn upstream-Server für JS/CSS-Dateien eine HTML-Fehlerseite zurückgibt. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -93,7 +93,17 @@ export async function grafikProxy(req: Request, slug?: string[]) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const contentType = upstream.headers.get('content-type') || outHeaders['content-type'] || '';
|
const contentType = upstream.headers.get('content-type') || outHeaders['content-type'] || '';
|
||||||
if (contentType.toLowerCase().includes('text/html')) {
|
const isHtmlResponse = contentType.toLowerCase().includes('text/html');
|
||||||
|
|
||||||
|
// JS/CSS/font assets that come back as HTML are upstream errors (404, redirect pages).
|
||||||
|
// Return a 404 so the browser doesn't refuse to execute them as wrong MIME type.
|
||||||
|
const assetExtension = /\.(js|css|woff2?|ttf|eot|png|jpg|gif|svg|ico)(\?|$)/i;
|
||||||
|
const requestPath = new URL(req.url).pathname;
|
||||||
|
if (isHtmlResponse && slug?.length && assetExtension.test(requestPath)) {
|
||||||
|
return new NextResponse(null, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isHtmlResponse) {
|
||||||
const text = Buffer.from(bodyBuf).toString('utf8');
|
const text = Buffer.from(bodyBuf).toString('utf8');
|
||||||
const cleaned = rewriteHtml(text);
|
const cleaned = rewriteHtml(text);
|
||||||
outHeaders['content-type'] = 'text/html; charset=utf-8';
|
outHeaders['content-type'] = 'text/html; charset=utf-8';
|
||||||
|
|||||||
Reference in New Issue
Block a user