Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e541364

Browse files
author
Peter Bengtsson
authored
404 if the applicable version no longer matches (#33086)
1 parent c959a26 commit e541364

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

middleware/find-page.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ export default async function findPage(
2424
let page = req.context.pages[req.pagePath]
2525
if (page && isDev && englishPrefixRegex.test(req.pagePath)) {
2626
page = await rereadByPath(req.pagePath, contentRoot, req.context.currentVersion)
27+
28+
// This can happen if the page we just re-read has changed which
29+
// versions it's available in (the `versions` frontmatter) meaning
30+
// it might no longer be available on the current URL.
31+
if (!page.applicableVersions.includes(req.context.currentVersion)) {
32+
return res
33+
.status(404)
34+
.send(
35+
`After re-reading the page, '${req.context.currentVersion}' is no longer an applicable version. ` +
36+
'A restart is required.'
37+
)
38+
}
2739
}
2840

2941
if (page) {

tests/fixtures/page-with-redirects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ redirect_from:
44
- /some-old-path
55
versions:
66
free-pro-team: '*'
7+
ghec: '*'
78
---
8-

tests/unit/find-page-middleware.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ function makeRequestResponse(url, currentVersion = 'free-pro-team@latest') {
2323
req.context.currentVersion = currentVersion
2424
req.context.pages = {}
2525

26-
return [req, new http.ServerResponse(req)]
26+
const res = new http.ServerResponse(req)
27+
res.status = function (code) {
28+
this._status = code
29+
return {
30+
send: function (message) {
31+
this._message = message
32+
}.bind(this),
33+
}
34+
}
35+
36+
return [req, res]
2737
}
2838

2939
describe('find page middleware', () => {
@@ -73,7 +83,6 @@ describe('find page middleware', () => {
7383
})
7484
expect(req.context.page).toBeInstanceOf(Page)
7585
})
76-
7786
test('finds it for non-fpt version URLS', async () => {
7887
const [req, res] = makeRequestResponse('/en/page-with-redirects', 'enterprise-cloud@latest')
7988
req.context.pages = {
@@ -91,6 +100,27 @@ describe('find page middleware', () => {
91100
expect(req.context.page).toBeInstanceOf(Page)
92101
})
93102

103+
test("will 404 if the request version doesn't match the page", async () => {
104+
// The 'versions:' frontmatter on 'page-with-redirects.md' does
105+
// not include ghes. So this'll eventually 404.
106+
const [req, res] = makeRequestResponse('/en/page-with-redirects', 'enterprise-server@latest')
107+
req.context.pages = {
108+
'/en/page-with-redirects': await Page.init({
109+
relativePath: 'page-with-redirects.md',
110+
basePath: path.join(__dirname, '../fixtures'),
111+
languageCode: 'en',
112+
}),
113+
}
114+
115+
await findPage(req, res, () => {}, {
116+
isDev: true,
117+
contentRoot: path.join(__dirname, '../fixtures'),
118+
})
119+
expect(res._status).toBe(404)
120+
expect(res._message).toMatch('')
121+
expect(req.context.page).toBeUndefined()
122+
})
123+
94124
test('re-reads from disk if in development mode and finds nothing', async () => {
95125
const [req, res] = makeRequestResponse('/en/never/heard/of')
96126

0 commit comments

Comments
 (0)