Skip to content

Commit

Permalink
fix(routes): reorder routes to override AMP pages (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz authored Jan 24, 2021
1 parent 07e8e81 commit a9c656d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions example/pages/amp/hello.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
Hello AMP
</div>
</template>
5 changes: 5 additions & 0 deletions example/pages/hello.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
Hello Everyone
</div>
</template>
5 changes: 4 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ function processRoutes (options) {
}
})
this.extendRoutes((routes) => {
for (const route of routes) {
let head = 0
for (const [index, route] of routes.entries()) {
route.meta = route.meta || {}
route.alias = route.alias || []
if (typeof route.alias === 'string') {
route.alias = [route.alias]
}

if (route.path === '/amp' || route.path.indexOf('/amp/') === 0) {
routes.splice(head, 0, routes.splice(index, 1)[0])
head += 1
route.meta.amp = true
} else if (!Array.isArray(options.routeAliases) || options.routeAliases.includes(route.path)) {
route.alias.push('/amp' + route.path)
Expand Down
28 changes: 27 additions & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ describe('Generates routes', () => {

test('Routes should be correctly localized', () => {
routes.forEach((route) => {
expect(route.alias).toEqual([`/amp${route.path}`])
if (!route.path.startsWith('/amp')) {
expect(route.alias).toEqual([`/amp${route.path}`])
}
})
})
})
Expand Down Expand Up @@ -123,6 +125,30 @@ describe('Render AMP version of home page', () => {
})
})

describe('Render hello page', () => {
let source
beforeAll(async () => {
const response = await page.goto(url('/hello'))
source = await response.text()
})

test('Hello Everyone', () => {
expect(source).toContain('Hello Everyone')
})
})

describe('Render AMP version of hello page', () => {
let source
beforeAll(async () => {
const response = await page.goto(url('/amp/hello'))
source = await response.text()
})

test('Hello AMP', () => {
expect(source).toContain('Hello AMP')
})
})

describe('Render AMP Story', () => {
let source, info
beforeAll(async () => {
Expand Down

0 comments on commit a9c656d

Please # to comment.