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 [content, setContent] = useState('');
const [contentType, setContentType] = useState(''); const [contentType, setContentType] = useState('');
const [error, setError] = useState(null); const [error, setError] = useState(null);
console.log(filePath) console.log(filePath);
useEffect(() => { useEffect(() => {
// Determine the content type based on the file extension
const fileExtension = filePath.split('.').pop(); const fileExtension = filePath.split('.').pop();
const type = fileExtension === 'xml' ? 'application/xml' : 'text/plain'; const type = fileExtension === 'xml' ? 'application/xml' : 'text/plain';
setContentType(type); setContentType(type);
@ -32,56 +32,79 @@ const SeoFile = ({ filePath }) => {
}); });
}, [filePath]); }, [filePath]);
// Set the content type and return the raw content
useEffect(() => { useEffect(() => {
if (content && contentType) { if (content && contentType) {
// For XML content, we need to handle it differently than document.write document.open();
if (contentType.includes('xml')) { if (contentType.includes('xml')) {
// Clear the existing document content // XML styling
document.body.innerHTML = ''; document.write(`
document.head.innerHTML = ''; <!DOCTYPE html>
<html>
// Set the XML MIME type <head>
const meta = document.createElement('meta'); <meta http-equiv="Content-Type" content="${contentType}; charset=utf-8">
meta.httpEquiv = 'Content-Type'; <style>
meta.content = `${contentType}; charset=utf-8`; body {
document.head.appendChild(meta); font-family: monospace;
background: #282c34;
// Create a pre element to display the XML with proper formatting color: #abb2bf;
const pre = document.createElement('pre'); padding: 20px;
pre.textContent = content; }
document.body.appendChild(pre); pre {
white-space: pre-wrap;
// For XML styling - optional but makes it look nicer word-wrap: break-word;
const style = document.createElement('style'); }
style.textContent = ` </style>
body { </head>
font-family: monospace; <body>
background: #282c34; <pre>${escapeHtml(content)}</pre>
color: #abb2bf; </body>
padding: 20px; </html>
} `);
pre {
white-space: pre-wrap;
word-wrap: break-word;
}
`;
document.head.appendChild(style);
} else { } else {
// For text content like robots.txt, use the standard approach document.write(`
document.open(); <!DOCTYPE html>
document.write(content); <html>
document.close(); <head>
<meta http-equiv="Content-Type" content="${contentType}; charset=utf-8">
// Set the correct content type <style>
const meta = document.createElement('meta'); body {
meta.httpEquiv = 'Content-Type'; font-family: monospace;
meta.content = `${contentType}; charset=utf-8`; background: #f5f5f5;
document.head.appendChild(meta); 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]); }, [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 there was an error, show a simple error message
if (error) { if (error) {
return <div>{error}</div>; return <div>{error}</div>;