Skip to content

Commit 5d44cc3

Browse files
authored
fix(web): show custom 500 page on CMS errors instead of blank page (#44)
1 parent bd097bc commit 5d44cc3

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

web/src/layout/collections/CollectionLayout.astro

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
import { getPageData } from '@/cms/getPageData'
32
import type { Locale } from '@/cms/types'
43
import LivePreviewRefreshPageScript from '@/components/LivePreviewRefreshPageScript.astro'
54
import PreviewAdminToolbar from '@/components/PreviewAdminToolbar.astro'
@@ -13,19 +12,20 @@ import ProjectLayout from './ProjectLayout.astro'
1312
1413
type Props = {
1514
pageProps: PageProps
15+
// The page data is fetched in the page route's frontmatter (not here) so that a CMS error is
16+
// thrown before the SSR response starts streaming. Otherwise Astro can no longer switch to the
17+
// custom 500 page and the visitor gets a blank page with HTTP 200.
18+
pageData: Page | Project | Article | Author
1619
locale: Locale
1720
preview?: boolean
1821
}
1922
2023
const {
21-
pageProps: { collection, id },
24+
pageProps: { collection },
25+
pageData,
2226
locale,
2327
preview,
2428
} = Astro.props
25-
26-
const pageData = await getPageData<Page | Project | Article | Author>(collection, id, locale, {
27-
preview,
28-
})
2929
---
3030

3131
<Layout lang={locale!} meta={pageData.meta}>

web/src/pages/[lang]/[...path].astro

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
2+
import { getPageData } from '@/cms/getPageData'
23
import CollectionLayout from '@/layout/collections/CollectionLayout.astro'
4+
import type { Article, Author, Page, Project } from 'cms/src/payload-types'
35
import { getStaticPaths as getStaticPathsCms } from '../../cms/getStaticPaths'
46
import { locales } from '../../cms/locales'
57
import type { Locale } from '../../cms/types'
@@ -15,6 +17,13 @@ if (!locales.includes(lang!)) {
1517
return Astro.redirect(Astro.url.pathname, 404)
1618
}
1719
const locale = lang as Locale
20+
21+
const pageData = await getPageData<Page | Project | Article | Author>(
22+
props.collection,
23+
props.id,
24+
locale,
25+
{ preview: false },
26+
)
1827
---
1928

20-
<CollectionLayout pageProps={props} preview={false} locale={locale} />
29+
<CollectionLayout pageProps={props} pageData={pageData} preview={false} locale={locale} />

web/src/pages/preview/[lang]/[...path].astro

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
---
2+
import { getPageData } from '@/cms/getPageData'
23
import { getPageProps } from '@/cms/getPageProps'
34
import { locales } from '@/cms/locales'
45
import type { Locale } from '@/cms/types'
56
import CollectionLayout from '@/layout/collections/CollectionLayout.astro'
7+
import type { Article, Author, Page, Project } from 'cms/src/payload-types'
68
79
// Preview mode, dont prerender these routes at build time
810
export const prerender = false
@@ -15,7 +17,15 @@ if (!locales.includes(lang!)) {
1517
const locale = lang as Locale
1618
const fullPath = `/${locale}${path ? '/' : ''}${path ?? ''}`
1719
20+
// Fetch the page data here in the frontmatter (before the response starts streaming) so that a CMS
21+
// error surfaces as the custom 500 page instead of a blank page with HTTP 200.
1822
const pageProps = await getPageProps(fullPath)
23+
const pageData = await getPageData<Page | Project | Article | Author>(
24+
pageProps.collection,
25+
pageProps.id,
26+
locale,
27+
{ preview: true },
28+
)
1929
---
2030

21-
<CollectionLayout pageProps={pageProps} preview={true} locale={locale} />
31+
<CollectionLayout pageProps={pageProps} pageData={pageData} preview={true} locale={locale} />

0 commit comments

Comments
 (0)