Fix text decoding when no content-type

Closes https://feedback.yaak.app/p/not-rendering-response
This commit is contained in:
Gregory Schier
2025-04-21 06:54:03 -07:00
parent faca29c789
commit 6d4fdc91fe
4 changed files with 6 additions and 16 deletions

View File

@@ -16,7 +16,7 @@ export function BinaryViewer({ response }: Props) {
const contentType = getContentTypeFromHeaders(response.headers) ?? 'unknown';
// Wait until the response has been fully-downloaded
if (response.state === 'closed') {
if (response.state !== 'closed') {
return (
<EmptyStateText>
<LoadingIcon size="sm" />

View File

@@ -2,7 +2,6 @@ import type { HttpResponse } from '@yaakapp-internal/models';
import { useResponseBodyText } from '../../hooks/useResponseBodyText';
import { languageFromContentType } from '../../lib/contentType';
import { getContentTypeFromHeaders } from '../../lib/model_util';
import { BinaryViewer } from './BinaryViewer';
import { TextViewer } from './TextViewer';
import { WebPageViewer } from './WebPageViewer';
@@ -21,11 +20,6 @@ export function HTMLOrTextViewer({ response, pretty, textViewerClassName }: Prop
return null;
}
// Wasn't able to decode as text, so it must be binary
if (rawTextBody.data == null) {
return <BinaryViewer response={response} />;
}
if (language === 'html' && pretty) {
return <WebPageViewer response={response} />;
} else {

View File

@@ -3,7 +3,7 @@ import type { HttpResponse } from '@yaakapp-internal/models';
import { getResponseBodyText } from '../lib/responseBody';
export function useResponseBodyText(response: HttpResponse) {
return useQuery<string | null>({
return useQuery({
placeholderData: (prev) => prev, // Keep previous data on refetch
queryKey: ['response-body-text', response.id, response.updatedAt, response.contentLength],
queryFn: () => getResponseBodyText(response),

View File

@@ -5,18 +5,14 @@ import { getCharsetFromContentType } from './model_util';
import { invokeCmd } from './tauri';
export async function getResponseBodyText(response: HttpResponse): Promise<string | null> {
if (!response.bodyPath) return null;
if (!response.bodyPath) {
return null;
}
const bytes = await readFile(response.bodyPath);
const charset = getCharsetFromContentType(response.headers);
try {
return new TextDecoder(charset ?? 'utf-8', { fatal: true }).decode(bytes);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
// Failed to decode as text, so return null
return null;
}
return new TextDecoder(charset ?? 'utf-8', { fatal: false }).decode(bytes);
}
export async function getResponseBodyBlob(response: HttpResponse): Promise<Uint8Array | null> {