ekoeryanto / vuepress-plugin-sitemap

Sitemap generator plugin for vuepress.
MIT License
93 stars 19 forks source link

[locale] RangeError: Invalid time value #16

Closed maxacarvalho closed 4 years ago

maxacarvalho commented 5 years ago

Hello,

Apparently, there's an issue related to the page.lastUpdated data.
At index.js:54

        const lastmodISO = page.lastUpdated
          ? new Date(page.lastUpdated).toISOString()
          : undefined

It throws an error like:

 SITEMAP  Generating sitemap...
error vuepress-plugin-sitemap apply generated failed.
RangeError: Invalid time value
    at Date.toISOString (<anonymous>)
    at pages.forEach.page (/Users/maxacarvalho/Code/kooomo/it.intranet.kooomo.com/node_modules/vuepress-plugin-sitemap/index.js:54:40)
    at Array.forEach (<anonymous>)
    at generated (/Users/maxacarvalho/Code/kooomo/it.intranet.kooomo.com/node_modules/vuepress-plugin-sitemap/index.js:52:13)
    at AsyncOption.asyncApply (/Users/maxacarvalho/Code/kooomo/it.intranet.kooomo.com/node_modules/@vuepress/core/lib/node/plugin-api/abstract/AsyncOption.js:33:21)
    at PluginAPI.applyAsyncOption (/Users/maxacarvalho/Code/kooomo/it.intranet.kooomo.com/node_modules/@vuepress/core/lib/node/plugin-api/index.js:272:32)
    at Build.render (/Users/maxacarvalho/Code/kooomo/it.intranet.kooomo.com/node_modules/@vuepress/core/lib/node/build/index.js:100:34)
error Command failed with exit code 1.

I'm using Vuepress version 1.x ("vuepress": "^1.0.0-alpha.47")

Thanks.

ekoeryanto commented 5 years ago

can you give us the output of the page.lastUpdated it self or have you a repo for us to reproduce the issue?

Thanks.

osdio commented 5 years ago

I think it caused by using @vuepress/last-updated which you change the page.lastUpdated.

Such as:

        [
          '@vuepress/last-updated',
          {
            transformer: (timestamp, lang) => {
              const moment = require('moment')
              moment.locale(lang)
              return moment(timestamp).fromNow()
            }
          }
        ]
maxacarvalho commented 5 years ago

Hi, first of all I beg your pardon because of the lack of update about this issue, I was dealing with a different project for the past few months and couldn't check the issue.

I found that it, probably, has to do with localisation. Since I require to use en-GB as default language, the default output of the last-updated plugin will be something like 18/02/2019, 09:54:26 which will then throw the RangeError: Invalid time value exception when trying to new Date(page.lastUpdated).toISOString() // new Date(18/02/2019, 09:54:26).toISOString()

I added a fix like:

  plugins: [
    [
      '@vuepress/last-updated',
      {
        transformer: (timestamp, lang) => {
          const moment = require('moment')
          moment.locale(lang)
          return moment(timestamp).format("MMMM DD YYYY HH:mm")
        }
      }
    ],
    ['sitemap', { hostname: 'https://path-to-docs' }]
  ],

This issue could be solved with the localisation proposal, maybe using moment as a dependency.

ekoeryanto commented 4 years ago

it fixed with dateFormatter option in v2.3.0

Genuifx commented 3 years ago

"vuepress-plugin-sitemap": "^2.3.1",

bug still alive.

Genuifx commented 3 years ago

The following code is work too~

transformer: (timestamp, lang) => {
  return new Date(timestamp).toLocaleDateString();
}
z979054461 commented 3 months ago

This works for me.

        [
            'sitemap',
            {
                hostname: 'xxx',
+                dateFormatter: lastUpdated => {
+                   return new Date(lastUpdated.replace(/[上|下]午/g, '')).toISOString()
+                },
            },
        ],