Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last Updated Time in sitemap.xml #3931

Closed
4 tasks done
lemonicy opened this issue May 30, 2024 · 1 comment
Closed
4 tasks done

Last Updated Time in sitemap.xml #3931

lemonicy opened this issue May 30, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@lemonicy
Copy link

lemonicy commented May 30, 2024

Describe the bug

When generating sitemap, the lastmod attribute only uses the commit time in git log, but not lastUpdated value in frontmatter.

Reproduction

No need to add any templates.

Expected behavior

If the frontmatter contains lastUpdated key, generated sitemap should use the lastUpdated value, otherwise use the commit time in git log.

I remember the behavior was right in RC versions, not very sure.

System Info

System:
  OS: Windows 11 10.0.22631
  CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700K
  Memory: 52.88 GB / 63.77 GB
Binaries:
  Node: 20.12.2 - D:\Program Files\nodejs\node.EXE
  npm: 10.5.0 - D:\Program Files\nodejs\npm.CMD
Browsers:
  Edge: Chromium (125.0.2535.67)
  Internet Explorer: 11.0.22621.3527
npmPackages:
  vitepress: ^1.2.2 => 1.2.2

Additional context

No response

Validations

Solution (Added in 3rd June 2024)

I wrote some codes to solve this issue, maybe I can help others who searched from google.

in config.mjs or config.mts:

import fs from "fs";

export default defineConfig({
    sitemap: {
        hostname: "Your Domain",
        transformItems: (items) => {
            // Reset last updated time of sitemap.xml
            items.forEach(item => {
                const url = item.url;
                // Read the file, replace the updated time from frontmatter with the time in git log
                // My blog path is "/docs", you can replace it to yours.
                const file = "docs/" + url + ".md";
                try {
                    const data = fs.readFileSync(file, "utf-8");
                    const getUrl = data => {
                        const allLines = data.split("\n");
                        for (let i = 0; i < allLines.length; i++) {
                            const currentLine = allLines[i];
                            // the statement i > 1 can prevent the program from suspending when reading at the first line
                            if (i > 1 && currentLine === "---") {
                                console.warn("There is no lastUpdated time in Frontmatter.");
                                return null;
                            }
                            if (currentLine.indexOf("lastUpdated") >= 0) {
                                return currentLine.replace("lastUpdated:", "").trim();
                            }
                        }
                    }
                    const newUpdatedTime = getUrl(data);
                    // Only replace updated time when frontmatter value exists.
                    if (newUpdatedTime) {
                        item.lastmod = newUpdatedTime;
                    }
                } catch (err) {
                    console.error(err);
                    return false;
                }
            });
            return filteredItems;
        }
    },
});
@lemonicy lemonicy added the bug: pending triage Maybe a bug, waiting for confirmation label May 30, 2024
@lemonicy
Copy link
Author

lemonicy commented Jun 3, 2024

VitePress Doc - Last Updated - Frontmatter Config

This can be disabled per-page using the lastUpdated option on frontmatter:

---
lastUpdated: false
---

I don't mean to disable lastUpdated option.

I have written some codes to solve this issue. The solution has been added to the main post.

@brc-dd brc-dd added bug Something isn't working and removed bug: pending triage Maybe a bug, waiting for confirmation labels Jun 3, 2024
@brc-dd brc-dd closed this as completed in 7fcf462 Jun 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants