Skip to content

Commit 1a30536

Browse files
committed
chore: add jest ts support
1 parent a5635c6 commit 1a30536

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

babel.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// This is just for jest
22
module.exports = {
3-
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
3+
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
44
}

demo/pages/getStaticProps/withRevalidate/[id].js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Show = ({ show, time }) => (
88

99
<h1>Show #{show.id}</h1>
1010
<p>{show.name}</p>
11-
<p>Rendered at {time}</p>
11+
<p>Rendered at {time} (slowly)</p>
1212
<hr />
1313

1414
<Link href="/">
@@ -33,7 +33,7 @@ export async function getStaticProps({ params }) {
3333
const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
3434
const data = await res.json()
3535
const time = new Date().toLocaleTimeString()
36-
await new Promise((resolve) => setTimeout(resolve, 1000))
36+
await new Promise((resolve) => setTimeout(resolve, 3000))
3737
return {
3838
props: {
3939
show: data,

package-lock.json

+57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"devDependencies": {
7373
"@babel/core": "^7.15.8",
7474
"@babel/preset-env": "^7.15.8",
75+
"@babel/preset-typescript": "^7.16.0",
7576
"@netlify/build": "^18.25.2",
7677
"@netlify/eslint-config-node": "^3.3.7",
7778
"@testing-library/cypress": "^8.0.1",

src/templates/getHandler.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines-per-function */
12
const { promises, existsSync } = require('fs')
23
const { Server } = require('http')
34
const { tmpdir } = require('os')
@@ -121,7 +122,10 @@ const makeHandler =
121122
const protocol = event.headers['x-forwarded-proto'] || 'http'
122123
base = `${protocol}://${host}`
123124
}
125+
console.time('Rendering time')
124126
const { headers, ...result } = await bridge.launcher(event, context)
127+
console.timeEnd('Rendering time')
128+
125129
/** @type import("@netlify/functions").HandlerResponse */
126130

127131
// Convert all headers to multiValueHeaders
@@ -144,12 +148,17 @@ const makeHandler =
144148
// Long-expiry TTL is basically no TTL
145149
if (ttl > 0 && ttl < ONE_YEAR_IN_SECONDS) {
146150
result.ttl = ttl
151+
multiValueHeaders['x-nf-ttl'] = [String(ttl)]
152+
} else {
153+
multiValueHeaders['x-nf-ttl'] = [`Invalid: ${ttl}`]
147154
}
155+
multiValueHeaders['x-rendered-at'] = [new Date().toISOString()]
148156
}
149157
multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate']
150158
}
151159
multiValueHeaders['x-render-mode'] = [mode]
152-
160+
const serialised = JSON.stringify(multiValueHeaders)
161+
multiValueHeaders['x-nf-headers'] = [serialised]
153162
return {
154163
...result,
155164
multiValueHeaders,
@@ -182,3 +191,4 @@ exports.handler = ${
182191
`
183192

184193
module.exports = getHandler
194+
/* eslint-enable max-lines-per-function */

0 commit comments

Comments
 (0)