tonyboylehub 1 rok pred
rodič
commit
5e32da4619
5 zmenil súbory, kde vykonal 151 pridanie a 2 odobranie
  1. 1 1
      generate-sitemap.mjs
  2. 1 1
      package.json
  3. 4 0
      public/robots.txt
  4. 4 0
      public/sitemap.xml.old
  5. 141 0
      src/pages/sitemap.xml.js

+ 1 - 1
generate-sitemap.mjs

@@ -63,7 +63,7 @@ export async function generateSitemap() {
         .join('')}
     </urlset>`
 
-  fs.writeFileSync('src/pages/sitemap.xml', sitemap)
+  fs.writeFileSync('public/sitemap.xml', sitemap)
   console.log('Sitemap generated!')
 
 }

+ 1 - 1
package.json

@@ -4,7 +4,7 @@
   "private": true,
   "scripts": {
     "dev": "next dev",
-    "build": "node generate-sitemap.mjs && next build",
+    "build": "next build",
     "start": "next start",
     "lint": "next lint",
     "sitemap": "node generate-sitemap.mjs"

+ 4 - 0
public/robots.txt

@@ -0,0 +1,4 @@
+User-agent: * 
+Disallow:
+
+Sitemap: https://developers.metaplex.com/sitemap.xml

+ 4 - 0
src/pages/sitemap.xml → public/sitemap.xml.old

@@ -1309,6 +1309,10 @@
               <loc>https://developers.metaplex.com/security</loc>
             </url>
           
+            <url>
+              <loc>https://developers.metaplex.com/sitemap.xml</loc>
+            </url>
+          
             <url>
               <loc>https://developers.metaplex.com/stability-index</loc>
             </url>

+ 141 - 0
src/pages/sitemap.xml.js

@@ -0,0 +1,141 @@
+import fs from 'fs'
+import path from 'path'
+
+// export async function generateSitemap() {
+//   let directoryPath = path.join(process.cwd(), 'src/pages').replace(/\\/g, '/')
+
+//   let files = []
+
+//   function throughDirectory(directory) {
+//     fs.readdirSync(directory).forEach((file) => {
+//       const Absolute = path.join(directory, file)
+//       if (fs.statSync(Absolute).isDirectory()) return throughDirectory(Absolute)
+//       else return files.push(Absolute)
+//     })
+//   }
+
+//   throughDirectory(path.join(process.cwd(), 'src/pages'))
+
+//   console.log(directoryPath)
+
+//   const filter = [
+//     '/api',
+//     '_app.jsx',
+//     '_document.jsx',
+//     '_error.jsx',
+//     'sitemap.xml.js',
+//   ]
+
+//   // List of all pages to be included in the sitemap
+//   const staticPages = files
+//     .filter((staticPage) => {
+//       return !filter.some((f) => staticPage.includes(f))
+//     })
+//     .map((staticPagePath) => {
+//       const path = staticPagePath
+//         .replace(/\\/g, '/')
+//         .replace(directoryPath, '')
+//         .replace('.jsx', '')
+//         .replace('.js', '')
+//         .replace('.md', '')
+//         .replace('//', '/')
+//       return `${`https://developers.metaplex.com`}${path}`
+//     })
+
+//   // Include dynamic pages if you have any
+//   // Example: Fetch dynamic routes from your CMS or database
+
+//   // Combine static and dynamic pages
+//   const allPages = [...staticPages]
+
+//   // Generate the sitemap XML
+//   const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
+//       <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+//         ${allPages
+//           .map((url) => {
+//             return `
+//               <url>
+//                 <loc>${url}</loc>
+//               </url>
+//             `
+//           })
+//           .join('')}
+//       </urlset>`
+
+// //   fs.writeFileSync('public/sitemap.xml', sitemap)
+// //   console.log('Sitemap generated!')
+// }
+
+export async function getServerSideProps({ res }) {
+
+    // let directoryPath = path.join(process.cwd(), 'src/pages').replace(/\\/g, '/')
+
+    let directoryPath = path.join(process.cwd(), '../src/pages').replace(/\\/g, '/')
+
+    let files = []
+  
+    function throughDirectory(directory) {
+      fs.readdirSync(directory).forEach((file) => {
+        const Absolute = path.join(directory, file)
+        if (fs.statSync(Absolute).isDirectory()) return throughDirectory(Absolute)
+        else return files.push(Absolute)
+      })
+    }
+  
+    throughDirectory(path.join(process.cwd(), 'src/pages'))
+  
+    console.log(directoryPath)
+  
+    const filter = [
+      '/api',
+      '_app.jsx',
+      '_document.jsx',
+      '_error.jsx',
+      'sitemap.xml.js',
+    ]
+  
+    // List of all pages to be included in the sitemap
+    const staticPages = files
+      .filter((staticPage) => {
+        return !filter.some((f) => staticPage.includes(f))
+      })
+      .map((staticPagePath) => {
+        const path = staticPagePath
+          .replace(/\\/g, '/')
+          .replace(directoryPath, '')
+          .replace('.jsx', '')
+          .replace('.js', '')
+          .replace('.md', '')
+          .replace('//', '/')
+        return `${`https://developers.metaplex.com`}${path}`
+      })
+  
+    // Include dynamic pages if you have any
+    // Example: Fetch dynamic routes from your CMS or database
+  
+    // Combine static and dynamic pages
+    const allPages = [...staticPages]
+  
+    // Generate the sitemap XML
+    const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
+        <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+          ${allPages
+            .map((url) => {
+              return `
+                <url>
+                  <loc>${url}</loc>
+                </url>
+              `
+            })
+            .join('')}
+        </urlset>`
+
+  res.setHeader('Content-Type', 'text/xml')
+  // Send the XML to the browser
+  res.write(sitemap)
+  res.end()
+  return {
+    props: {},
+  }
+}
+export default function SiteMap() {}