Skip to content

Commit

Permalink
fix: i18n reference to root causes memory leak (#1044) (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzxb committed Mar 22, 2021
1 parent 25470cc commit ca4e11a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export default {
options.i18n.silentFallbackWarn = rootI18n.silentFallbackWarn
options.i18n.pluralizationRules = rootI18n.pluralizationRules
options.i18n.preserveDirectiveContent = rootI18n.preserveDirectiveContent
this.$root.$once('hook:beforeDestroy', () => {
options.i18n.root = null;
options.i18n.formatter = null;
options.i18n.fallbackLocale = null;
options.i18n.formatFallbackMessages = null;
options.i18n.silentTranslationWarn = null;
options.i18n.silentFallbackWarn = null;
options.i18n.pluralizationRules = null;
options.i18n.preserveDirectiveContent = null;
});
}

// init locale messages via custom blocks
Expand Down
37 changes: 37 additions & 0 deletions test/unit/issues.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import messages from './fixture/index'
import { parse } from '../../src/format'
import VueI18n from '../../src'
import { Vue } from '../../src/install'
const compiler = require('vue-template-compiler')

const delay = time => new Promise(resolve => setTimeout(resolve, time))
Expand Down Expand Up @@ -498,6 +499,42 @@ describe('issues', () => {
}).then(done)
})
})
// 1044
describe('#1044', () => {
it('should be free memory', done => {
const i18n = {
messages: {
hello: 'hello world!'
}
}
const Test = {
i18n,
}
const vm = new Vue({
components: {
Test
},
i18n: {
locale: 'en',
},
render (h) {
return h('Test')
}
}).$mount()
vm.$destroy();

assert.strictEqual(i18n.root, null)
assert.strictEqual(i18n.formatter, null)
assert.strictEqual(i18n.fallbackLocale, null)
assert.strictEqual(i18n.formatFallbackMessages, null)
assert.strictEqual(i18n.silentTranslationWarn, null)
assert.strictEqual(i18n.silentFallbackWarn, null)
assert.strictEqual(i18n.pluralizationRules, null)
assert.strictEqual(i18n.preserveDirectiveContent, null)

done();
});
})

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

0 comments on commit ca4e11a

Please sign in to comment.