-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-sitemap.config.js
177 lines (147 loc) · 5.62 KB
/
next-sitemap.config.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const fs = require('fs').promises
const path = require('path')
const cheerio = require('cheerio')
/** @type {import('next-sitemap').IConfig} */
// Default code you can customize according to your requirements.
const mySiteUrl = 'https://pixiumdigital.com/';
async function getPageImages(pagePath) {
const images = []
try {
// Convert URL path to file system path
// Remove trailing slash and add .html
const htmlPath = path.join(
process.cwd(),
'dist', // or '.next/server/pages' if using server-side rendering
pagePath.replace(/\/$/, '') + '/index.html'
)
// Read the HTML file
const html = await fs.readFile(htmlPath, 'utf-8')
const $ = cheerio.load(html)
// Find all img tags
$('img').each((_, element) => {
const img = $(element)
const src = img.attr('src')
// Skip data URLs or invalid sources
if (!src || src.startsWith('data:')) return
// Create absolute URL if needed
const absoluteUrl = src.startsWith('http')
? src
: `${mySiteUrl}${src}`
images.push({
url: absoluteUrl,
title: img.attr('title') || '',
caption: img.attr('alt') || ''
})
})
// Also check for background images in style attributes
$('[style*="background-image"]').each((_, element) => {
const style = $(element).attr('style')
const match = style.match(/background-image:\s*url\(['"]?([^'"()]+)['"]?\)/)
if (match && match[1]) {
const src = match[1]
if (!src.startsWith('data:')) {
const absoluteUrl = src.startsWith('http')
? src
: `${mySiteUrl}${src}`
images.push({
url: absoluteUrl,
title: '',
caption: ''
})
}
}
})
// Remove duplicates based on URL
const uniqueImages = [...new Map(images.map(img =>
[img.url, img]
)).values()]
return uniqueImages
} catch (error) {
console.error(`Error getting images for path ${pagePath}:`, error)
return []
}
}
// If you need to add a slash when it's missing
function ensureSingleTrailingSlash(path) {
if (path.endsWith('//')) {
return path.slice(0, -1);
}
if (!path.endsWith('/')) {
return path + '/';
}
return path;
}
module.exports = {
siteUrl: ensureSingleTrailingSlash(mySiteUrl),
alternateRefs: [
{
href: `${mySiteUrl}en/`,
hreflang: 'en',
},
{
href: `${mySiteUrl}fr/`,
hreflang: 'fr',
},
],
xslUrl: 'sitemap.xsl',
xmlNs: 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"',
generateRobotsTxt: true,
sitemapIndexFileName: 'sitemap.xml', // explicitly name your sitemap index
transform: async (config, path) => {
if (!path.startsWith('/en')) {
return null;
}
// Get the images for this page/path
const images = await getPageImages(path); // You'll need to implement this function
// custom function to ignore the path
// if (customIgnoreFunction(path)) {
// return null
// }
// const priorityMap: Record<string, number> = {
// 'contact-us': 0.8,
// 'blog': 0.6,
// 'about': 0.7,
// 'privacy-policy': 0.3,
// 'terms': 0.3
// }
let _priority = 1.0;
if (path.endsWith('/') || path.endsWith('/en') || path.endsWith('/fr')) {
_priority = 1.0
} else if (path.endsWith('/contact-us')
|| path.endsWith('/blog')
|| path.endsWith('/about-us')
|| path.endsWith('/services')
|| path.endsWith('/use-case')
|| path.endsWith('/reviews') ) {
_priority = 0.9
} else {
_priority = 0.7
}
// Create the image:image tags directly in the format expected by search engines
const imageXMLElements = images.map(image => `<image:image>
<image:loc>${image.url}</image:loc>
${image.title ? `<image:title>${image.title}</image:title>` : '<image:title>Pixium</image:title>'}
${image.caption ? `<image:caption>${image.caption.replace(/[^\w\s-]/g, '')}</image:caption>` : '<image:caption>Digital</image:caption>'}
</image:image>`).join('\n');
// Create the French alternate path
path = path + "/"
const frPath = path.replace('/en/', '/fr/');
return {
loc: path, // e.g., "/en/about"
changefreq: 'weekly', // config.changefreq,
priority: _priority, //config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
// This is where the alternate urls are fixed
alternateRefs: config.alternateRefs.map((alternate) => {
// console.log(path);
// console.log(alternate);
return {
...alternate,
href: ensureSingleTrailingSlash(alternate.href + (path.substring(4) ? (path.substring(4) + '/') : '')),
hrefIsAbsolute: true,
}
}),
custom: imageXMLElements,
}
},
}