Skip to content

Commit dea5dae

Browse files
authored
feat: change how handler is created (#127)
* feat: change how handler is created * chore: upgrade
1 parent ddb50cb commit dea5dae

File tree

3 files changed

+126
-124
lines changed

3 files changed

+126
-124
lines changed

plugin/src/templates/dsg/__dsg.ts

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Handler for DSR/DSG routes
2+
* Handler for DSG routes
33
*/
44

55
import { join } from 'path'
@@ -24,83 +24,85 @@ import {
2424
prepareFilesystem,
2525
} from './utils'
2626

27-
// Doing this as an import side-effect as it only needs to happen once, when bootstrapping the function
28-
prepareFilesystem()
29-
3027
type PageSSR = {
3128
getData: typeof getDataType
3229
renderHTML: typeof renderHTMLType
3330
renderPageData: typeof renderPageDataType
3431
}
3532

36-
// Requiring this dynamically so esbuild doesn't re-bundle it
37-
// eslint-disable-next-line @typescript-eslint/no-var-requires
38-
const { getData, renderHTML, renderPageData }: PageSSR = require(join(
39-
CACHE_DIR,
40-
'page-ssr',
41-
))
42-
4333
const DATA_SUFFIX = '/page-data.json'
4434
const DATA_PREFIX = '/page-data/'
4535

46-
export const handler: Handler = builder(async function handler(event) {
47-
const eventPath = event.path
36+
function getHandler(): Handler {
37+
prepareFilesystem()
38+
// Requiring this dynamically so esbuild doesn't re-bundle it
39+
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/global-require
40+
const { getData, renderHTML, renderPageData }: PageSSR = require(join(
41+
CACHE_DIR,
42+
'page-ssr',
43+
))
4844

49-
const isPageData =
50-
eventPath.endsWith(DATA_SUFFIX) && eventPath.startsWith(DATA_PREFIX)
51-
52-
const pathName = isPageData
53-
? getPagePathFromPageDataPath(eventPath)
54-
: eventPath
5545
const graphqlEngine = getGraphQLEngine()
56-
// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
57-
const page: IGatsbyPage & { mode?: string } =
58-
graphqlEngine.findPageByPath(pathName)
5946

60-
// Is it DSR or DSG? One is the old name for the other.
61-
if (!page?.mode?.startsWith('DS')) {
62-
const body = await readFile(
63-
join(process.cwd(), 'public', '404.html'),
64-
'utf8',
65-
)
47+
return async function handler(event) {
48+
const eventPath = event.path
6649

67-
return {
68-
statusCode: 404,
69-
body,
70-
headers: {
71-
Tag: etag(body),
72-
'Content-Type': 'text/html; charset=utf-8',
73-
'X-Mode': 'DSG',
74-
},
50+
const isPageData =
51+
eventPath.endsWith(DATA_SUFFIX) && eventPath.startsWith(DATA_PREFIX)
52+
53+
const pathName = isPageData
54+
? getPagePathFromPageDataPath(eventPath)
55+
: eventPath
56+
// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
57+
const page: IGatsbyPage & { mode?: string } =
58+
graphqlEngine.findPageByPath(pathName)
59+
60+
if (page?.mode !== 'DSG') {
61+
const body = await readFile(
62+
join(process.cwd(), 'public', '404.html'),
63+
'utf8',
64+
)
65+
66+
return {
67+
statusCode: 404,
68+
body,
69+
headers: {
70+
Tag: etag(body),
71+
'Content-Type': 'text/html; charset=utf-8',
72+
'X-Mode': 'DSG',
73+
},
74+
}
75+
}
76+
const data = await getData({
77+
pathName,
78+
graphqlEngine,
79+
})
80+
81+
if (isPageData) {
82+
const body = JSON.stringify(await renderPageData({ data }))
83+
return {
84+
statusCode: 200,
85+
body,
86+
headers: {
87+
ETag: etag(body),
88+
'Content-Type': 'application/json',
89+
'X-Mode': 'DSG',
90+
},
91+
}
7592
}
76-
}
77-
const data = await getData({
78-
pathName,
79-
graphqlEngine,
80-
})
8193

82-
if (isPageData) {
83-
const body = JSON.stringify(await renderPageData({ data }))
94+
const body = await renderHTML({ data })
95+
8496
return {
8597
statusCode: 200,
8698
body,
8799
headers: {
88100
ETag: etag(body),
89-
'Content-Type': 'application/json',
101+
'Content-Type': 'text/html; charset=utf-8',
90102
'X-Mode': 'DSG',
91103
},
92104
}
93105
}
106+
}
94107

95-
const body = await renderHTML({ data })
96-
97-
return {
98-
statusCode: 200,
99-
body,
100-
headers: {
101-
ETag: etag(body),
102-
'Content-Type': 'text/html; charset=utf-8',
103-
'X-Mode': 'DSG',
104-
},
105-
}
106-
})
108+
export const handler: Handler = builder(getHandler())

plugin/src/templates/ssr/__ssr.ts

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,86 +37,88 @@ type PageSSR = {
3737
renderPageData: typeof renderPageDataType
3838
}
3939

40-
prepareFilesystem()
41-
42-
// Requiring this dynamically so esbuild doesn't re-bundle it
43-
// eslint-disable-next-line @typescript-eslint/no-var-requires
44-
const { getData, renderHTML, renderPageData }: PageSSR = require(join(
45-
CACHE_DIR,
46-
'page-ssr',
47-
))
48-
49-
// eslint-disable-next-line func-style, max-statements, complexity
50-
export const handler: Handler = async function handler(event) {
51-
const eventPath = event.path
52-
const isPageData =
53-
eventPath.startsWith('/page-data/') && eventPath.endsWith('/page-data.json')
54-
55-
const pathName = isPageData
56-
? getPagePathFromPageDataPath(eventPath)
57-
: eventPath
58-
40+
function getHandler(): Handler {
41+
prepareFilesystem()
42+
// Requiring this dynamically so esbuild doesn't re-bundle it
43+
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/global-require
44+
const { getData, renderHTML, renderPageData }: PageSSR = require(join(
45+
CACHE_DIR,
46+
'page-ssr',
47+
))
5948
const graphqlEngine = getGraphQLEngine()
6049

61-
// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
62-
const page: IGatsbyPage & { mode?: string } =
63-
graphqlEngine.findPageByPath(pathName)
64-
65-
if (page?.mode !== `SSR`) {
66-
const body = await readFile(
67-
join(process.cwd(), 'public', '404.html'),
68-
'utf8',
69-
)
70-
return {
71-
statusCode: 404,
72-
body,
73-
headers: {
74-
Tag: etag(body),
75-
'Content-Type': 'text/html; charset=utf-8',
76-
'X-Mode': 'SSR',
77-
},
50+
// eslint-disable-next-line complexity
51+
return async function handler(event) {
52+
const eventPath = event.path
53+
const isPageData =
54+
eventPath.startsWith('/page-data/') &&
55+
eventPath.endsWith('/page-data.json')
56+
57+
const pathName = isPageData
58+
? getPagePathFromPageDataPath(eventPath)
59+
: eventPath
60+
61+
// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
62+
const page: IGatsbyPage & { mode?: string } =
63+
graphqlEngine.findPageByPath(pathName)
64+
65+
if (page?.mode !== `SSR`) {
66+
const body = await readFile(
67+
join(process.cwd(), 'public', '404.html'),
68+
'utf8',
69+
)
70+
return {
71+
statusCode: 404,
72+
body,
73+
headers: {
74+
Tag: etag(body),
75+
'Content-Type': 'text/html; charset=utf-8',
76+
'X-Mode': 'SSR',
77+
},
78+
}
7879
}
79-
}
8080

81-
const req: SSRReq = {
82-
query: event.queryStringParameters,
83-
method: event.httpMethod,
84-
url: event.path,
85-
headers: event.headers,
86-
}
81+
const req: SSRReq = {
82+
query: event.queryStringParameters,
83+
method: event.httpMethod,
84+
url: event.path,
85+
headers: event.headers,
86+
}
8787

88-
const data = await getData({
89-
pathName,
90-
graphqlEngine,
91-
req,
92-
})
88+
const data = await getData({
89+
pathName,
90+
graphqlEngine,
91+
req,
92+
})
93+
94+
const headers = data.serverDataHeaders || {}
95+
96+
if (isPageData) {
97+
const body = JSON.stringify(await renderPageData({ data }))
98+
return {
99+
statusCode: 200,
100+
body,
101+
headers: {
102+
ETag: etag(body),
103+
'Content-Type': 'application/json',
104+
'X-Mode': 'SSR',
105+
...headers,
106+
},
107+
}
108+
}
93109

94-
const headers = data.serverDataHeaders || {}
110+
const body = await renderHTML({ data })
95111

96-
if (isPageData) {
97-
const body = JSON.stringify(await renderPageData({ data }))
98112
return {
99113
statusCode: 200,
100114
body,
101115
headers: {
102116
ETag: etag(body),
103-
'Content-Type': 'application/json',
117+
'Content-Type': 'text/html; charset=utf-8',
104118
'X-Mode': 'SSR',
105119
...headers,
106120
},
107121
}
108122
}
109-
110-
const body = await renderHTML({ data })
111-
112-
return {
113-
statusCode: 200,
114-
body,
115-
headers: {
116-
ETag: etag(body),
117-
'Content-Type': 'text/html; charset=utf-8',
118-
'X-Mode': 'SSR',
119-
...headers,
120-
},
121-
}
122123
}
124+
export const handler: Handler = getHandler()

renovate.json5

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
automerge: true,
77
packageRules: [
88
{
9-
matchPackagePrefixes: [
10-
'@sindresorhus/'
11-
],
9+
matchPackagePrefixes: ['@sindresorhus/'],
1210
major: {
1311
enabled: false,
1412
},

0 commit comments

Comments
 (0)