fixed sitemap rendering
This commit is contained in:
parent
384f5df17c
commit
9c09d34a5f
1 changed files with 67 additions and 44 deletions
|
|
@ -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,29 +32,18 @@ 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`;
|
|
||||||
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 = `
|
|
||||||
body {
|
body {
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
background: #282c34;
|
background: #282c34;
|
||||||
|
|
@ -65,23 +54,57 @@ const SeoFile = ({ filePath }) => {
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
word-wrap: break-word;
|
word-wrap: break-word;
|
||||||
}
|
}
|
||||||
`;
|
</style>
|
||||||
document.head.appendChild(style);
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre>${escapeHtml(content)}</pre>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`);
|
||||||
} 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, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
|
}
|
||||||
|
|
||||||
// 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>;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue