-
Notifications
You must be signed in to change notification settings - Fork 27.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server side rendering for dynamic imports.
- Loading branch information
Showing
12 changed files
with
198 additions
and
46 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
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 @@ | ||
module.exports = require('./dist/lib/dynamic') |
This file was deleted.
Oops, something went wrong.
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,44 @@ | ||
import React from 'react' | ||
|
||
let currentChunks = [] | ||
|
||
export default function dynamicComponent (promise, Loading = () => (<p>Loading...</p>)) { | ||
return class Comp extends React.Component { | ||
constructor (...args) { | ||
super(...args) | ||
this.state = { AsyncComponent: null } | ||
this.isServer = typeof window === 'undefined' | ||
this.loadComponent() | ||
} | ||
|
||
loadComponent () { | ||
promise.then((AsyncComponent) => { | ||
if (this.mounted) { | ||
this.setState({ AsyncComponent }) | ||
} else { | ||
if (this.isServer) { | ||
currentChunks.push(AsyncComponent.__webpackChunkName) | ||
} | ||
this.state.AsyncComponent = AsyncComponent | ||
} | ||
}) | ||
} | ||
|
||
componentDidMount () { | ||
this.mounted = true | ||
} | ||
|
||
render () { | ||
const { AsyncComponent } = this.state | ||
if (!AsyncComponent) return (<Loading {...this.props} />) | ||
|
||
return <AsyncComponent {...this.props} /> | ||
} | ||
} | ||
} | ||
|
||
export function flushChunks () { | ||
const chunks = currentChunks | ||
currentChunks = [] | ||
return chunks | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export default class PagesPlugin { | ||
apply (compiler) { | ||
const isImportChunk = /^chunks[/\\].*\.js$/ | ||
const matchChunkName = /^chunks[/\\](.*)$/ | ||
|
||
compiler.plugin('after-compile', (compilation, callback) => { | ||
const chunks = Object | ||
.keys(compilation.namedChunks) | ||
.map(key => compilation.namedChunks[key]) | ||
.filter(chunk => isImportChunk.test(chunk.name)) | ||
|
||
chunks.forEach((chunk) => { | ||
const asset = compilation.assets[chunk.name] | ||
if (!asset) return | ||
|
||
const chunkName = matchChunkName.exec(chunk.name)[1] | ||
|
||
const content = asset.source() | ||
const newContent = ` | ||
window.__NEXT_REGISTER_CHUNK('${chunkName}', function() { | ||
${content} | ||
}) | ||
` | ||
// Replace the exisiting chunk with the new content | ||
compilation.assets[chunk.name] = { | ||
source: () => newContent, | ||
size: () => newContent.length | ||
} | ||
}) | ||
callback() | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.