Update 404/500 pages to no longer use liquid templates (#20450)

* Update 404/500 page rendering to no longer use liquid templates

* updating tests

* remove unused path

Co-authored-by: Grace Park <gracepark@github.com>
This commit is contained in:
Mike Surowiec 2021-07-21 15:22:30 -04:00 коммит произвёл GitHub
Родитель a0eff64e4f
Коммит df38087aed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 181 добавлений и 342 удалений

118
components/GenericError.tsx Normal file
Просмотреть файл

@ -0,0 +1,118 @@
import Head from 'next/head'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { MarkGithubIcon, CommentDiscussionIcon } from '@primer/octicons-react'
import { useVersion } from 'components/hooks/useVersion'
export function GenericError() {
const { isEnterprise } = useVersion()
return (
<div className="min-h-screen d-flex flex-column">
<Head>
<title>GitHub Documentation</title>
</Head>
<SimpleHeader />
<div className="container-xl p-responsive py-9 width-full flex-1">
<article className="markdown-body col-md-10 col-lg-7 mx-auto">
<h1>Ooops!</h1>
<p className="lead-mktg">It looks like something went wrong.</p>
<p className="lead-mktg">
We track these errors automatically, but if the problem persists please feel free to
contact us.
</p>
<a
id="contact-us"
href={
isEnterprise
? 'https://enterprise.github.com/support'
: 'https://support.github.com/contact'
}
className="btn btn-outline mt-2"
>
<CommentDiscussionIcon size="small" className="octicon mr-1" />
Contact support
</a>
</article>
</div>
<SimpleFooter />
</div>
)
}
export const SimpleHeader = () => {
const router = useRouter()
return (
<div className="border-bottom color-border-secondary no-print">
<header className="container-xl p-responsive py-3 position-relative d-flex width-full">
<div
className="d-flex flex-items-center"
style={{ zIndex: 3 }}
id="github-logo-mobile"
role="banner"
>
<Link href={`/${router.locale}`}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a aria-hidden="true" tabIndex={-1}>
<MarkGithubIcon size={32} className="color-icon-primary" />
</a>
</Link>
<Link href={`/${router.locale}`}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a className="h4-mktg color-text-primary no-underline no-wrap pl-2">GitHub Docs</a>
</Link>
</div>
</header>
</div>
)
}
export const SimpleFooter = () => {
return (
<footer className="py-6 text-small">
<div className="container-xl d-flex px-3 px-md-6 flex-justify-center">
<ul className="d-flex list-style-none flex-wrap flex-justify-center">
<li className="d-flex mr-xl-3 color-text-secondary">
<MarkGithubIcon className="mr-2 mr-xl-3" size={20} />
<span>&copy; {new Date().getFullYear()} GitHub, Inc.</span>
</li>
<li className="ml-3">
<a href="/github/site-policy/github-terms-of-service">Terms</a>
</li>
<li className="ml-3">
<a href="/github/site-policy/github-privacy-statement">Privacy </a>
</li>
<li className="ml-3">
<a href="https://github.com/security">Security</a>
</li>
<li className="ml-3">
<a href="https://www.githubstatus.com/">Status</a>
</li>
<li className="ml-3">
<a href="/">Help</a>
</li>
<li className="ml-3">
<a href="https://support.github.com">Contact GitHub</a>
</li>
<li className="ml-3">
<a href="https://github.com/pricing">Pricing</a>
</li>
<li className="ml-3">
<a href="/developers">Developer API</a>
</li>
<li className="ml-3">
<a href="https://services.github.com/">Training</a>
</li>
<li className="ml-3">
<a href="https://github.com/about">About</a>
</li>
</ul>
</div>
</footer>
)
}

Просмотреть файл

@ -1,9 +1,7 @@
import fs from 'fs'
import path from 'path'
import { liquid } from '../lib/render-content/index.js'
import FailBot from '../lib/failbot.js'
import loadSiteData from '../lib/site-data.js'
import builtAssets from '../lib/built-asset-urls.js'
import { nextApp } from './next.js'
function shouldLogException(error) {
const IGNORED_ERRORS = [
@ -54,15 +52,7 @@ export default async function handleError(error, req, res, next) {
// Special handling for when a middleware calls `next(404)`
if (error === 404) {
// Again, we can remove this once the 404/500 pages are ready
return res
.status(404)
.send(
await liquid.parseAndRender(
fs.readFileSync(path.join(process.cwd(), './layouts/error-404.html'), 'utf8'),
req.context
)
)
return nextApp.render404(req, res)
}
// If the error contains a status code, just send that back. This is usually
@ -75,15 +65,9 @@ export default async function handleError(error, req, res, next) {
console.error('500 error!', req.path)
console.error(error)
}
// Again, we can remove this once the 404/500 pages are ready
res
.status(500)
.send(
await liquid.parseAndRender(
fs.readFileSync(path.join(process.cwd(), './layouts/error-500.html'), 'utf8'),
req.context
)
)
res.statusCode = 500
nextApp.renderError(error, req, res, req.path)
// Report to Failbot AFTER responding to the user
await logException(error, req)

Просмотреть файл

@ -1,14 +1,11 @@
import fs from 'fs'
import path from 'path'
import { get } from 'lodash-es'
import { liquid } from '../lib/render-content/index.js'
import patterns from '../lib/patterns.js'
import getMiniTocItems from '../lib/get-mini-toc-items.js'
import Page from '../lib/page.js'
import statsd from '../lib/statsd.js'
import RedisAccessor from '../lib/redis-accessor.js'
import { isConnectionDropped } from './halt-on-dropped-connection.js'
import { nextHandleRequest } from './next.js'
import { nextApp, nextHandleRequest } from './next.js'
const { HEROKU_RELEASE_VERSION } = process.env
@ -64,10 +61,7 @@ export default async function renderPage(req, res, next) {
`\nTried to redirect to ${req.context.redirectNotFound}, but that page was not found.\n`
)
}
return res
.status(404)
// We can get rid of reading the layout for 404 once we have the 404 page up and running
.send(modifyOutput(req, await liquid.parseAndRender(fs.readFileSync(path.join(process.cwd(), './layouts/error-404.html'), 'utf8'), req.context)))
return nextApp.render404(req, res)
}
if (req.method === 'HEAD') {

1
next-env.d.ts поставляемый
Просмотреть файл

@ -1,2 +1,3 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

Просмотреть файл

@ -1,158 +1,44 @@
import { useRouter } from 'next/router'
import { SimpleHeader, SimpleFooter } from 'components/GenericError'
import Head from 'next/head'
import Link from 'next/link'
import {
MarkGithubIcon,
GitPullRequestIcon,
PeopleIcon,
CommentDiscussionIcon,
} from '@primer/octicons-react'
import { CommentDiscussionIcon } from '@primer/octicons-react'
import { useVersion } from 'components/hooks/useVersion'
import { AllProductsLink } from 'components/product/AllProductsLink'
export default function Custom404() {
const router = useRouter()
const { currentVersion, isEnterprise } = useVersion()
const contribution_href = router.locale
? `https://github.com/github/docs/edit/main/content/`
: 'https://github.com/github/docs'
const Custom404 = () => {
const { isEnterprise } = useVersion()
return (
<div className="d-lg-flex error-404">
<div className="min-h-screen d-flex flex-column">
<Head>
<title>Ooops!</title>
<title>404 - Page not found</title>
</Head>
<div className="sidebar d-none d-lg-block color-bg-tertiary position-sticky top-0 overflow-y-auto root">
<div
className="d-flex flex-items-center p-4 position-sticky top-0 color-bg-tertiary"
style={{ zIndex: 3 }}
id="github-logo"
role="banner"
>
<Link href={`/${router.locale}`}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a className="color-text-primary" aria-hidden="true" tabIndex={-1}>
<MarkGithubIcon size={32} />
</a>
</Link>
<Link href={`/${router.locale}`}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a className="h4-mktg color-text-primary no-underline no-wrap pl-2 flex-auto">
GitHub Docs
</a>
</Link>
</div>
<nav>
<ul className="sidebar-products mt-4">
{currentVersion !== 'homepage' && <AllProductsLink />}
</ul>
</nav>
<SimpleHeader />
<div className="container-xl p-responsive py-6 width-full flex-1">
<article className="markdown-body col-md-10 col-lg-7 mx-auto">
<h1>Ooops!</h1>
<p className="lead-mktg">It looks like this page doesn't exist.</p>
<p className="lead-mktg">
We track these errors automatically, but if the problem persists please feel free to
contact us.
</p>
<a
id="contact-us"
href={
isEnterprise
? 'https://enterprise.github.com/support'
: 'https://support.github.com/contact'
}
className="btn btn-outline mt-2"
>
<CommentDiscussionIcon size="small" className="octicon mr-1" />
Contact support
</a>
</article>
</div>
<main className="width-full">
<div className="container-xl p-responsive py-6">
<article className="markdown-body col-md-10 col-lg-7 mx-auto">
<h1>Ooops</h1>
<div className="lead-mktg mb-5">It looks like this page doesn't exist.</div>
<div className="col-lg-12 mt-6">
<h3 className="mb-3">Need help?</h3>
</div>
</article>
</div>
<section className="mt-lg-9 py-7 px-3 px-md-6 no-print color-bg-tertiary">
<div className="container-xl gutter-lg-spacious clearfix">
<div className="col-12 col-lg-6 col-xl-4 mb-6 mb-xl-0 float-left">
<div className="f5 contribution">
<h2 className="f4">Help us make these docs great!</h2>
<p className="color-text-secondary f6">
All GitHub docs are open source. See something that's wrong or unclear? Submit a
pull request.
</p>
<a className="btn btn-outline" href={contribution_href}>
<GitPullRequestIcon size="small" className="octicon mr-1" />
Make a contribution
</a>
<p className="color-text-secondary f6 mt-2">
Or,{' '}
<a
href="https://github.com/github/docs/blob/main/CONTRIBUTING.md"
target="_blank"
rel="noopener"
>
learn how to contribute.
</a>
</p>
</div>
</div>
<div className="col-12 col-lg-12 col-xl-4 float-left">
<div>
<h3 className="mb-2 f4">Still need help?</h3>
<a
id="ask-community"
href="https://github.community"
className="btn btn-outline mr-4 mt-2"
>
<PeopleIcon size="small" className="octicon mr-1" />
Ask the GitHub community
</a>
<a
id="contact-us"
href={
isEnterprise
? 'https://enterprise.github.com/support'
: 'https://support.github.com/contact'
}
className="btn btn-outline mt-2"
>
<CommentDiscussionIcon size="small" className="octicon mr-1" />
Contact support
</a>
</div>
</div>
</div>
</section>
<footer className="py-6 text-small">
<div className="container-xl d-flex px-3 px-md-6">
<ul className="d-flex list-style-none flex-wrap flex-justify-center flex-xl-justify-start">
<li className="d-flex mr-xl-3 color-text-secondary">
<MarkGithubIcon className="mr-2 mr-xl-3" size={20} />
<span>&copy; {new Date().getFullYear()} GitHub, Inc.</span>
</li>
<li className="ml-3">
<a href="/github/site-policy/github-terms-of-service">Terms</a>
</li>
<li className="ml-3">
<a href="/github/site-policy/github-privacy-statement">Privacy </a>
</li>
<li className="ml-3">
<a href="https://github.com/security">Security</a>
</li>
<li className="ml-3">
<a href="https://www.githubstatus.com/">Status</a>
</li>
<li className="ml-3">
<a href="/">Help</a>
</li>
<li className="ml-3">
<a href="https://support.github.com">Contact GitHub</a>
</li>
<li className="ml-3">
<a href="https://github.com/pricing">Pricing</a>
</li>
<li className="ml-3">
<a href="/developers">Developer API</a>
</li>
<li className="ml-3">
<a href="https://services.github.com/">Training</a>
</li>
<li className="ml-3">
<a href="https://github.com/about">About</a>
</li>
</ul>
</div>
</footer>
</main>
<SimpleFooter />
</div>
)
}
export default Custom404

Просмотреть файл

@ -1,159 +1,5 @@
import { useRouter } from 'next/router'
import Head from 'next/head'
import Link from 'next/link'
import {
MarkGithubIcon,
GitPullRequestIcon,
PeopleIcon,
CommentDiscussionIcon,
} from '@primer/octicons-react'
import { useVersion } from 'components/hooks/useVersion'
import { GenericError } from 'components/GenericError'
export default function Custom500() {
const router = useRouter()
const { isEnterprise } = useVersion()
const contribution_href = router.locale
? `https://github.com/github/docs/edit/main/content/`
: 'https://github.com/github/docs'
return (
<div>
<Head>
<title>GitHub Documentation</title>
</Head>
<div className="border-bottom color-border-secondary no-print">
<header className="container-xl px-3 px-md-6 pt-3 pb-2 position-relative d-flex flex-justify-between width-full">
<div
className="d-flex flex-items-center d-lg-none"
style={{ zIndex: 3 }}
id="github-logo-mobile"
role="banner"
>
<Link href={`/${router.locale}`}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a aria-hidden="true" tabIndex={-1}>
<MarkGithubIcon size={32} className="color-icon-primary" />
</a>
</Link>
<Link href={`/${router.locale}`}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a className="h4-mktg color-text-primary no-underline no-wrap pl-2">GitHub Docs</a>
</Link>
</div>
<div className="width-full">
<div className="d-inline-block width-full d-md-flex" style={{ zIndex: 1 }}>
<div className="float-right d-md-none position-relative" style={{ zIndex: 3 }}>
<div className="d-md-inline-block"></div>
</div>
</div>
</div>
</header>
</div>
<div className="container-xl p-responsive py-9">
<article className="markdown-body col-md-10 col-lg-7 mx-auto">
<h1>Ooops!</h1>
<div className="lead-mktg mb-5">
It looks like something went wrong. We track these errors automatically, but if the
problem persists please feel free to contact us.
</div>
</article>
</div>
<section className="mt-lg-9 py-7 px-3 px-md-6 no-print color-bg-tertiary">
<div className="container-xl gutter-lg-spacious clearfix">
<div className="col-12 col-lg-6 col-xl-4 mb-6 mb-xl-0 float-left">
<div className="f5 contribution">
<h2 className="f4">Help us make these docs great!</h2>
<p className="color-text-secondary f6">
All GitHub docs are open source. See something that's wrong or unclear? Submit a
pull request.
</p>
<a className="btn btn-outline" href={contribution_href}>
<GitPullRequestIcon size="small" className="octicon mr-1" />
Make a contribution
</a>
<p className="color-text-secondary f6 mt-2">
Or,{' '}
<a
href="https://github.com/github/docs/blob/main/CONTRIBUTING.md"
target="_blank"
rel="noopener"
>
learn how to contribute.
</a>
</p>
</div>
</div>
<div className="col-12 col-lg-12 col-xl-4 float-left">
<div>
<h3 className="mb-2 f4">Still need help?</h3>
<a
id="ask-community"
href="https://github.community"
className="btn btn-outline mr-4 mt-2"
>
<PeopleIcon size="small" className="octicon mr-1" />
Ask the GitHub community
</a>
<a
id="contact-us"
href={
isEnterprise
? 'https://enterprise.github.com/support'
: 'https://support.github.com/contact'
}
className="btn btn-outline mt-2"
>
<CommentDiscussionIcon size="small" className="octicon mr-1" />
Contact support
</a>
</div>
</div>
</div>
</section>
<footer className="py-6 text-small">
<div className="container-xl d-flex px-3 px-md-6">
<ul className="d-flex list-style-none flex-wrap flex-justify-center flex-xl-justify-start">
<li className="d-flex mr-xl-3 color-text-secondary">
<MarkGithubIcon className="mr-2 mr-xl-3" size={20} />
<span>&copy; {new Date().getFullYear()} GitHub, Inc.</span>
</li>
<li className="ml-3">
<a href="/github/site-policy/github-terms-of-service">Terms</a>
</li>
<li className="ml-3">
<a href="/github/site-policy/github-privacy-statement">Privacy </a>
</li>
<li className="ml-3">
<a href="https://github.com/security">Security</a>
</li>
<li className="ml-3">
<a href="https://www.githubstatus.com/">Status</a>
</li>
<li className="ml-3">
<a href="/">Help</a>
</li>
<li className="ml-3">
<a href="https://support.github.com">Contact GitHub</a>
</li>
<li className="ml-3">
<a href="https://github.com/pricing">Pricing</a>
</li>
<li className="ml-3">
<a href="/developers">Developer API</a>
</li>
<li className="ml-3">
<a href="https://services.github.com/">Training</a>
</li>
<li className="ml-3">
<a href="https://github.com/about">About</a>
</li>
</ul>
</div>
</footer>
</div>
)
return <GenericError />
}

10
pages/_error.tsx Normal file
Просмотреть файл

@ -0,0 +1,10 @@
import { NextPage } from 'next'
import { GenericError } from 'components/GenericError'
type Props = {}
const ErrorPage: NextPage<Props> = () => {
return <GenericError />
}
export default ErrorPage

Просмотреть файл

@ -13,3 +13,7 @@
.rotate-180 {
transform: rotateX(180deg);
}
.min-h-screen {
min-height: 100vh;
}

Просмотреть файл

@ -46,8 +46,8 @@ describe('browser search', () => {
expect(hits.length).toBeGreaterThan(5)
await page.setViewport(initialViewport)
})
it('works on 404 error page', async () => {
// 404 page is statically generated with next, so search is not available, but may possibly be brought back
it.skip('works on 404 error page', async () => {
await page.goto('http://localhost:4001/en/404')
await page.click('#search-input-container input[type="search"]')
await page.type('#search-input-container input[type="search"]', 'actions')

Просмотреть файл

@ -2,7 +2,6 @@ import lodash from 'lodash-es'
import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
import { get, getDOM, head, post } from '../helpers/supertest.js'
import { describeViaActionsOnly } from '../helpers/conditional-runs.js'
import path from 'path'
import { loadPages } from '../../lib/page-data.js'
import builtAssets from '../../lib/built-asset-urls.js'
import CspParse from 'csp-parse'
@ -178,7 +177,7 @@ describe('server', () => {
const $ = await getDOM('/not-a-real-page')
expect($('h1').text()).toBe('Ooops!')
expect($.text().includes("It looks like this page doesn't exist.")).toBe(true)
expect($.text().includes('Still need help?')).toBe(true)
expect($.text().includes('We track these errors automatically, but if the problem persists please feel free to contact us.')).toBe(true)
expect($.res.statusCode).toBe(404)
})
@ -201,11 +200,8 @@ describe('server', () => {
test('renders a 500 page when errors are thrown', async () => {
const $ = await getDOM('/_500')
expect($('h1').text()).toBe('Ooops!')
expect($('code').text().startsWith('Error: Intentional error')).toBe(true)
expect($('code').text().includes(path.join('node_modules', 'express', 'lib', 'router'))).toBe(
true
)
expect($.text().includes('Still need help?')).toBe(true)
expect($.text().includes("It looks like something went wrong.")).toBe(true)
expect($.text().includes("We track these errors automatically, but if the problem persists please feel free to contact us.")).toBe(true)
expect($.res.statusCode).toBe(500)
})