Skip to content

Commit

Permalink
feat(404): return 404 on unavailable amp pages (#80)
Browse files Browse the repository at this point in the history
feat(404): return 404 on unavailable amp pages
  • Loading branch information
farnabaz authored Oct 19, 2019
2 parents fa7957a + 79e6d76 commit fd397f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
11 changes: 11 additions & 0 deletions example/pages/noamp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
No to AMP
</div>
</template>

<script>
export default {
amp: false
}
</script>
11 changes: 8 additions & 3 deletions lib/amp/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ export default function (ctx, inject) {
if (!route.matched[0]) {
return
}

const hasAMPPrefix = route.path === '/amp' || route.path.indexOf('/amp/') === 0
const { options } = route.matched[0].components.default
const metaAMP = Array.isArray(route.meta) ? route.meta[0].amp : route.meta.amp

let ampMode = pick(
options.amp,
route.meta.amp,
metaAMP,
'<%= options.mode %>'
)

Expand All @@ -41,7 +42,7 @@ export default function (ctx, inject) {
ampMode = 'only'
break
case 'hybrid':
isAMP = route.path === '/amp' || route.path.indexOf('/amp/') === 0
isAMP = hasAMPPrefix
ampMode = 'hybrid'
break
case false:
Expand All @@ -62,6 +63,10 @@ export default function (ctx, inject) {
inject('isAMP', isAMP)
inject('ampMode', ampMode)

if (!isAMP && hasAMPPrefix) {
ctx.error({ statusCode: 404, message: 'This page could not be found' })
}

if (ampMode !== false && !options._amp) {
options.head = createCustomHead(options.head)
options.layout = createCustomLayout(options.layout, options.ampLayout)
Expand Down
10 changes: 10 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,13 @@ describe('Render AMP Story', () => {
expect(await isValid(source)).toEqual(true)
})
})

describe('Render disabled amp page', () => {
beforeAll(async () => {
await page.goto(url('/amp/noamp'))
})

test('404 for not amp pages', async () => {
await expect(page).toMatch('This page could not be found')
})
})

0 comments on commit fd397f3

Please # to comment.