diff --git a/package.json b/package.json index 9766875e..0f64bd6e 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "fkill": "5.1.0", "flatten": "1.0.2", "get-port": "3.2.0", + "isomorphic-unfetch": "3.0.0", "jest-cli": "23.6.0", "jest-junit": "^5.0.0", "lerna": "^3.4.0", diff --git a/packages/next/build/webpack-config.js b/packages/next/build/webpack-config.js index 2eee58d3..219fa29e 100644 --- a/packages/next/build/webpack-config.js +++ b/packages/next/build/webpack-config.js @@ -213,7 +213,8 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer ], alias: { next: NEXT_PROJECT_ROOT - } + }, + mainFields: [!isServer && 'browser', !isServer && 'module', 'main'].filter(Boolean) } const webpackMode = dev ? 'development' : 'production' diff --git a/test/integration/lambdas/pages/fetch.js b/test/integration/lambdas/pages/fetch.js new file mode 100644 index 00000000..f15c3a9c --- /dev/null +++ b/test/integration/lambdas/pages/fetch.js @@ -0,0 +1,29 @@ +import fetch from 'isomorphic-unfetch' +import React from 'react' + +export default class extends React.Component { + static async getInitialProps () { + try { + const res = await fetch('') + const text = await res.text() + console.log(text) + return {text} + } catch (err) { + if (err.message.includes('is not a function')) { + return {failed: true, error: err.toString()} + } + + return {error: err.toString()} + } + } + render () { + const {failed, error, text} = this.props + return
+ {failed ? 'failed' : ''} + {error} +
+ {text} +
+
+ } +} diff --git a/test/integration/lambdas/pages/index.js b/test/integration/lambdas/pages/index.js index 77346cb4..d200f452 100644 --- a/test/integration/lambdas/pages/index.js +++ b/test/integration/lambdas/pages/index.js @@ -1 +1,9 @@ -export default () => 'Hello World' +import Link from 'next/link' +export default () => { + return
+ Hello World + + fetch page + +
+} diff --git a/test/integration/lambdas/test/index.test.js b/test/integration/lambdas/test/index.test.js index 9b64ec63..3fd38d6d 100644 --- a/test/integration/lambdas/test/index.test.js +++ b/test/integration/lambdas/test/index.test.js @@ -8,6 +8,8 @@ import { stopApp, renderViaHTTP } from 'next-test-utils' +import webdriver from 'next-webdriver' +import fetch from 'node-fetch' const appDir = join(__dirname, '../') let appPort @@ -35,4 +37,26 @@ describe('Lambdas', () => { const html = await renderViaHTTP(appPort, '/') expect(html).toMatch(/Hello World/) }) + + it('should render correctly when importing isomorphic-unfetch', async () => { + const url = `http://localhost:${appPort}/fetch` + const res = await fetch(url) + expect(res.status).toBe(200) + const text = await res.text() + expect(text.includes('failed')).toBe(false) + }) + + it('should render correctly when importing isomorphic-unfetch on the client side', async () => { + const browser = await webdriver(appPort, '/') + try { + const text = await browser + .elementByCss('a').click() + .waitForElementByCss('.fetch-page') + .elementByCss('#text').text() + + expect(text).toMatch(/fetch page/) + } finally { + browser.close() + } + }) })