Skip to content

Commit 7333dd6

Browse files
fatfisztimneutkens
authored andcommitted
Process available chunk names properly in dev mode (#4604)
Fixes #4603. Tests explain it the best: ```js describe('development mode (no chunkhash)', () => { it('should strip the extension from the filename', () => { const filename = 'foo_bar_0123456789abcdef.js' expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef') }) it('should only strip the extension even if there\'s a hyphen in the name', () => { const filename = 'foo-bar-0123456789abcdef.js' expect(getChunkNameFromFilename(filename, true)).toBe('foo-bar-0123456789abcdef') }) }) describe('production mode (with chunkhash)', () => { it('should strip the hash from the filename', () => { const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js' expect(getChunkNameFromFilename(filename, false)).toBe('foo_bar_0123456789abcdef') }) it('should only strip the part after the last hyphen in the filename', () => { const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js' expect(getChunkNameFromFilename(filename, false)).toBe('foo-bar-0123456789abcdef') }) }) ```
1 parent ecf61f6 commit 7333dd6

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

server/export.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default async function (dir, options, configuration) {
9191
dev: false,
9292
staticMarkup: false,
9393
hotReloader: null,
94-
availableChunks: getAvailableChunks(distDir)
94+
availableChunks: getAvailableChunks(distDir, false)
9595
}
9696

9797
const {serverRuntimeConfig, publicRuntimeConfig} = nextConfig

server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class Server {
5454
distDir: this.distDir,
5555
hotReloader: this.hotReloader,
5656
buildId: this.buildId,
57-
availableChunks: dev ? {} : getAvailableChunks(this.distDir),
57+
availableChunks: dev ? {} : getAvailableChunks(this.distDir, dev),
5858
generateEtags
5959
}
6060

server/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ function loadChunks ({ dev, distDir, availableChunks }) {
238238
}
239239

240240
if (dev) {
241-
availableChunks = getAvailableChunks(distDir)
241+
availableChunks = getAvailableChunks(distDir, dev)
242242
}
243243

244244
for (var chunk of flushedChunks) {

server/utils.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import { readdirSync, existsSync } from 'fs'
44
export const IS_BUNDLED_PAGE = /^bundles[/\\]pages.*\.js$/
55
export const MATCH_ROUTE_NAME = /^bundles[/\\]pages[/\\](.*)\.js$/
66

7-
export function getChunkNameFromFilename (filename) {
7+
export function getChunkNameFromFilename (filename, dev) {
8+
if (dev) {
9+
return filename.replace(/.[^.]*$/, '')
10+
}
811
return filename.replace(/-[^-]*$/, '')
912
}
1013

11-
export function getAvailableChunks (distDir) {
14+
export function getAvailableChunks (distDir, dev) {
1215
const chunksDir = join(distDir, 'chunks')
1316
if (!existsSync(chunksDir)) return {}
1417

@@ -17,7 +20,7 @@ export function getAvailableChunks (distDir) {
1720

1821
chunkFiles.forEach(filename => {
1922
if (/\.js$/.test(filename)) {
20-
const chunkName = getChunkNameFromFilename(filename)
23+
const chunkName = getChunkNameFromFilename(filename, dev)
2124
chunksMap[chunkName] = filename
2225
}
2326
})

test/unit/server-utils.test.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@ import { getChunkNameFromFilename } from '../../dist/server/utils'
44

55
describe('Server utils', () => {
66
describe('getChunkNameFromFilename', () => {
7-
it('should strip the hash from the filename', () => {
8-
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
9-
expect(getChunkNameFromFilename(filename)).toBe('foo_bar_0123456789abcdef')
7+
describe('development mode (no chunkhash)', () => {
8+
it('should strip the extension from the filename', () => {
9+
const filename = 'foo_bar_0123456789abcdef.js'
10+
expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef')
11+
})
12+
13+
it('should only strip the extension even if there\'s a hyphen in the name', () => {
14+
const filename = 'foo-bar-0123456789abcdef.js'
15+
expect(getChunkNameFromFilename(filename, true)).toBe('foo-bar-0123456789abcdef')
16+
})
1017
})
1118

12-
it('should only strip the part after the last hyphen in the filename', () => {
13-
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
14-
expect(getChunkNameFromFilename(filename)).toBe('foo-bar-0123456789abcdef')
19+
describe('production mode (with chunkhash)', () => {
20+
it('should strip the hash from the filename', () => {
21+
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
22+
expect(getChunkNameFromFilename(filename, false)).toBe('foo_bar_0123456789abcdef')
23+
})
24+
25+
it('should only strip the part after the last hyphen in the filename', () => {
26+
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
27+
expect(getChunkNameFromFilename(filename, false)).toBe('foo-bar-0123456789abcdef')
28+
})
1529
})
1630
})
1731
})

0 commit comments

Comments
 (0)