1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/plugins/json-pages-plugin.js
Arunoda Susiripala 165924b71b Add Gzip support for JSON pages (#571)
* Server JSON pages directly from the filesystem.

* Make Json pages even if there's an error.

* Implement much better page serving.

* Use JsonPagesPlugin in the production mode as well.

* Add gzip support for JSON pages.

* Use glob-promise instead of recursive-readdir

* Handle renderStatic 404 properly.

* Simply the gzip code.

* Cache already read JSON pages.

* Change JSON pages extension to .json.

* Fix HMR related issue.

* Fix hot-reload for .json solely on server.

* Properly clear cache on hot-reloader.

* Convert .js pages into .json page right inside the plugin.

* Fix gzipping .json pages.

* Remove unwanted json pages cleanup.

* Get rid of deprecated fs.exists for fs.access
2016-12-31 21:46:23 +09:00

25 lines
669 B
JavaScript

export default class JsonPagesPlugin {
apply (compiler) {
compiler.plugin('after-compile', (compilation, callback) => {
const pages = Object
.keys(compilation.assets)
.filter((filename) => /^bundles\/pages.*\.js$/.test(filename))
pages.forEach((pageName) => {
const page = compilation.assets[pageName]
delete compilation.assets[pageName]
const content = page.source()
const newContent = JSON.stringify({ component: content })
compilation.assets[`${pageName}on`] = {
source: () => newContent,
size: () => newContent.length
}
})
callback()
})
}
}