Skip to content

Commit

Permalink
feat: change how handler is created (#127)
Browse files Browse the repository at this point in the history
* feat: change how handler is created

* chore: upgrade
  • Loading branch information
ascorbic authored Sep 27, 2021
1 parent ddb50cb commit dea5dae
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 124 deletions.
114 changes: 58 additions & 56 deletions plugin/src/templates/dsg/__dsg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Handler for DSR/DSG routes
* Handler for DSG routes
*/

import { join } from 'path'
Expand All @@ -24,83 +24,85 @@ import {
prepareFilesystem,
} from './utils'

// Doing this as an import side-effect as it only needs to happen once, when bootstrapping the function
prepareFilesystem()

type PageSSR = {
getData: typeof getDataType
renderHTML: typeof renderHTMLType
renderPageData: typeof renderPageDataType
}

// Requiring this dynamically so esbuild doesn't re-bundle it
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { getData, renderHTML, renderPageData }: PageSSR = require(join(
CACHE_DIR,
'page-ssr',
))

const DATA_SUFFIX = '/page-data.json'
const DATA_PREFIX = '/page-data/'

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

const isPageData =
eventPath.endsWith(DATA_SUFFIX) && eventPath.startsWith(DATA_PREFIX)

const pathName = isPageData
? getPagePathFromPageDataPath(eventPath)
: eventPath
const graphqlEngine = getGraphQLEngine()
// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
const page: IGatsbyPage & { mode?: string } =
graphqlEngine.findPageByPath(pathName)

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

return {
statusCode: 404,
body,
headers: {
Tag: etag(body),
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'DSG',
},
const isPageData =
eventPath.endsWith(DATA_SUFFIX) && eventPath.startsWith(DATA_PREFIX)

const pathName = isPageData
? getPagePathFromPageDataPath(eventPath)
: eventPath
// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
const page: IGatsbyPage & { mode?: string } =
graphqlEngine.findPageByPath(pathName)

if (page?.mode !== 'DSG') {
const body = await readFile(
join(process.cwd(), 'public', '404.html'),
'utf8',
)

return {
statusCode: 404,
body,
headers: {
Tag: etag(body),
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'DSG',
},
}
}
const data = await getData({
pathName,
graphqlEngine,
})

if (isPageData) {
const body = JSON.stringify(await renderPageData({ data }))
return {
statusCode: 200,
body,
headers: {
ETag: etag(body),
'Content-Type': 'application/json',
'X-Mode': 'DSG',
},
}
}
}
const data = await getData({
pathName,
graphqlEngine,
})

if (isPageData) {
const body = JSON.stringify(await renderPageData({ data }))
const body = await renderHTML({ data })

return {
statusCode: 200,
body,
headers: {
ETag: etag(body),
'Content-Type': 'application/json',
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'DSG',
},
}
}
}

const body = await renderHTML({ data })

return {
statusCode: 200,
body,
headers: {
ETag: etag(body),
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'DSG',
},
}
})
export const handler: Handler = builder(getHandler())
132 changes: 67 additions & 65 deletions plugin/src/templates/ssr/__ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,86 +37,88 @@ type PageSSR = {
renderPageData: typeof renderPageDataType
}

prepareFilesystem()

// Requiring this dynamically so esbuild doesn't re-bundle it
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { getData, renderHTML, renderPageData }: PageSSR = require(join(
CACHE_DIR,
'page-ssr',
))

// eslint-disable-next-line func-style, max-statements, complexity
export const handler: Handler = async function handler(event) {
const eventPath = event.path
const isPageData =
eventPath.startsWith('/page-data/') && eventPath.endsWith('/page-data.json')

const pathName = isPageData
? getPagePathFromPageDataPath(eventPath)
: eventPath

function getHandler(): Handler {
prepareFilesystem()
// Requiring this dynamically so esbuild doesn't re-bundle it
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/global-require
const { getData, renderHTML, renderPageData }: PageSSR = require(join(
CACHE_DIR,
'page-ssr',
))
const graphqlEngine = getGraphQLEngine()

// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
const page: IGatsbyPage & { mode?: string } =
graphqlEngine.findPageByPath(pathName)

if (page?.mode !== `SSR`) {
const body = await readFile(
join(process.cwd(), 'public', '404.html'),
'utf8',
)
return {
statusCode: 404,
body,
headers: {
Tag: etag(body),
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'SSR',
},
// eslint-disable-next-line complexity
return async function handler(event) {
const eventPath = event.path
const isPageData =
eventPath.startsWith('/page-data/') &&
eventPath.endsWith('/page-data.json')

const pathName = isPageData
? getPagePathFromPageDataPath(eventPath)
: eventPath

// Gatsby doesn't currently export this type. Hopefully fixed by v4 release
const page: IGatsbyPage & { mode?: string } =
graphqlEngine.findPageByPath(pathName)

if (page?.mode !== `SSR`) {
const body = await readFile(
join(process.cwd(), 'public', '404.html'),
'utf8',
)
return {
statusCode: 404,
body,
headers: {
Tag: etag(body),
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'SSR',
},
}
}
}

const req: SSRReq = {
query: event.queryStringParameters,
method: event.httpMethod,
url: event.path,
headers: event.headers,
}
const req: SSRReq = {
query: event.queryStringParameters,
method: event.httpMethod,
url: event.path,
headers: event.headers,
}

const data = await getData({
pathName,
graphqlEngine,
req,
})
const data = await getData({
pathName,
graphqlEngine,
req,
})

const headers = data.serverDataHeaders || {}

if (isPageData) {
const body = JSON.stringify(await renderPageData({ data }))
return {
statusCode: 200,
body,
headers: {
ETag: etag(body),
'Content-Type': 'application/json',
'X-Mode': 'SSR',
...headers,
},
}
}

const headers = data.serverDataHeaders || {}
const body = await renderHTML({ data })

if (isPageData) {
const body = JSON.stringify(await renderPageData({ data }))
return {
statusCode: 200,
body,
headers: {
ETag: etag(body),
'Content-Type': 'application/json',
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'SSR',
...headers,
},
}
}

const body = await renderHTML({ data })

return {
statusCode: 200,
body,
headers: {
ETag: etag(body),
'Content-Type': 'text/html; charset=utf-8',
'X-Mode': 'SSR',
...headers,
},
}
}
export const handler: Handler = getHandler()
4 changes: 1 addition & 3 deletions renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
automerge: true,
packageRules: [
{
matchPackagePrefixes: [
'@sindresorhus/'
],
matchPackagePrefixes: ['@sindresorhus/'],
major: {
enabled: false,
},
Expand Down

0 comments on commit dea5dae

Please sign in to comment.