-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsitemap-xml.html
122 lines (99 loc) · 3.82 KB
/
sitemap-xml.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" id="meta-description" content="">
<meta name="author" id="meta-author" content="">
<meta name="robots" content="index, follow">
<title id="page-title">XML Sitemap</title>
</head>
<body>
<h1>XML Sitemap</h1>
<pre id="xml-sitemap"></pre> <!-- Tempat untuk menampilkan XML -->
<script>
async function fetchPosts() {
try {
const response = await fetch('/.netlify/functions/readPosts');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const posts = await response.json();
console.log('Posts fetched:', posts);
const escapeXML = str => str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
const baseUrl = window.location.origin;
let xml = `<?xml version="1.0" encoding="UTF-8"?>\n`;
xml += `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n`;
posts.forEach(post => {
//const url = `${baseUrl}/post/${escapeXML(post.data.slug)}`;
const url = `${baseUrl}/static/${escapeXML(post.data.slug)}`;
const lastmod = new Date(post.created_at).toISOString();
xml += `
<url>
<loc>${url}</loc>
<lastmod>${lastmod}</lastmod>
<priority>0.5</priority>
</url>\n
`;
});
xml += `</urlset>`;
// Tampilkan XML di elemen <pre>
document.getElementById('xml-sitemap').textContent = xml;
// Optionally, you can download the XML as a file by prompting the user to save it
const blob = new Blob([xml], { type: 'application/xml' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'sitemap.xml';
link.innerText = 'Download XML Sitemap file';
document.body.appendChild(link);
// Kirim sitemap ke GitHub melalui Netlify Function
await saveSitemapToGitHub(xml);
} catch (error) {
console.error('Error fetching posts:', error);
document.getElementById('xml-sitemap').textContent = `Error fetching posts: ${error.message}`;
}
}
async function saveSitemapToGitHub(xml) {
try {
const response = await fetch('/.netlify/functions/saveSitemap', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ sitemap: xml }),
});
if (!response.ok) {
throw new Error(`Error saving sitemap: ${response.statusText}`);
}
const result = await response.json();
console.log('Sitemap saved to GitHub:', result.message);
} catch (error) {
console.error('Error saving to GitHub:', error);
}
}
// Fetch posts saat DOM selesai dimuat
document.addEventListener('DOMContentLoaded', fetchPosts);
</script>
<br>Di bagian terbawah halaman ini ada link <b>Download XML Sitemap file</b>.
<br>Klik link tersebut untuk mendownload file sitemap.xml.
<br>Di belakang layar, sitemap-xml.html ini akan membuat data sitemap.xml,
<br>dan akan mengirim file sitemap.xml tersebut ke github repo ini untuk disimpan/replace
<br>melalui fungsi netlify saveSitemap.js ,
<br>process deploy netlify ke github akan membutuhkan waktu 2-5 menit.
<br><br><br>
<footer>
<nav class="footer-menu">
<a href="/">Home</a>
<a href="/sitemap">HTML Sitemap</a>
<a href="/sitemap-xml">XML Sitemap Generator ini</a>
<a href="/sitemap.xml">Lihat XML Sitemap</a>
</nav>
</footer>
<br><br><br>
</body>
</html>