fix: Decode HTML entities in chat responses and improve n8n error handling
Some checks failed
Dev Deployment (Zero Downtime) / deploy-dev (push) Has been cancelled
Some checks failed
Dev Deployment (Zero Downtime) / deploy-dev (push) Has been cancelled
- Add HTML entity decoding for chat responses (fixes ' display issue) - Add timeout handling for n8n webhook requests (30s chat, 10s status) - Improve error logging with detailed error information - Add N8N_SECRET_TOKEN support for authentication - Better fallback handling when n8n is unavailable - Fix server-side HTML entity decoding for chat and status endpoints
This commit is contained in:
41
lib/html-decode.ts
Normal file
41
lib/html-decode.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Decode HTML entities in strings
|
||||
* Converts ' " & < > etc. to their actual characters
|
||||
*/
|
||||
export function decodeHtmlEntities(text: string): string {
|
||||
if (!text || typeof text !== 'string') {
|
||||
return text;
|
||||
}
|
||||
|
||||
// Create a temporary element to decode HTML entities
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.innerHTML = text;
|
||||
return textarea.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Server-side HTML entity decoding (for Node.js/Next.js API routes)
|
||||
*/
|
||||
export function decodeHtmlEntitiesServer(text: string): string {
|
||||
if (!text || typeof text !== 'string') {
|
||||
return text;
|
||||
}
|
||||
|
||||
// Map of common HTML entities
|
||||
const entityMap: Record<string, string> = {
|
||||
''': "'",
|
||||
'"': '"',
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
''': "'",
|
||||
''': "'",
|
||||
'/': '/',
|
||||
'`': '`',
|
||||
'=': '=',
|
||||
};
|
||||
|
||||
return text.replace(/&[#\w]+;/g, (entity) => {
|
||||
return entityMap[entity] || entity;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user