Open hermanschaaf opened 1 year ago
https://github.com/iamvishnusankar/next-sitemap#custom-transformation-function
and after do some magic to get the last modified time for md/mdx file
@dimaMachina Cool! Hope can help someone
const { execSync } = require('child_process');
const fs = require('fs');
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: 'https://tsdk.dev',
generateRobotsTxt: true,
transform: async (config, path) => {
let filePath = `pages${path}.md`;
const exist = fs.existsSync(filePath);
if (!exist) {
filePath += 'x';
}
const cmd = `git log -1 --format=%ct ${filePath}`;
const result = execSync(cmd).toString();
// Use default transformation for all other cases
return {
loc: path, // => this will be exported as http(s)://<config.siteUrl>/<path>
changefreq: config.changefreq,
priority: config.priority,
lastmod: result
? new Date(+result.toString() * 1000).toISOString()
: config.autoLastmod
? new Date().toISOString()
: undefined,
alternateRefs: config.alternateRefs ?? [],
};
},
};
const { execSync } = require("child_process");
const fs = require("fs");
const packageJson = require("kitchn/package.json");
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: packageJson.homepage,
generateRobotsTxt: true,
robotsTxtOptions: {
policies: [{ userAgent: "*", allow: "/" }],
},
transform: async (config, path) => {
let filePath = `pages${path}.md`;
let exist = fs.existsSync(filePath);
if (!exist) {
filePath += "x";
exist = fs.existsSync(filePath);
}
let lastmod = config.autoLastmod ? new Date().toISOString() : undefined;
if (exist) {
try {
const cmd = `git log -1 --format=%ct ${filePath}`;
const result = execSync(cmd).toString().trim();
if (result) {
lastmod = new Date(+result * 1000).toISOString();
}
} catch (error) {
console.error(
`Error executing git command for file ${filePath}:`,
error.message,
);
}
}
return {
loc: path,
changefreq: config.changefreq,
priority: config.priority,
lastmod,
alternateRefs: config.alternateRefs ?? [],
};
},
};
We are using Nextra + Next Sitemap to update our sitemap, but noticed that the lastmod times are always the last time a deploy happened, rather than the last time the page really had any changes made to it. Apart from the sitemap, the pages themselves also say they were updated very recently, even when that's not truly the case.
I wanted to ask if the community has any solutions for dealing with this problem, other than manually updating it in each page's metadata? Is there a way to hook into Git commit history for example?