Skip to content

Commit fd397f3

Browse files
authored
feat(404): return 404 on unavailable amp pages (#80)
feat(404): return 404 on unavailable amp pages
2 parents fa7957a + 79e6d76 commit fd397f3

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

example/pages/noamp.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<div>
3+
No to AMP
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
amp: false
10+
}
11+
</script>

lib/amp/plugin.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export default function (ctx, inject) {
2323
if (!route.matched[0]) {
2424
return
2525
}
26-
26+
const hasAMPPrefix = route.path === '/amp' || route.path.indexOf('/amp/') === 0
2727
const { options } = route.matched[0].components.default
28+
const metaAMP = Array.isArray(route.meta) ? route.meta[0].amp : route.meta.amp
2829

2930
let ampMode = pick(
3031
options.amp,
31-
route.meta.amp,
32+
metaAMP,
3233
'<%= options.mode %>'
3334
)
3435

@@ -41,7 +42,7 @@ export default function (ctx, inject) {
4142
ampMode = 'only'
4243
break
4344
case 'hybrid':
44-
isAMP = route.path === '/amp' || route.path.indexOf('/amp/') === 0
45+
isAMP = hasAMPPrefix
4546
ampMode = 'hybrid'
4647
break
4748
case false:
@@ -62,6 +63,10 @@ export default function (ctx, inject) {
6263
inject('isAMP', isAMP)
6364
inject('ampMode', ampMode)
6465

66+
if (!isAMP && hasAMPPrefix) {
67+
ctx.error({ statusCode: 404, message: 'This page could not be found' })
68+
}
69+
6570
if (ampMode !== false && !options._amp) {
6671
options.head = createCustomHead(options.head)
6772
options.layout = createCustomLayout(options.layout, options.ampLayout)

test/module.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,13 @@ describe('Render AMP Story', () => {
146146
expect(await isValid(source)).toEqual(true)
147147
})
148148
})
149+
150+
describe('Render disabled amp page', () => {
151+
beforeAll(async () => {
152+
await page.goto(url('/amp/noamp'))
153+
})
154+
155+
test('404 for not amp pages', async () => {
156+
await expect(page).toMatch('This page could not be found')
157+
})
158+
})

0 commit comments

Comments
 (0)