-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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 support for customizing compress encodings #657
Closed
Closed
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c427d5a
Add support for compiling with custom compression types.
arunoda 9b60143
Add support for serving custom compress types as well.
arunoda 0274b23
Fix a typo.
arunoda 4adbf0c
Add an example app and add docs to README.
arunoda 0794418
Resolve conflicts.
arunoda 3f93273
Change the compress mappings to return just a stream.
arunoda 83c5798
Change gzip wording into compress over the code.
arunoda fcabbc8
Fix a typo.
arunoda 318a9d5
Fix some typo.
arunoda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,29 @@ | ||
# Example app with custom compress encodings | ||
|
||
## How to use | ||
|
||
Download the example (or clone the repo)[https://github.com/zeit/next.js.git]: | ||
|
||
```bash | ||
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-custom-compress-encodings | ||
cd with-custom-compress-encodings | ||
``` | ||
|
||
Install it and run: | ||
|
||
```bash | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) | ||
|
||
```bash | ||
now | ||
``` | ||
|
||
## The idea behind the example | ||
|
||
By default, Next.js compile your assets with `gzip` compression. But if you want to add support more encodings like `br` or `deflate` you can do it very easily. | ||
|
||
This example shows how to add support for `br` and `gzip` compress encodings. For that it uses a config in `next.config.js`. |
19 changes: 19 additions & 0 deletions
19
examples/with-custom-compress-encodings/components/Counter.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' | ||
|
||
let count = 0 | ||
|
||
export default class Counter extends React.Component { | ||
add () { | ||
count += 1 | ||
this.forceUpdate() | ||
} | ||
|
||
render () { | ||
return ( | ||
<div> | ||
<p>Count is: {count}</p> | ||
<button onClick={() => this.add()}>Add</button> | ||
</div> | ||
) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/with-custom-compress-encodings/components/Header.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 Link from 'next/link' | ||
|
||
export default () => ( | ||
<div> | ||
<Link href='/'> | ||
<a style={styles.a} >Home</a> | ||
</Link> | ||
|
||
<Link href='/about'> | ||
<a style={styles.a} >About</a> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be cool to use styled-jsx There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanna talk about this feature in this example. |
||
</Link> | ||
</div> | ||
) | ||
|
||
const styles = { | ||
a: { | ||
marginRight: 10 | ||
} | ||
} |
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 @@ | ||
var fs = require('fs') | ||
var zlib = require('zlib') | ||
var iltorb = require('iltorb') | ||
|
||
module.exports = { | ||
// Return a stream of compressed file for each of the encodings you want | ||
// | ||
// The first listed encoding has the higher priority over others. | ||
// In this case, it'll try to serve the `br` version if the browser supports it. | ||
// Otherwise, it'll server gzipped version. | ||
compress: { | ||
br: function (filePath) { | ||
return fs.createReadStream(filePath).pipe(iltorb.compressStream()) | ||
}, | ||
gzip: function (filePath) { | ||
return fs.createReadStream(filePath).pipe(zlib.createGzip()) | ||
} | ||
} | ||
} |
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,17 @@ | ||
{ | ||
"name": "shared-modules", | ||
"version": "1.0.0", | ||
"description": "This example features:", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"iltorb": "^1.0.13", | ||
"next": "^2.0.0-beta" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
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,10 @@ | ||
import Header from '../components/Header' | ||
import Counter from '../components/Counter' | ||
|
||
export default () => ( | ||
<div> | ||
<Header /> | ||
<p>This is the about page.</p> | ||
<Counter /> | ||
</div> | ||
) |
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,10 @@ | ||
import Header from '../components/Header' | ||
import Counter from '../components/Counter' | ||
|
||
export default () => ( | ||
<div> | ||
<Header /> | ||
<p>HOME PAGE is here!</p> | ||
<Counter /> | ||
</div> | ||
) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should accept
Promise
instead of ReadableStream since errors might occur in the middle of compression process.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we capture it inside our code. We convert this into a promise.
Why I like this approach is, this is how most of the compression API works. So, it's pretty simple for the user. We handle the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it doesn't capture all errors since errors are emitted on each stream. http://stackoverflow.com/questions/21771220/error-handling-with-node-js-streams
Additionally, we should consider the case you do something async inside the callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nkzawa so what what do get from the promise?
We can do something like this too:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 That seems the simplest way.
Another idea is to accept only a transform stream.
I'm not so sure if this would cover all cases though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nkzawa I like that approach. And we can also also accept a promise from that function. (Which returns a stream)