mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-18 06:49:50 +02:00
Fix text decoding when no content-type
Closes https://feedback.yaak.app/p/not-rendering-response
This commit is contained in:
@@ -16,7 +16,7 @@ export function BinaryViewer({ response }: Props) {
|
|||||||
const contentType = getContentTypeFromHeaders(response.headers) ?? 'unknown';
|
const contentType = getContentTypeFromHeaders(response.headers) ?? 'unknown';
|
||||||
|
|
||||||
// Wait until the response has been fully-downloaded
|
// Wait until the response has been fully-downloaded
|
||||||
if (response.state === 'closed') {
|
if (response.state !== 'closed') {
|
||||||
return (
|
return (
|
||||||
<EmptyStateText>
|
<EmptyStateText>
|
||||||
<LoadingIcon size="sm" />
|
<LoadingIcon size="sm" />
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import type { HttpResponse } from '@yaakapp-internal/models';
|
|||||||
import { useResponseBodyText } from '../../hooks/useResponseBodyText';
|
import { useResponseBodyText } from '../../hooks/useResponseBodyText';
|
||||||
import { languageFromContentType } from '../../lib/contentType';
|
import { languageFromContentType } from '../../lib/contentType';
|
||||||
import { getContentTypeFromHeaders } from '../../lib/model_util';
|
import { getContentTypeFromHeaders } from '../../lib/model_util';
|
||||||
import { BinaryViewer } from './BinaryViewer';
|
|
||||||
import { TextViewer } from './TextViewer';
|
import { TextViewer } from './TextViewer';
|
||||||
import { WebPageViewer } from './WebPageViewer';
|
import { WebPageViewer } from './WebPageViewer';
|
||||||
|
|
||||||
@@ -21,11 +20,6 @@ export function HTMLOrTextViewer({ response, pretty, textViewerClassName }: Prop
|
|||||||
return null;
|
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) {
|
if (language === 'html' && pretty) {
|
||||||
return <WebPageViewer response={response} />;
|
return <WebPageViewer response={response} />;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import type { HttpResponse } from '@yaakapp-internal/models';
|
|||||||
import { getResponseBodyText } from '../lib/responseBody';
|
import { getResponseBodyText } from '../lib/responseBody';
|
||||||
|
|
||||||
export function useResponseBodyText(response: HttpResponse) {
|
export function useResponseBodyText(response: HttpResponse) {
|
||||||
return useQuery<string | null>({
|
return useQuery({
|
||||||
placeholderData: (prev) => prev, // Keep previous data on refetch
|
placeholderData: (prev) => prev, // Keep previous data on refetch
|
||||||
queryKey: ['response-body-text', response.id, response.updatedAt, response.contentLength],
|
queryKey: ['response-body-text', response.id, response.updatedAt, response.contentLength],
|
||||||
queryFn: () => getResponseBodyText(response),
|
queryFn: () => getResponseBodyText(response),
|
||||||
|
|||||||
@@ -5,18 +5,14 @@ import { getCharsetFromContentType } from './model_util';
|
|||||||
import { invokeCmd } from './tauri';
|
import { invokeCmd } from './tauri';
|
||||||
|
|
||||||
export async function getResponseBodyText(response: HttpResponse): Promise<string | null> {
|
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 bytes = await readFile(response.bodyPath);
|
||||||
const charset = getCharsetFromContentType(response.headers);
|
const charset = getCharsetFromContentType(response.headers);
|
||||||
|
|
||||||
try {
|
return new TextDecoder(charset ?? 'utf-8', { fatal: false }).decode(bytes);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getResponseBodyBlob(response: HttpResponse): Promise<Uint8Array | null> {
|
export async function getResponseBodyBlob(response: HttpResponse): Promise<Uint8Array | null> {
|
||||||
|
|||||||
Reference in New Issue
Block a user