@@ -10,7 +10,6 @@ import type { IGatsbyPage } from 'gatsby/cache-dir/query-engine'
1010
1111// These are "require()"d rather than imported so the symbol names are not munged,
1212// as we need them to match the hard-coded values
13- const { readFileSync } = require ( 'fs' )
1413const { join } = require ( 'path' )
1514
1615const etag = require ( 'etag' )
@@ -19,6 +18,7 @@ const {
1918 getPagePathFromPageDataPath,
2019 getGraphQLEngine,
2120 prepareFilesystem,
21+ getErrorResponse,
2222} = require ( './utils' )
2323
2424type SSRReq = Pick < GatsbyFunctionRequest , 'query' | 'method' | 'url' > & {
@@ -39,6 +39,8 @@ export type RenderMode = 'SSR' | 'DSG'
3939 * the actual handler code, with the correct paths and render mode injected.
4040 */
4141const getHandler = ( renderMode : RenderMode , appDir : string ) : Handler => {
42+ process . chdir ( appDir )
43+
4244 const DATA_SUFFIX = '/page-data.json'
4345 const DATA_PREFIX = '/page-data/'
4446 const cacheDir = join ( appDir , '.cache' )
@@ -68,17 +70,7 @@ const getHandler = (renderMode: RenderMode, appDir: string): Handler => {
6870 graphqlEngine . findPageByPath ( pathName )
6971
7072 if ( page ?. mode !== renderMode ) {
71- const body = readFileSync ( join ( appDir , 'public' , '404.html' ) , 'utf8' )
72-
73- return {
74- statusCode : 404 ,
75- body,
76- headers : {
77- Tag : etag ( body ) ,
78- 'Content-Type' : 'text/html; charset=utf-8' ,
79- 'X-Mode' : renderMode ,
80- } ,
81- }
73+ return getErrorResponse ( { statusCode : 404 , renderMode } )
8274 }
8375 const req : SSRReq =
8476 renderMode === 'SSR'
@@ -95,48 +87,52 @@ const getHandler = (renderMode: RenderMode, appDir: string): Handler => {
9587 headers : { } ,
9688 }
9789
98- const data = await getData ( {
99- pathName,
100- graphqlEngine,
101- req,
102- } )
90+ console . log ( `[${ req . method } ] ${ event . path } (${ renderMode } )` )
91+
92+ try {
93+ const data = await getData ( {
94+ pathName,
95+ graphqlEngine,
96+ req,
97+ } )
98+ if ( isPageData ) {
99+ const body = JSON . stringify ( await renderPageData ( { data } ) )
100+ return {
101+ statusCode : 200 ,
102+ body,
103+ headers : {
104+ ETag : etag ( body ) ,
105+ 'Content-Type' : 'application/json' ,
106+ 'X-Render-Mode' : renderMode ,
107+ ...data . serverDataHeaders ,
108+ } ,
109+ }
110+ }
111+
112+ const body = await renderHTML ( { data } )
103113
104- if ( isPageData ) {
105- const body = JSON . stringify ( await renderPageData ( { data } ) )
106114 return {
107115 statusCode : 200 ,
108116 body,
109117 headers : {
110118 ETag : etag ( body ) ,
111- 'Content-Type' : 'application/json ' ,
112- 'X-Mode' : renderMode ,
119+ 'Content-Type' : 'text/html; charset=utf-8 ' ,
120+ 'X-Render- Mode' : renderMode ,
113121 ...data . serverDataHeaders ,
114122 } ,
115123 }
116- }
117-
118- const body = await renderHTML ( { data } )
119-
120- return {
121- statusCode : 200 ,
122- body,
123- headers : {
124- ETag : etag ( body ) ,
125- 'Content-Type' : 'text/html; charset=utf-8' ,
126- 'X-Mode' : renderMode ,
127- ...data . serverDataHeaders ,
128- } ,
124+ } catch ( error ) {
125+ return getErrorResponse ( { error, renderMode } )
129126 }
130127 }
131128}
132129
133130export const makeHandler = ( appDir : string , renderMode : RenderMode ) : string =>
134131 // This is a string, but if you have the right editor plugin it should format as js
135132 javascript `
136- // @ts -check
137133 const { readFileSync } = require ( 'fs' ) ;
138134 const { builder } = require ( '@netlify/functions' ) ;
139- const { getPagePathFromPageDataPath, getGraphQLEngine, prepareFilesystem } = require ( './utils' )
135+ const { getPagePathFromPageDataPath, getGraphQLEngine, prepareFilesystem, getErrorResponse } = require ( './utils' )
140136 const { join, resolve } = require ( "path" ) ;
141137 const etag = require ( 'etag' ) ;
142138 const pageRoot = resolve ( join ( __dirname , "${ appDir } " ) ) ;
0 commit comments