-
Notifications
You must be signed in to change notification settings - Fork 29.9k
implement next/static #2241
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
Closed
Closed
implement next/static #2241
Changes from all commits
Commits
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 hidden or 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,30 @@ | ||
| [](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/using-static-hashing) | ||
| # Example app utilizing next/static for immutable static files | ||
|
|
||
| ## How to use | ||
|
|
||
| Download the example [or clone the repo](https://github.com/zeit/next.js): | ||
|
|
||
| ```bash | ||
| curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/using-router | ||
| cd using-router | ||
| ``` | ||
|
|
||
| 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 | ||
|
|
||
| This example features: | ||
|
|
||
| * Optimized static file serving using `next/static`. |
This file contains hidden or 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,7 @@ | ||
| module.exports = { | ||
| exportPathMap: function () { | ||
| return { | ||
| '/': { page: '/' } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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": "hello-world", | ||
| "version": "1.0.0", | ||
| "scripts": { | ||
| "dev": "next", | ||
| "build": "next build", | ||
| "export": "next export", | ||
| "start": "next start" | ||
| }, | ||
| "dependencies": { | ||
| "next": "*", | ||
| "react": "^15.4.2", | ||
| "react-dom": "^15.4.2" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC" | ||
| } |
This file contains hidden or 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,39 @@ | ||
| import { Component } from 'react' | ||
| import _static from 'next/static' | ||
|
|
||
| export default class Index extends Component { | ||
| state = { | ||
| white: true | ||
| } | ||
|
|
||
| render () { | ||
| const { white } = this.state | ||
| const logoSrc = white | ||
| ? _static('white-logo.svg') | ||
| : _static('black-logo.svg') | ||
|
|
||
| return ( | ||
| <div | ||
| onClick={() => this.setState({ white: !this.state.white })} | ||
| style={{ background: white ? 'white' : 'black' }}> | ||
| <img src={logoSrc} /> | ||
| <style jsx> | ||
| {` | ||
| :global(body) { | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
| div { | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| height: 100vh; | ||
| width: 100vw; | ||
| transition: background 1s; | ||
| } | ||
| `} | ||
| </style> | ||
| </div> | ||
| ) | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 @@ | ||
| export default function _static () { | ||
| throw new Error('Please check you are using the next/babel preset and you are calling next/static with a string literal not a computed value') | ||
| } |
This file contains hidden or 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,86 @@ | ||
| const fs = require('fs') | ||
| const path = require('path') | ||
| const md5 = require('md5-file') | ||
|
|
||
| const dev = process.env.NODE_ENV !== 'production' | ||
| const moduleName = 'next/static' | ||
| const staticStats = {} | ||
| const staticStatsPath = process.env.__NEXT_STATIC_STATS_PATH__ | ||
| const staticDir = process.env.__NEXT_STATIC_DIR__ | ||
|
|
||
| function getHashedName (fileName) { | ||
| let hashedName = staticStats[fileName] | ||
| if (hashedName) return hashedName | ||
|
|
||
| const hash = md5.sync(path.join(staticDir, fileName)) | ||
| const ext = path.extname(fileName) | ||
| const name = path.basename(fileName, ext) | ||
| hashedName = staticStats[fileName] = `${name}_${hash}${ext}` | ||
|
|
||
| // update assets.json | ||
| let stats = JSON.parse(fs.readFileSync(staticStatsPath, 'utf8')) | ||
| stats = Object.assign({}, stats, staticStats) | ||
| fs.writeFileSync(staticStatsPath, JSON.stringify(stats, null, 2)) | ||
|
|
||
| return hashedName | ||
| } | ||
|
|
||
| module.exports = function ({ types: t }) { | ||
| const identifiers = new Set() | ||
| return { | ||
| visitor: { | ||
| ImportDeclaration (path) { | ||
| const defaultSpecifierPath = path.get('specifiers')[0] | ||
| if ( | ||
| path.node.source.value !== moduleName || | ||
| !t.isImportDefaultSpecifier(defaultSpecifierPath) | ||
| ) { | ||
| return | ||
| } | ||
| const { node: { local: { name } } } = defaultSpecifierPath | ||
| const { referencePaths } = path.scope.getBinding(name) | ||
| referencePaths.forEach(reference => { | ||
| identifiers.add(reference) | ||
| }) | ||
| }, | ||
| VariableDeclarator (path) { | ||
| const { node } = path | ||
| if (!isRequireCall(node.init) || !t.isIdentifier(node.id)) { | ||
| return | ||
| } | ||
| const { id: { name } } = node | ||
| const binding = path.scope.getBinding(name) | ||
| if (binding) { | ||
| const { referencePaths } = binding | ||
| referencePaths.forEach(reference => { | ||
| identifiers.add(reference) | ||
| }) | ||
| } | ||
| }, | ||
| Program: { | ||
| exit () { | ||
| Array.from(identifiers).forEach(identifier => { | ||
| if (!t.isCallExpression(identifier.parent)) return | ||
| const [firstArg] = identifier.parent.arguments | ||
| if (!t.isStringLiteral(firstArg)) return | ||
|
|
||
| const replacement = !dev | ||
| ? `/_next/static/${getHashedName(firstArg.value)}` | ||
| : `/static/${firstArg.value}` | ||
| identifier.parentPath.replaceWith(t.stringLiteral(replacement)) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function isRequireCall (callExpression) { | ||
| return ( | ||
| callExpression && | ||
| callExpression.type === 'CallExpression' && | ||
| callExpression.callee.name === 'require' && | ||
| callExpression.arguments.length === 1 && | ||
| callExpression.arguments[0].value === moduleName | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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/static') |
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.
need better way to tell babel plugin where the static dir is located
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.
@cloudkite I wonder if it would make sense for the static location to somehow be configured / set as part of config: https://github.com/zeit/next.js/blob/master/server/config.js
I know it's not configurable at the moment, but
getConfighas access todirand the result is cached / stored.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.
@kochis that could work 👍 @arunoda what do you think of this approach?
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.
@kochis not sure when I'll get a chance to pick this up, feel free to jump in :)