Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit ca4e11a

Browse files
authored
fix: i18n reference to root causes memory leak (#1044) (#1151)
1 parent 25470cc commit ca4e11a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/mixin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ export default {
4242
options.i18n.silentFallbackWarn = rootI18n.silentFallbackWarn
4343
options.i18n.pluralizationRules = rootI18n.pluralizationRules
4444
options.i18n.preserveDirectiveContent = rootI18n.preserveDirectiveContent
45+
this.$root.$once('hook:beforeDestroy', () => {
46+
options.i18n.root = null;
47+
options.i18n.formatter = null;
48+
options.i18n.fallbackLocale = null;
49+
options.i18n.formatFallbackMessages = null;
50+
options.i18n.silentTranslationWarn = null;
51+
options.i18n.silentFallbackWarn = null;
52+
options.i18n.pluralizationRules = null;
53+
options.i18n.preserveDirectiveContent = null;
54+
});
4555
}
4656

4757
// init locale messages via custom blocks

test/unit/issues.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import messages from './fixture/index'
22
import { parse } from '../../src/format'
33
import VueI18n from '../../src'
4+
import { Vue } from '../../src/install'
45
const compiler = require('vue-template-compiler')
56

67
const delay = time => new Promise(resolve => setTimeout(resolve, time))
@@ -498,6 +499,42 @@ describe('issues', () => {
498499
}).then(done)
499500
})
500501
})
502+
// 1044
503+
describe('#1044', () => {
504+
it('should be free memory', done => {
505+
const i18n = {
506+
messages: {
507+
hello: 'hello world!'
508+
}
509+
}
510+
const Test = {
511+
i18n,
512+
}
513+
const vm = new Vue({
514+
components: {
515+
Test
516+
},
517+
i18n: {
518+
locale: 'en',
519+
},
520+
render (h) {
521+
return h('Test')
522+
}
523+
}).$mount()
524+
vm.$destroy();
525+
526+
assert.strictEqual(i18n.root, null)
527+
assert.strictEqual(i18n.formatter, null)
528+
assert.strictEqual(i18n.fallbackLocale, null)
529+
assert.strictEqual(i18n.formatFallbackMessages, null)
530+
assert.strictEqual(i18n.silentTranslationWarn, null)
531+
assert.strictEqual(i18n.silentFallbackWarn, null)
532+
assert.strictEqual(i18n.pluralizationRules, null)
533+
assert.strictEqual(i18n.preserveDirectiveContent, null)
534+
535+
done();
536+
});
537+
})
501538

502539
describe('#78, #464', () => {
503540
it('should fallback to default pluralization', () => {

0 commit comments

Comments
 (0)