Skip to content

Commit

Permalink
Make early-access link 404 in client-side nav (#50044)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Apr 8, 2024
1 parent a014ba8 commit 2a6acbf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/workflows/local-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
- name: Install dependencies
run: npm install

- uses: ./.github/actions/get-docs-early-access
if: ${{ github.repository == 'github/docs-internal' }}
with:
token: ${{ secrets.DOCS_BOT_PAT_READPUBLICKEY }}

# Note that we don't check out docs-early-access, Elasticsearch,
# or any remote translations. Nothing fancy here!

Expand All @@ -42,6 +47,7 @@ jobs:
# tests not exiting at the end with a non-zero. Otherwise,
# by default failures are marked as "flaky" instead of "failed".
PLAYWRIGHT_RETRIES: 0
TEST_EARLY_ACCESS: ${{ github.repository == 'github/docs-internal' }}
run: npm run playwright-test -- playwright-local-dev

- name: Start server in the background
Expand Down
15 changes: 15 additions & 0 deletions src/early-access/middleware/early-access-links.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { uniq } from 'lodash-es'

export default function earlyAccessContext(req, res, next) {
// Use req.pagePath instead of req.path because req.path is the path
// normalized after "converting" that `/_next/data/...` path to the
// equivalent path if it had *not* been a client-side routing fetch.
const url = req.pagePath.split('/').slice(2)
if (
!(
// Is it `/early-access` or `/enterprise-cloud@latest/early-access`?
(
(url.length === 2 && url[1] === 'early-access') ||
(url.length === 1 && url[0] === 'early-access')
)
)
) {
return next()
}
if (process.env.NODE_ENV !== 'development') {
return next(404)
}
Expand Down
5 changes: 5 additions & 0 deletions src/early-access/tests/early-access-rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe('rendering early-access', () => {
expect(res.statusCode).toBe(404)
})

testViaActionsOnly('the enterprise-cloud TOC is always 404', async () => {
const res = await get('/en/enterprise-cloud@latest/early-access')
expect(res.statusCode).toBe(404)
})

testViaActionsOnly('TOCs display on category pages', async () => {
const $ = await getDOM('/en/early-access/github/enforcing-best-practices-with-github-policies')
expect($('ul a').length).toBeGreaterThan(5)
Expand Down
15 changes: 15 additions & 0 deletions src/fixtures/tests/playwright-local-dev.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import { test, expect } from '@playwright/test'

const TEST_EARLY_ACCESS = Boolean(JSON.parse(process.env.TEST_EARLY_ACCESS || 'false'))

test('view home page', async ({ page }) => {
await page.goto('/')
await expect(page).toHaveTitle(/GitHub Docs/)
Expand All @@ -32,3 +34,16 @@ test('search "git" and get results', async ({ page }) => {
await page.getByTestId('site-search-input').press('Enter')
await expect(page.getByRole('heading', { name: /\d+ Search results for "git"/ })).toBeVisible()
})

test('view the early-access links page', async ({ page }) => {
if (!TEST_EARLY_ACCESS) return

await page.goto('/early-access')
await expect(page).toHaveURL(/\/en\/early-access/)
await page.getByRole('heading', { name: 'Early Access documentation' }).click()
const links = await page.$$eval(
'#article-contents ul li a',
(elements: HTMLAnchorElement[]) => elements,
)
expect(links.length).toBeGreaterThan(0)
})
2 changes: 1 addition & 1 deletion src/frame/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default function (app) {
app.use(haltOnDroppedConnection)

app.use(robots)
app.use(/(\/.*)?\/early-access$/, earlyAccessLinks)
app.use(earlyAccessLinks)
app.use('/categories.json', asyncMiddleware(categoriesForSupport))
app.get('/_500', asyncMiddleware(triggerError))

Expand Down

0 comments on commit 2a6acbf

Please sign in to comment.