Skip to content

Commit

Permalink
Fix global fetch polyfill causing build errors (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasecdb authored Nov 8, 2020
1 parent 71c89a7 commit 2979ac1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
14 changes: 4 additions & 10 deletions packages/react-app-server/bin/rs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv
commands[command]()

// @ts-ignore
const { Request, Response, Headers } = require('minipass-fetch')
const Body = require('minipass-fetch/lib/body')

Object.assign(
global,
{ Request, Response, Headers, Body },
{
fetch: require('make-fetch-happen'),
}
)
global.Body = require('minipass-fetch/lib/body')
global.Request = require('minipass-fetch').Request
global.Response = require('minipass-fetch').Response
global.Headers = require('minipass-fetch').Headers
9 changes: 9 additions & 0 deletions packages/react-app-server/server/defaultServer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs'
import { IncomingMessage, ServerResponse } from 'http'
import * as path from 'path'
import { Writable } from 'stream'
import { parse as parseUrl } from 'url'

import { RootContext } from '@app-server/components'
Expand All @@ -24,6 +25,12 @@ import {
requestContainsPrecondition,
} from './utils'

declare global {
const Body: {
writeToStream(stream: Writable, body: Body): void
}
}

const readJSON = (filePath: string) => {
const file = fs.readFileSync(filePath)
return JSON.parse(file.toString())
Expand Down Expand Up @@ -244,6 +251,8 @@ export class DefaultServer {
]) as Array<[string, string]>,
})

global.fetch = require('make-fetch-happen')

const handleRequest = await this.getAppRequestHandler()

let response = handleRequest(request, 200, request.headers, serverContext)
Expand Down

0 comments on commit 2979ac1

Please sign in to comment.