daaru00 / gridsome-plugin-i18n

Gridsome plugin for i18n
MIT License
53 stars 12 forks source link

Feature/skip pages where path is starting with locale already #43

Open chrisrueger opened 2 years ago

chrisrueger commented 2 years ago

This PR suggests a new option skipPagesStartingWithLocale (default: false)

It skips creating page clones per locale if the path already starts with a locale e.g. /en/blog/foo or /de/blog/foo

This can be the case for gridsome templates which already contain a locale-prefix coming e.g. from a .md file where people maintain the language / locale themselves (e.g. in a frontmatter block with a "lang: en" flag)

In this case those pages are already correct and can be skipped while still creating a locale-path for everything else.

What problem does it solve?

Without this option this plugin will create double-prefix the locale to pages which already have a locale-prefix. This leads to a deeply nested /dist folder where each locale contains the locale sub-folders again.

e.g.

This increases the resulting /dist folder size a lot. It works technically, because all files are there and correct, but the dist folder is larger than needed which wastes build and deployment time.

Example:

// gridsome-config.js

templates: {
Post: [
 {
  path: (node) => {
    return `/${node.lang}/blog/${node.slug}`
  }
 }
],
}

In this example the locale is take from node.lang which could come from a .md file when using @gridsome/source-filesystem.

Since template paths are different for every template / type, some could contain the locale, some not.

This might be a corner-case flag, but useful if this plugin works for 90% of existing pages, but you have some exceptions, where you have maintained you locale yourself already.

Just some background about our use case

We build a site using different datasources:

Our templates in gridsome.config.js are:

templates: {

    Tag: '/tag/:id',

    PostType: '/explore/:id',

    Post: [
      {
        path: (node) => {
          return `/${node.lang}/blog/${node.slug}`
        }
      }
    ],

    Addon: [
      {
        path: (node) => {
          return `/${node.lang}/addon/${node.slug}`
        }
      },
}

As you see some templates do have the /${node.lang} some have not. The latter work already fine with this plugin.

chrisrueger commented 2 years ago

Hi @daaru00 , any chance you could have a look at this? Thanks 😃