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

Add lastmod datetime #733

Closed
bep opened this issue Dec 21, 2014 · 24 comments
Closed

Add lastmod datetime #733

bep opened this issue Dec 21, 2014 · 24 comments

Comments

@bep
Copy link
Member

bep commented Dec 21, 2014

There is date (default sort) publishdate (when it is published). #726 want expirydate.

I want lastmod -- to be used in sitemap.xml etc. I often make changes to my content, but I do not want to bring it to the top just to notify Google about these changes.

It should by default be set to the vale of date, but should be overridable in frontmatter. Maybe "hugo new" should add this as well?

@byronwilliams
Copy link

@bjornerik I also want lastmod for my sitemap and am happy to implement it. Does the following sound reasonable?

  1. Always add a field to the sitemap
  2. Use the front matter publishDate to fill the if present
  3. Alternatively get the actual modification time of the markdown file

@bep
Copy link
Member Author

bep commented Jan 6, 2015

Great!

  1. In sitemap code, just replace node.Date with node.Lastmod
  2. No, the date is better, I think. Not 100% sure -- but that's more in line with how it is today. Date is normally the timestamp on creation.
  3. Yes.

@bep
Copy link
Member Author

bep commented Feb 17, 2015

@reactor10 need any more input on this? This would be very nice to have.

@jbrodriguez
Copy link
Contributor

Hi, is there any update on this ?

Sometimes, I update an article to refresh the content or if something major happened (like Google Code hosting shutting down), and I'd like to have a Last Modified date to show.

Will appreciate any feedback.

@bep
Copy link
Member Author

bep commented Apr 18, 2015

@reactor10 may know.

@byronwilliams
Copy link

Sorry guys. I got stuck with the implementation as I couldn't work out where best to put the timestamp. Here's a branch I was working on: master...reactor10:byron-lastmod

My question @bep: When the sitemap is generated I cannot access the full original path in order to do an os.Stat, it therefore must be written a variable in the Page struct - is this OK?

@bep
Copy link
Member Author

bep commented Apr 19, 2015

@reactor10 for many reasons it should be a field on Node, Node.Lastmod.

@rhewitt22
Copy link

Is the scope of this issue only to add Lastmod to the sitemap? I'd like the ability to use this on pages, too. Would using Node.Lastmod preclude that?

@bep
Copy link
Member Author

bep commented Apr 21, 2015

The scope is:

  • add lastmod to Node
  • for Page:
    • If lastmod is set in front matter, use that value, else set it to the value of date
  • for Node
    • Use the same logic that is used for date, but for lastmod (fetch the value from the "last" Page)

@bep
Copy link
Member Author

bep commented Apr 29, 2015

@reactor10 any progress on this?

@jbrodriguez
Copy link
Contributor

@bep I took @reactor10's code as a base and made some modifications, to get lastmod working on Page. Here's the branch master...jbrodriguez:jbrodriguez-lastmod
If that's ok with @reactor10, I can move forward based on this and your review of the code, to check if I'm on the right path

@bep bep added this to the v0.14 milestone May 12, 2015
@byronwilliams
Copy link

Sorry that my communication has been poor (nonexistent), thanks for
stepping up - it looks good @jbrodriguezeen[1]

Byron Williams byron@112percent.com

On Tue, 12 May 2015, at 14:42, Juan B. Rodriguez wrote:

@bep[2] I took @reactor10[3]'s code as a base and made some
modifications, to get lastmod working on Page. Here's the branch
master...jbrodriguez:jbrodriguez-lastmod[4]

If that's ok with @reactor10[5], I can move forward based on this and
your review of the code, to check if I'm on the right path

— Reply to this email directly or view it on GitHub[6].

Links:

  1. master...jbrodriguez:jbrodriguez-lastmod
  2. https://github.com/bep
  3. https://github.com/reactor10
  4. master...jbrodriguez:jbrodriguez-lastmod
  5. https://github.com/reactor10
  6. Add lastmod datetime #733 (comment)

@bep
Copy link
Member Author

bep commented May 12, 2015

@jbrodriguez it doesn't look like what I specified. It's not like what I say must be right all the time, but I said something about:

  • Put it on Node (not Page, not File)
  • Follow the logic used currently for date
  • Use the value set in front matter, if not set use the same value as date

@jbrodriguez
Copy link
Contributor

@bep Let me check, I seem to have a general idea on how to put it in Node.

One question, eventually, from a page template (let's say _default/single.html), one would refer to Lastmod by

{{ .Node.Lastmod.Format "20060102-15:04:05.000" }} ?

@bep
Copy link
Member Author

bep commented May 12, 2015

{{ .Lastmod.Format "20060102-15:04:05.000" }}

This should work on both Page and Node.

The distinction here is confusing in Hugo. Page = content pages, Node = list pages.

In technical terms, the Node struct is embedded in Page (this is a very cool Go construct that encourages composition (in contrast to inheritance)).

So, if you look at Node.Date - this is only defined on Node, but can also be accessed from Page.

But since Node (home page, list pages) has no physical file connected to it, the value for Date (and Lastmod) must be derived (last post) for the list pages -- but fetched from front matter for the content pages.

@jbrodriguez
Copy link
Contributor

@bep That's a very cool language feature, didn't know about it.

Here's a branch that follows your specs master...jbrodriguez:jbrodriguez-lastmod-3

If Lastmod is set in front matter, it picks it up; if not set, it assigns Date to Lastmod.

The only issue I see here is that Date == "file creation date", whereas file modification date feels like a better fit.

Please let me know your comments.

@bep
Copy link
Member Author

bep commented May 14, 2015

@jbrodriguez this looks very good. Just two comments:

  1. In site.go -> BuildSiteMeta. s.Info.LastChange must be set to Lastmod
  2. Related to the above, and I didn't think about this until now: I think we should add another field s.Info.LastCreated and put the last Date into that one. And then use that value in RenderSitemap.

Finally - if you could also change the sitemap.xml template in /tpl/template_embedded.go from .Date => .Lastmod I would be perfectly happy. That was the reason I opened up this issue in the first place.

@bep
Copy link
Member Author

bep commented May 14, 2015

No, drop the s.Info.LastCreated for now -- but make sure that s.Info.LastChange gets the Lastmod value.

@jbrodriguez
Copy link
Contributor

Ok, I've made the changes (same branch master...jbrodriguez:jbrodriguez-lastmod-3)

Two comments:

1 - There are other internal templates that should probably be modified as well:

  • rss.xml
    lastBuildDate
  • opengraph.html
    og -> meta property="og:updated_time"
    facebook -> meta property="article:modified_time"
    twitter -> meta itemprop="dateModified"

2 - This code

    if len(t.pages) > 0 {
        n.Date = t.pages[0].Page.Date
        n.Lastmod = t.pages[0].Page.Lastmod
    }

t.pages is a WeightedPages, so it's sorted by user's definition (defaults to Date?).

But adding Lastmod means t.pages[0] not necessarily has the "latest" Lastmod.

If that's the case, perhaps we should scan t.pages or sort a copy of t.pages, to find the latest Lastmod.

Let me know how you see this.

@bep
Copy link
Member Author

bep commented May 14, 2015

@jbrodriguez - this looks good. Create a pull request and I will pull it in.

The rest of your comments are good, but let us take those in #1141

@bep bep closed this as completed in 3882e7c May 15, 2015
bep pushed a commit that referenced this issue May 15, 2015
Set sitemap.xml <lastmod> field to Lastmod.

See #733
@riklopfer
Copy link

Have the user docs been updated with this change? I don't see anything on the template variables page.

@bep
Copy link
Member Author

bep commented Nov 19, 2015

@riklopfer #1590

@riklopfer
Copy link

Okay cool. It's not clear to me how to order by that field though. I checked the docs here.

From here, I got to this bit of code.

{{ range sort .Data.Pages "Lastmod" "desc" }}

which yields this exception when I attempt to build.

ERROR: 2015/11/19 Error while rendering sitemap: reflect: call of reflect.Value.FieldByName on ptr Value

which led me to #1330 -- I guess I will wait for v 0.15

tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
Create new field in Node
Update Page to look for lastmod field in the front matter. If not present, then assign Date to Lastmod
Update Site, to assign a value to Lastmod (based on the same logic used for Date)

Fixes gohugoio#733
tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
Set sitemap.xml <lastmod> field to Lastmod.

See gohugoio#733
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants