Skip to content

Commit 8f874f0

Browse files
authored
[WIP] Use buildId in chunk urls as well (vercel#2873)
Use buildId in chunk urls as well
1 parent fd198d2 commit 8f874f0

File tree

9 files changed

+41
-27
lines changed

9 files changed

+41
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
exportPathMap: function () {
3+
return {
4+
'/': { page: '/' },
5+
'/about': { page: '/about' }
6+
}
7+
}
8+
}

examples/with-dynamic-import/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"scripts": {
77
"dev": "next",
88
"build": "next build",
9+
"export": "next export",
910
"start": "next start"
1011
},
1112
"dependencies": {
12-
"async-reactor": "^1.1.1",
13-
"next": "^3.0.1-beta.5",
13+
"next": "^3.0.6",
1414
"react": "^15.4.2",
1515
"react-dom": "^15.4.2"
1616
},

examples/with-dynamic-import/pages/index.js

-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Router from 'next/router'
33
import Header from '../components/Header'
44
import Counter from '../components/Counter'
55
import dynamic from 'next/dynamic'
6-
import { asyncReactor } from 'async-reactor'
76

87
const DynamicComponent = dynamic(import('../components/hello1'))
98
const DynamicComponentWithCustomLoading = dynamic(
@@ -16,11 +15,6 @@ const DynamicComponentWithNoSSR = dynamic(
1615
import('../components/hello3'),
1716
{ ssr: false }
1817
)
19-
const DynamicComponentWithAsyncReactor = asyncReactor(async () => {
20-
const Hello4 = await import('../components/hello4')
21-
return (<Hello4 />)
22-
})
23-
2418
const DynamicComponent5 = dynamic(import('../components/hello5'))
2519

2620
const DynamicBundle = dynamic({
@@ -67,7 +61,6 @@ export default class Index extends React.Component {
6761
<DynamicComponent />
6862
<DynamicComponentWithCustomLoading />
6963
<DynamicComponentWithNoSSR />
70-
<DynamicComponentWithAsyncReactor />
7164
<DynamicBundle showMore={showMore} />
7265
<button onClick={() => this.toggleShowMore()}>Toggle Show More</button>
7366
{

server/build/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import replaceCurrentBuild from './replace'
88
import md5File from 'md5-file/promise'
99

1010
export default async function build (dir, conf = null) {
11+
const buildId = uuid.v4()
1112
const buildDir = join(tmpdir(), uuid.v4())
12-
const compiler = await webpack(dir, { buildDir, conf })
13+
const compiler = await webpack(dir, { buildId, buildDir, conf })
1314

1415
try {
1516
await runCompiler(compiler)
1617
await writeBuildStats(buildDir)
17-
await writeBuildId(buildDir)
18+
await writeBuildId(buildDir, buildId)
1819
} catch (err) {
1920
console.error(`> Failed to build on ${buildDir}`)
2021
throw err
@@ -59,8 +60,7 @@ async function writeBuildStats (dir) {
5960
await fs.writeFile(buildStatsPath, JSON.stringify(assetHashMap), 'utf8')
6061
}
6162

62-
async function writeBuildId (dir) {
63+
async function writeBuildId (dir, buildId) {
6364
const buildIdPath = join(dir, '.next', 'BUILD_ID')
64-
const buildId = uuid.v4()
6565
await fs.writeFile(buildIdPath, buildId, 'utf8')
6666
}

server/build/webpack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const interpolateNames = new Map(defaultPages.map((p) => {
2727

2828
const relativeResolve = rootModuleRelativePath(require)
2929

30-
export default async function createCompiler (dir, { dev = false, quiet = false, buildDir, conf = null } = {}) {
30+
export default async function createCompiler (dir, { buildId, dev = false, quiet = false, buildDir, conf = null } = {}) {
3131
dir = resolve(dir)
3232
const config = getConfig(dir, conf)
3333
const defaultEntries = dev ? [
@@ -296,7 +296,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
296296
path: buildDir ? join(buildDir, '.next') : join(dir, config.distDir),
297297
filename: '[name]',
298298
libraryTarget: 'commonjs2',
299-
publicPath: '/_next/webpack/',
299+
publicPath: `/_next/${buildId}/webpack/`,
300300
strictModuleExceptionHandling: true,
301301
devtoolModuleFilenameTemplate ({ resourcePath }) {
302302
const hash = createHash('sha1')

server/document.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ export class Head extends Component {
6767

6868
getPreloadDynamicChunks () {
6969
const { chunks, __NEXT_DATA__ } = this.context._documentProps
70-
let { assetPrefix } = __NEXT_DATA__
70+
let { assetPrefix, buildId } = __NEXT_DATA__
7171
return chunks.map((chunk) => (
7272
<link
7373
key={chunk}
7474
rel='preload'
75-
href={`${assetPrefix}/_next/webpack/chunks/${chunk}`}
75+
href={`${assetPrefix}/_next/${buildId}/webpack/chunks/${chunk}`}
7676
as='script'
7777
/>
7878
))
@@ -153,15 +153,15 @@ export class NextScript extends Component {
153153

154154
getDynamicChunks () {
155155
const { chunks, __NEXT_DATA__ } = this.context._documentProps
156-
let { assetPrefix } = __NEXT_DATA__
156+
let { assetPrefix, buildId } = __NEXT_DATA__
157157
return (
158158
<div>
159159
{chunks.map((chunk) => (
160160
<script
161161
async
162162
key={chunk}
163163
type='text/javascript'
164-
src={`${assetPrefix}/_next/webpack/chunks/${chunk}`}
164+
src={`${assetPrefix}/_next/${buildId}/webpack/chunks/${chunk}`}
165165
/>
166166
))}
167167
</div>

server/export.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ export default async function (dir, options) {
5151
if (existsSync(join(nextDir, 'chunks'))) {
5252
log(' copying dynamic import chunks')
5353

54-
await mkdirp(join(outDir, '_next', 'webpack'))
54+
await mkdirp(join(outDir, '_next', buildId, 'webpack'))
5555
await cp(
5656
join(nextDir, 'chunks'),
57-
join(outDir, '_next', 'webpack', 'chunks')
57+
join(outDir, '_next', buildId, 'webpack', 'chunks')
5858
)
5959
}
6060

server/hot-reloader.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import onDemandEntryHandler from './on-demand-entry-handler'
55
import webpack from './build/webpack'
66
import clean from './build/clean'
77
import getConfig from './config'
8+
import UUID from 'uuid'
89
import {
910
IS_BUNDLED_PAGE
1011
} from './utils'
@@ -23,6 +24,11 @@ export default class HotReloader {
2324
this.prevChunkNames = null
2425
this.prevFailedChunkNames = null
2526
this.prevChunkHashes = null
27+
// Here buildId could be any value.
28+
// Our router accepts any value in the dev mode.
29+
// But for the webpack-compiler and for the webpack-dev-server
30+
// it should be the same value.
31+
this.buildId = UUID.v4()
2632

2733
this.config = getConfig(dir, conf)
2834
}
@@ -40,7 +46,7 @@ export default class HotReloader {
4046

4147
async start () {
4248
const [compiler] = await Promise.all([
43-
webpack(this.dir, { dev: true, quiet: this.quiet }),
49+
webpack(this.dir, { buildId: this.buildId, dev: true, quiet: this.quiet }),
4450
clean(this.dir)
4551
])
4652

@@ -66,7 +72,7 @@ export default class HotReloader {
6672
this.stats = null
6773

6874
const [compiler] = await Promise.all([
69-
webpack(this.dir, { dev: true, quiet: this.quiet }),
75+
webpack(this.dir, { buildId: this.buildId, dev: true, quiet: this.quiet }),
7076
clean(this.dir)
7177
])
7278

@@ -173,7 +179,7 @@ export default class HotReloader {
173179
]
174180

175181
let webpackDevMiddlewareConfig = {
176-
publicPath: '/_next/webpack/',
182+
publicPath: `/_next/${this.buildId}/webpack/`,
177183
noInfo: true,
178184
quiet: true,
179185
clientLogLevel: 'warning',

server/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,21 @@ export default class Server {
123123
},
124124

125125
// This is to support, webpack dynamic imports in production.
126-
'/_next/webpack/chunks/:name': async (req, res, params) => {
127-
res.setHeader('Cache-Control', 'max-age=365000000, immutable')
126+
'/_next/:buildId/webpack/chunks/:name': async (req, res, params) => {
127+
if (!this.handleBuildId(params.buildId, res)) {
128+
return this.send404(res)
129+
}
130+
128131
const p = join(this.dir, this.dist, 'chunks', params.name)
129132
await this.serveStatic(req, res, p)
130133
},
131134

132135
// This is to support, webpack dynamic import support with HMR
133-
'/_next/webpack/:id': async (req, res, params) => {
136+
'/_next/:buildId/webpack/:id': async (req, res, params) => {
137+
if (!this.handleBuildId(params.buildId, res)) {
138+
return this.send404(res)
139+
}
140+
134141
const p = join(this.dir, this.dist, 'chunks', params.id)
135142
await this.serveStatic(req, res, p)
136143
},

0 commit comments

Comments
 (0)