fixed sitemap rendering

This commit is contained in:
2ManyProjects 2025-05-07 21:27:31 -05:00
parent 384f5df17c
commit 9c09d34a5f

View file

@ -9,9 +9,9 @@ const SeoFile = ({ filePath }) => {
const [content, setContent] = useState('');
const [contentType, setContentType] = useState('');
const [error, setError] = useState(null);
console.log(filePath)
console.log(filePath);
useEffect(() => {
// Determine the content type based on the file extension
const fileExtension = filePath.split('.').pop();
const type = fileExtension === 'xml' ? 'application/xml' : 'text/plain';
setContentType(type);
@ -32,29 +32,18 @@ const SeoFile = ({ filePath }) => {
});
}, [filePath]);
// Set the content type and return the raw content
useEffect(() => {
if (content && contentType) {
// For XML content, we need to handle it differently than document.write
document.open();
if (contentType.includes('xml')) {
// Clear the existing document content
document.body.innerHTML = '';
document.head.innerHTML = '';
// Set the XML MIME type
const meta = document.createElement('meta');
meta.httpEquiv = 'Content-Type';
meta.content = `${contentType}; charset=utf-8`;
document.head.appendChild(meta);
// Create a pre element to display the XML with proper formatting
const pre = document.createElement('pre');
pre.textContent = content;
document.body.appendChild(pre);
// For XML styling - optional but makes it look nicer
const style = document.createElement('style');
style.textContent = `
// XML styling
document.write(`
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="${contentType}; charset=utf-8">
<style>
body {
font-family: monospace;
background: #282c34;
@ -65,23 +54,57 @@ const SeoFile = ({ filePath }) => {
white-space: pre-wrap;
word-wrap: break-word;
}
`;
document.head.appendChild(style);
</style>
</head>
<body>
<pre>${escapeHtml(content)}</pre>
</body>
</html>
`);
} else {
// For text content like robots.txt, use the standard approach
document.open();
document.write(content);
document.close();
// Set the correct content type
const meta = document.createElement('meta');
meta.httpEquiv = 'Content-Type';
meta.content = `${contentType}; charset=utf-8`;
document.head.appendChild(meta);
document.write(`
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="${contentType}; charset=utf-8">
<style>
body {
font-family: monospace;
background: #f5f5f5;
padding: 20px;
margin: 0;
}
pre {
white-space: pre;
font-size: 14px;
line-height: 1.5;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<pre>${escapeHtml(content)}</pre>
</body>
</html>
`);
}
document.close();
}
}, [content, contentType]);
// Helper function to escape HTML entities
function escapeHtml(text) {
if (!text) return '';
return text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}
// If there was an error, show a simple error message
if (error) {
return <div>{error}</div>;