Skip to content

Commit

Permalink
fix: redirect handling in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Mar 23, 2022
1 parent 1c30258 commit f62bacf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-dolphins-wonder.md
@@ -0,0 +1,5 @@
---
'presta': patch
---

Unset headers and body if response is a redirect
10 changes: 6 additions & 4 deletions packages/presta/lib/normalizeResponse.ts
Expand Up @@ -36,6 +36,7 @@ export function normalizeResponse(response: Partial<Response> | string): LambdaR
body: response,
}
: response
const redir = statusCode > 299 && statusCode < 399

let contentType = 'text/html; charset=utf-8'

Expand All @@ -46,15 +47,16 @@ export function normalizeResponse(response: Partial<Response> | string): LambdaR
}

const normalizedIncomingHeaders = normalizeHeaders(headers as Headers)
const normalizedHeaders: LambdaResponse['headers'] = {
'content-type': contentType,
...normalizedIncomingHeaders,
const normalizedHeaders: LambdaResponse['headers'] = {}

if (!redir) {
normalizedHeaders['content-type'] = contentType
}

return {
isBase64Encoded,
statusCode,
headers: normalizedHeaders,
headers: Object.assign({}, normalizedHeaders, normalizedIncomingHeaders),
multiValueHeaders,
body: stringify(body || html || json || xml || ''),
}
Expand Down
4 changes: 2 additions & 2 deletions packages/presta/lib/serve.ts
Expand Up @@ -13,7 +13,7 @@ import { Handler, Event, Response, Context } from './lambda'
import { Config } from './config'
import { Hooks } from './createEmitter'
import { normalizeResponse } from './normalizeResponse'
import { getDynamicFilesFromManifest, Manifest, ManifestDynamicFile } from './manifest'
import { getDynamicFilesFromManifest, Manifest } from './manifest'

export interface HttpError extends Error {
statusCode?: number
Expand Down Expand Up @@ -87,7 +87,7 @@ export function createRequestHandler({ port, config }: { port: number; config: C
const lambda = loadLambdaFromManifest(event.path, manifest)
const response = await processHandler(event, lambda)
const redir = response.statusCode > 299 && response.statusCode < 399
const mime = getMimeType(response)
const mime = redir ? undefined : getMimeType(response)

if (mime === 'html') {
response.body = (response.body || '').split('</body>')[0] + createLiveReloadScript({ port })
Expand Down

0 comments on commit f62bacf

Please sign in to comment.