Skip to content

Commit

Permalink
feat(custom-routes): create an option to define AMP pages
Browse files Browse the repository at this point in the history
Add routeAliases support
  • Loading branch information
farnabaz authored Mar 24, 2020
2 parents c0f694f + 60cc5c3 commit 81c5717
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ General options of amp module
| tags | `{}` | Object of tag options | Define new tags or modify current tags, for instance if you want to use `amp-mustache` version 0.1, tags value must be `{ 'amp-mustache': { version: '0.1' } }` |
| origin | `` | Any String | Main domain of website. Using this AMP modules tries to add missing canonical link for pages. |
| mode | `hybrid` | `only\|hybrid\|false` | Default behaviour of amp module. (`only` all pages serve in AMP mode by default, `hybrid` pages serves in both normal and AMP mode, `false` pages does not serve AMP by default ) |
| routeAliases | `auto` | `auto\|Array` | Allows to limit route aliases to only AMP pages. With `auto` the module will create aliases for every route. If your app uses AMP only on a few routes you can provide those routes into an Array. Routes are absolute, without '/amp' prefix, eg. `['/story', '/page2']` |


## Page Options
Expand Down
7 changes: 4 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function (moduleOptions) {
origin: '',
mode: 'hybrid',
tags: {},
routeAliases: 'auto',
...this.options.amp,
...moduleOptions
}
Expand All @@ -31,10 +32,10 @@ module.exports = function (moduleOptions) {
registerValidator.call(this, options)
}

processRoutes.call(this)
processRoutes.call(this, options)
}

function processRoutes () {
function processRoutes (options) {
this.extendRoutes((routes) => {
for (const route of routes) {
route.meta = route.meta || {}
Expand All @@ -45,7 +46,7 @@ function processRoutes () {

if (route.path === '/amp' || route.path.indexOf('/amp/') === 0) {
route.meta.amp = true
} else {
} else if (!Array.isArray(options.routeAliases) || options.routeAliases.includes(route.path)) {
route.alias.push('/amp' + route.path)
}
}
Expand Down

0 comments on commit 81c5717

Please # to comment.