-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v2] Build-html static file using the webpack magic comment to provid…
…e the link rel. (#5901) * upgrade webpack version to 4.12.0 for webpackPrefetch propose * use stats.toJson() instead of manually parse chunkGroups * WIP: debug static entry * add childAssets to rel link in static-entry 🎉 * use stats.toJson with less verbose * WIP: unique link rel and give priority to preload * fixed assetsByChunkName in webpack.stats different from current implementation * add using-prefetching-preloading-modules examples * Don't add prefetched bundles to end of html + move preloading json with other scripts
- Loading branch information
1 parent
0eb3f69
commit d30f1c2
Showing
14 changed files
with
247 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"env": { | ||
"browser": true | ||
}, | ||
"globals": { | ||
"graphql": false | ||
} | ||
} |
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,6 @@ | ||
# Using Prefetching/Preloading modules | ||
|
||
https://using-prefetch-preload-module.gatsbyjs.org | ||
|
||
## References | ||
- [Prefetching/Preloading Modules](https://webpack.js.org/guides/code-splitting/#prefetching-preloading-modules). |
16 changes: 16 additions & 0 deletions
16
examples/using-prefetching-preloading-modules/gatsby-config.js
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,16 @@ | ||
module.exports = { | ||
siteMetadata: { | ||
title: `Gatsby with styled components`, | ||
}, | ||
plugins: [ | ||
`gatsby-plugin-styled-components`, | ||
{ | ||
resolve: `gatsby-plugin-typography`, | ||
options: { | ||
pathToConfigModule: `src/utils/typography.js`, | ||
}, | ||
}, | ||
`gatsby-plugin-react-helmet`, | ||
`gatsby-plugin-offline`, | ||
], | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/using-prefetching-preloading-modules/package.json
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,33 @@ | ||
{ | ||
"name": "gatsby-example-using-prefetching-preloading-modules", | ||
"private": true, | ||
"description": "Gatsby example site using Prefetching/Preloading modules", | ||
"version": "2.0.0", | ||
"author": "Nuttapol Laoticharoen <[email protected]>", | ||
"dependencies": { | ||
"babel-plugin-styled-components": "^1.5.1", | ||
"gatsby": "next", | ||
"gatsby-plugin-offline": "next", | ||
"gatsby-plugin-react-helmet": "next", | ||
"gatsby-plugin-styled-components": "next", | ||
"gatsby-plugin-typography": "next", | ||
"lodash": "^4.16.4", | ||
"react": "^16.4.0", | ||
"react-dom": "^16.4.0", | ||
"react-helmet": "^5.2.0", | ||
"react-typography": "^0.16.13", | ||
"slash": "^1.0.0", | ||
"styled-components": "^3.3.2", | ||
"typography": "^0.16.17", | ||
"typography-breakpoint-constants": "^0.15.10" | ||
}, | ||
"keywords": [ | ||
"gatsby" | ||
], | ||
"license": "MIT", | ||
"main": "n/a", | ||
"scripts": { | ||
"develop": "gatsby develop", | ||
"build": "gatsby build" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/using-prefetching-preloading-modules/src/components/DynamicComponent.js
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,19 @@ | ||
import React from 'react' | ||
|
||
class DynamicComponent extends React.Component { | ||
handleClick = () => { | ||
console.log(`Sync-Click!`) | ||
import(/* webpackChunkName: "async-console", webpackPreload: true */ `../utils/async-console`).then(module => { | ||
const asyncConsole = module.default | ||
asyncConsole(`Async-Log!`) | ||
}) | ||
} | ||
|
||
render() { | ||
return <button onClick={this.handleClick}> | ||
Dynamic Log | ||
</button> | ||
} | ||
} | ||
|
||
export default DynamicComponent |
65 changes: 65 additions & 0 deletions
65
examples/using-prefetching-preloading-modules/src/pages/index.js
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,65 @@ | ||
import React from "react" | ||
import Helmet from "react-helmet" | ||
import styled from "styled-components" | ||
import DynamicComponent from "../components/DynamicComponent" | ||
import "./style.css" | ||
|
||
// Create a Title component that'll render an <h1> tag with some styles | ||
const Title = styled.h1` | ||
font-size: 1.5em; | ||
text-align: center; | ||
color: brown; | ||
` | ||
|
||
// Create a Wrapper component that'll render a <section> tag with some styles | ||
const Wrapper = styled.section` | ||
padding: 4em; | ||
background: papayawhip; | ||
` | ||
|
||
|
||
class IndexPage extends React.Component { | ||
handleClick = () => { | ||
console.log(`Sync-Click!`) | ||
import(/* webpackChunkName: "async-alert", webpackPrefetch: true */ `../utils/async-alert`).then(module => { | ||
const asyncAlert = module.default | ||
asyncAlert(`Async-Click!`) | ||
}) | ||
} | ||
|
||
render() { | ||
return ( | ||
<React.Fragment> | ||
<Helmet> | ||
<title>Gatsby Prefetching/Preloading modules</title> | ||
<meta | ||
name="description" | ||
content="Gatsby example site using prefetching/preloading modules" | ||
/> | ||
<meta name="referrer" content="origin" /> | ||
</Helmet> | ||
<div | ||
style={{ | ||
margin: `0 auto`, | ||
marginTop: `3rem`, | ||
padding: `1.5rem`, | ||
maxWidth: 900, | ||
color: `red`, | ||
}} | ||
> | ||
<Wrapper> | ||
<Title>Hello World, this is my first prefetching/preloading component!</Title> | ||
<p> | ||
<button onClick={this.handleClick}> | ||
Dynamic Alert! | ||
</button> | ||
<DynamicComponent /> | ||
</p> | ||
</Wrapper> | ||
</div> | ||
</React.Fragment> | ||
) | ||
} | ||
} | ||
|
||
export default IndexPage |
9 changes: 9 additions & 0 deletions
9
examples/using-prefetching-preloading-modules/src/pages/style.css
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,9 @@ | ||
|
||
.changeable-title { | ||
color: red; | ||
} | ||
|
||
button { | ||
margin-right: 20px; | ||
float: left; | ||
} |
3 changes: 3 additions & 0 deletions
3
examples/using-prefetching-preloading-modules/src/utils/async-alert.js
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,3 @@ | ||
|
||
|
||
module.exports = x => alert(x) |
3 changes: 3 additions & 0 deletions
3
examples/using-prefetching-preloading-modules/src/utils/async-console.js
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,3 @@ | ||
|
||
|
||
module.exports = (...x) => console.log(...x) |
35 changes: 35 additions & 0 deletions
35
examples/using-prefetching-preloading-modules/src/utils/typography.js
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,35 @@ | ||
import Typography from "typography" | ||
import { | ||
MOBILE_MEDIA_QUERY, | ||
TABLET_MEDIA_QUERY, | ||
} from "typography-breakpoint-constants" | ||
|
||
const options = { | ||
baseFontSize: `18px`, | ||
baseLineHeight: 1.45, | ||
blockMarginBottom: 0.75, | ||
scaleRatio: 2.15, | ||
overrideStyles: ({ rhythm, scale }, options) => { | ||
return { | ||
"h1,h2,h3,h4": { | ||
lineHeight: 1.2, | ||
}, | ||
[TABLET_MEDIA_QUERY]: { | ||
// Make baseFontSize on mobile 17px. | ||
html: { | ||
fontSize: `${17 / 16 * 100}%`, | ||
}, | ||
}, | ||
[MOBILE_MEDIA_QUERY]: { | ||
// Make baseFontSize on mobile 16px. | ||
html: { | ||
fontSize: `${16 / 16 * 100}%`, | ||
}, | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
const typography = new Typography(options) | ||
|
||
export default typography |
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
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