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

Add Gzip support for JSON pages #571

Merged
merged 18 commits into from
Dec 31, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Server JSON pages directly from the filesystem.
  • Loading branch information
arunoda committed Dec 30, 2016
commit bc2774ae3bfcf51c9e297d60e34e2990d67c5df3
19 changes: 19 additions & 0 deletions server/build/plugins/json-pages-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default class JsonPagesPlugin {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nkzawa This is my first webpack plugin.
Could you check whether I have done something ugly here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me 👍

apply (compiler) {
compiler.plugin('emit', (compilation, callback) => {
const pages = Object
.keys(compilation.assets)
.filter((filename) => /^bundles\/pages/.test(filename))

pages.forEach((pageName) => {
const page = compilation.assets[pageName]
const content = page.source()
const newContent = JSON.stringify({ component: content })
page.source = () => newContent
page.size = () => newContent.length
})

callback()
})
}
}
2 changes: 2 additions & 0 deletions server/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import WatchPagesPlugin from './plugins/watch-pages-plugin'
import WatchRemoveEventPlugin from './plugins/watch-remove-event-plugin'
import DynamicEntryPlugin from './plugins/dynamic-entry-plugin'
import DetachPlugin from './plugins/detach-plugin'
import JsonPagesPlugin from './plugins/json-pages-plugin'
import getConfig from '../config'

const documentPage = join('pages', '_document.js')
Expand Down Expand Up @@ -77,6 +78,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new DetachPlugin(),
new JsonPagesPlugin(),
new DynamicEntryPlugin(),
new UnlinkFilePlugin(),
new WatchRemoveEventPlugin(),
Expand Down
5 changes: 3 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ export default class Server {
})

this.router.get('/_next/pages/:path*', async (req, res, params) => {
const paths = params.path || []
const paths = params.path || ['index']
const pathname = `/${paths.join('/')}`
await this.renderJSON(res, pathname)
const p = join(this.dir, '.next', 'bundles', 'pages', `${pathname}.js`)
await this.serveStatic(req, res, p)
})

this.router.get('/_next/:path+', async (req, res, params) => {
Expand Down
7 changes: 5 additions & 2 deletions server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ async function doRender (req, res, pathname, query, {

const [
props,
component,
errorComponent
componentJson,
errorComponentJson
] = await Promise.all([
Component.getInitialProps ? Component.getInitialProps(ctx) : {},
read(join(dir, '.next', 'bundles', 'pages', page)),
read(join(dir, '.next', 'bundles', 'pages', dev ? '_error-debug' : '_error'))
])

const component = JSON.parse(componentJson).component
const errorComponent = JSON.parse(errorComponentJson).component
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should cache the parsed result.


// the response might be finshed on the getinitialprops call
if (res.finished) return

Expand Down