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

Process available chunk names properly in dev mode #4604

Merged
merged 1 commit into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion server/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default async function (dir, options, configuration) {
dev: false,
staticMarkup: false,
hotReloader: null,
availableChunks: getAvailableChunks(distDir)
availableChunks: getAvailableChunks(distDir, false)
}

const {serverRuntimeConfig, publicRuntimeConfig} = nextConfig
Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class Server {
distDir: this.distDir,
hotReloader: this.hotReloader,
buildId: this.buildId,
availableChunks: dev ? {} : getAvailableChunks(this.distDir),
availableChunks: dev ? {} : getAvailableChunks(this.distDir, dev),
generateEtags
}

Expand Down
2 changes: 1 addition & 1 deletion server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function loadChunks ({ dev, distDir, availableChunks }) {
}

if (dev) {
availableChunks = getAvailableChunks(distDir)
availableChunks = getAvailableChunks(distDir, dev)
}

for (var chunk of flushedChunks) {
Expand Down
9 changes: 6 additions & 3 deletions server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { readdirSync, existsSync } from 'fs'
export const IS_BUNDLED_PAGE = /^bundles[/\\]pages.*\.js$/
export const MATCH_ROUTE_NAME = /^bundles[/\\]pages[/\\](.*)\.js$/

export function getChunkNameFromFilename (filename) {
export function getChunkNameFromFilename (filename, dev) {
if (dev) {
return filename.replace(/.[^.]*$/, '')
}
return filename.replace(/-[^-]*$/, '')
}

export function getAvailableChunks (distDir) {
export function getAvailableChunks (distDir, dev) {
const chunksDir = join(distDir, 'chunks')
if (!existsSync(chunksDir)) return {}

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

chunkFiles.forEach(filename => {
if (/\.js$/.test(filename)) {
const chunkName = getChunkNameFromFilename(filename)
const chunkName = getChunkNameFromFilename(filename, dev)
chunksMap[chunkName] = filename
}
})
Expand Down
26 changes: 20 additions & 6 deletions test/unit/server-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ import { getChunkNameFromFilename } from '../../dist/server/utils'

describe('Server utils', () => {
describe('getChunkNameFromFilename', () => {
it('should strip the hash from the filename', () => {
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename)).toBe('foo_bar_0123456789abcdef')
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')
})
})

it('should only strip the part after the last hyphen in the filename', () => {
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename)).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')
})
})
})
})