-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added NextScriptDeferred component that allows you to import scripts with defer instead of async in _document.js. * Add mode * add missing files
- Loading branch information
1 parent
4e29c0e
commit d3624bc
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import { NextScript as OriginalNextScript } from 'next/document' | ||
import PropTypes from 'prop-types' | ||
|
||
/** | ||
* A replacement for NextScript from `next/document` that gives you greater control over how script elements are rendered. | ||
* This should be used in the body of `pages/_document.js` in place of `NextScript`. | ||
*/ | ||
export default class NextScript extends OriginalNextScript { | ||
static propTypes = { | ||
/** | ||
* Set to `defer` to use `defer` instead of `async` when rendering script elements. | ||
*/ | ||
mode: PropTypes.oneOf(['async', 'defer']), | ||
} | ||
|
||
static defaultProps = { | ||
mode: 'async', | ||
} | ||
|
||
getScripts() { | ||
return super.getScripts().map(script => { | ||
return React.cloneElement(script, { | ||
key: script.props.src, | ||
defer: this.props.mode === 'defer' ? true : undefined, | ||
async: this.props.mode === 'async' ? true : undefined, | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { Component } from 'react' | ||
import { mount } from 'enzyme' | ||
|
||
describe('NextScriptDeferred', () => { | ||
let NextScript | ||
|
||
beforeEach(() => { | ||
jest.isolateModules(() => { | ||
jest.doMock('next/document', () => ({ | ||
NextScript: class OriginalNextScript extends Component { | ||
getScripts() { | ||
return [<script src="foo.js" async />] | ||
} | ||
|
||
render() { | ||
return <>{this.getScripts()}</> | ||
} | ||
}, | ||
})) | ||
|
||
NextScript = require('react-storefront/NextScript').default | ||
}) | ||
}) | ||
|
||
it('should remove async and add defer to all script tags returned from super.getScripts', () => { | ||
const wrapper = mount(<NextScript mode="defer" />) | ||
const scriptProps = wrapper.find('script').props() | ||
expect(scriptProps.async).toBe(undefined) | ||
expect(scriptProps.defer).toBe(true) | ||
}) | ||
|
||
it('should use async by default', () => { | ||
const wrapper = mount(<NextScript />) | ||
const scriptProps = wrapper.find('script').props() | ||
expect(scriptProps.async).toBe(true) | ||
expect(scriptProps.defer).toBe(undefined) | ||
}) | ||
}) |