Skip to content

Commit 7838b18

Browse files
authored
feat(gatsby): test redirects (#31011)
* feat(gatsby): test redirects * Fix test * refine message * another error tweak * actually fix test
1 parent 8250c9c commit 7838b18

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { GatsbyAPIFunctionResponse, GatsbyAPIFunctionRequest } from "gatsby"
2+
3+
export default function topLevel(
4+
req: GatsbyAPIFunctionRequest,
5+
res: GatsbyAPIFunctionResponse
6+
) {
7+
res.redirect(`/`)
8+
}

integration-tests/functions/test-helpers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ export function runTests(env, host) {
212212
})
213213
})
214214

215+
describe(`functions can redirect`, () => {
216+
test(`normal`, async () => {
217+
const result = await fetch(`${host}/api/redirect-me`)
218+
219+
expect(result.url).toEqual(host + `/`)
220+
})
221+
})
222+
215223
// TODO figure out why this gets into endless loops
216224
// describe.only(`hot reloading`, () => {
217225
// const fixturesDir = path.join(__dirname, `fixtures`)

packages/gatsby/src/internal-plugins/functions/gatsby-node.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,11 @@ export async function onCreateDevServer({
364364
await Promise.resolve(fnToExecute(req, res))
365365
} catch (e) {
366366
reporter.error(e)
367-
res.sendStatus(500)
367+
res
368+
.status(500)
369+
.send(
370+
`Error when executing function "${functionObj.originalFilePath}": "${e.message}"`
371+
)
368372
}
369373

370374
const end = Date.now()

0 commit comments

Comments
 (0)