Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polyfill window.fetch by Default #9168

Merged
merged 41 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f7b7625
Polyfilling fetch and object-assign
janicklas-ralph Oct 17, 2019
b284603
Polyfilling corejs object-assign
janicklas-ralph Oct 18, 2019
b1b2868
Adding object-assign in polyfills.js. IE11 does not support Object.as…
janicklas-ralph Oct 18, 2019
7ca78a2
Fixing failing test
janicklas-ralph Oct 18, 2019
f78f772
Updating object.assign polyfill to fix aliasing
janicklas-ralph Oct 18, 2019
db9f5d4
Merge branch 'canary' of github.com:zeit/next.js into polyfill-fetch
janicklas-ralph Oct 22, 2019
f95ecab
Merge branch 'canary' of github.com:zeit/next.js into polyfill-fetch
janicklas-ralph Oct 25, 2019
3662480
Merge branch 'canary' into polyfill-fetch
timneutkens Oct 25, 2019
6c53959
Updating test case value to match new build stats
janicklas-ralph Oct 28, 2019
1ab61d6
Merge branch 'polyfill-fetch' of github.com:janicklas-ralph/next.js i…
janicklas-ralph Oct 28, 2019
7ca58b6
Increasing the size of default build to 225kb
janicklas-ralph Oct 28, 2019
8e0815d
Resolving upstream conflicts
janicklas-ralph Oct 29, 2019
d9a6414
Fixing defer-script test case to not include polyfill.js
janicklas-ralph Oct 29, 2019
c1e6544
Merge branch 'canary' into polyfill-fetch
Timer Oct 30, 2019
c04d2dd
Revert README.md
Timer Oct 30, 2019
df1a5f4
Merge branch 'canary' of github.com:zeit/next.js into polyfill-fetch
janicklas-ralph Oct 30, 2019
61cbb31
Re-design the polyfill approach based on PR feedback
janicklas-ralph Nov 1, 2019
562126a
Merge branch 'polyfill-fetch' of github.com:janicklas-ralph/next.js i…
janicklas-ralph Nov 1, 2019
26a7586
Rename polyfill chunk
janicklas-ralph Nov 1, 2019
8b73f09
Adding comment and fixing test case
janicklas-ralph Nov 1, 2019
d25f8d4
Rename polyfills chunk
Timer Nov 1, 2019
425ba25
Extract aliases into helper
Timer Nov 1, 2019
b672aca
Remove extra new line
Timer Nov 1, 2019
fd48959
Fix TypeScript typings
Timer Nov 1, 2019
9ca22a7
Adding _internal_fetch alias
janicklas-ralph Nov 1, 2019
db9ac6e
Adjust build manifest plugin
Timer Nov 1, 2019
396cfa6
Build manifest plugin changes - adding a separate entry for polyfills
janicklas-ralph Nov 1, 2019
d847e41
Rename polyfills entry in build-manifest.json
janicklas-ralph Nov 1, 2019
a65639f
Remove old comment
janicklas-ralph Nov 1, 2019
ddaa945
Fix TS
Timer Nov 1, 2019
158ea95
Set key
Timer Nov 1, 2019
9948d4a
Polyfills already added
Timer Nov 1, 2019
5875e56
Filtring polyfill.module.js
janicklas-ralph Nov 1, 2019
67abbb4
Merge branch 'polyfill-fetch' of github.com:janicklas-ralph/next.js i…
janicklas-ralph Nov 1, 2019
1465e49
Fix test
Timer Nov 1, 2019
1d62898
Add __internal_fetch to alias rule
Timer Nov 1, 2019
cc105fc
Adjust name
Timer Nov 1, 2019
f496dfe
bump size
Timer Nov 2, 2019
6476ac4
ignore polyfills
Timer Nov 2, 2019
8da6ec3
sigh
Timer Nov 2, 2019
d18f903
Merge branch 'canary' into polyfill-fetch
Timer Nov 2, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/build/polyfills/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default window.fetch
Timer marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/next/build/polyfills/object-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = Object.assign
25 changes: 25 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { fileExists } from '../lib/file-exists'
import { resolveRequest } from '../lib/resolve-request'
import {
CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME_WEBPACK,
REACT_LOADABLE_MANIFEST,
SERVER_DIRECTORY,
Expand Down Expand Up @@ -115,6 +116,11 @@ export default async function getBaseWebpackConfig(
dev ? `next-dev.js` : 'next.js'
)
),
[path.join(
CLIENT_STATIC_FILES_PATH,
buildId,
Timer marked this conversation as resolved.
Show resolved Hide resolved
'polyfill.js'
)]: path.join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'polyfill.js'),
}
: undefined

Expand Down Expand Up @@ -163,6 +169,25 @@ export default async function getBaseWebpackConfig(
next: NEXT_PROJECT_ROOT,
[PAGES_DIR_ALIAS]: pagesDir,
[DOT_NEXT_ALIAS]: distDir,
...(!isServer
? {
unfetch: path.join(__dirname, 'polyfills', 'fetch.js'),
'isomorphic-unfetch': path.join(__dirname, 'polyfills', 'fetch.js'),
'whatwg-fetch': path.join(__dirname, 'polyfills', 'fetch.js'),
'object-assign': path.join(
__dirname,
'polyfills',
'object-assign.js'
),
'object.assign$': path.join(
__dirname,
'polyfills',
'object-assign.js'
),
'@babel/runtime-corejs2/core-js/object/assign':
'next/dist/build/polyfills/object-assign.js',
}
: {}),
},
mainFields: isServer ? ['main', 'module'] : ['browser', 'module', 'main'],
plugins: [PnpWebpackPlugin],
Expand Down
2 changes: 2 additions & 0 deletions packages/next/client/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'whatwg-fetch'
Object.assign = require('object.assign/polyfill')()
Timer marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 3 additions & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"mini-css-extract-plugin": "0.8.0",
"mkdirp": "0.5.1",
"node-fetch": "2.6.0",
"object.assign": "4.1.0",
Timer marked this conversation as resolved.
Show resolved Hide resolved
"ora": "3.4.0",
"path-to-regexp": "2.1.0",
"pnp-webpack-plugin": "1.5.0",
Expand All @@ -127,7 +128,8 @@
"webpack": "4.39.0",
"webpack-dev-middleware": "3.7.0",
"webpack-hot-middleware": "2.25.0",
"webpack-sources": "1.4.3"
"webpack-sources": "1.4.3",
"whatwg-fetch": "3.0.0"
},
"peerDependencies": {
"react": "^16.6.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,16 @@ export class NextScript extends Component<OriginProps> {
}}
/>
) : null}
<script
nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || process.crossOrigin}
noModule={true}
src={
assetPrefix +
`/_next/static/${buildId}/polyfill.js` +
_devOnlyInvalidateCacheQueryString
}
/>
{page !== '/_error' && pageScript}
{appScript}
{staticMarkup ? null : this.getDynamicChunks()}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/build-stats-output/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const appDir = join(__dirname, '../react-site')
describe('Build Stats Output', () => {
it('Shows correct package count in output', async () => {
const { stdout } = await nextBuild(appDir, undefined, { stdout: true })
expect(stdout).toMatch(/\/something .*?4/)
expect(stdout).toMatch(/\/something .*?2/)
})
})
5 changes: 3 additions & 2 deletions test/integration/defer-scripts/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ describe('Defer Scripts', () => {
let missing = false

for (const script of $('script').toArray()) {
const { defer, type } = script.attribs
const { defer, type, src } = script.attribs
// application/json doesn't need defer
if (type === 'application/json') {
if (type === 'application/json' || /polyfill\.js$/.test(src)) {
continue
}

if (defer !== '') {
console.log(src)
missing = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/size-limit/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('Production response size', () => {
)

// These numbers are without gzip compression!
expect(responseSizeKilobytes).toBeLessThanOrEqual(221) // Kilobytes
expect(responseSizeKilobytes).toBeLessThanOrEqual(225) // Kilobytes
})

it('should not increase the overall response size of modern build', async () => {
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10449,7 +10449,7 @@ object-visit@^1.0.0:
dependencies:
isobject "^3.0.0"

object.assign@^4.1.0:
object.assign@4.1.0, object.assign@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/object.assign/-/object.assign-4.1.0.tgz#968bf1100d7956bb3ca086f006f846b3bc4008da"
integrity sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==
Expand Down Expand Up @@ -15020,7 +15020,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f"
integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng==

whatwg-fetch@>=0.10.0:
whatwg-fetch@3.0.0, whatwg-fetch@>=0.10.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"
integrity sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==
Expand Down