Skip to content

Commit

Permalink
Merge pull request #423 from LeetCode-OpenSource/fix/ssr-module
Browse files Browse the repository at this point in the history
fix(ssr): avoid to throw error after HMR in SsrModule
  • Loading branch information
zry656565 authored Oct 29, 2020
2 parents 88a1d16 + 240c4ce commit 331b940
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ssr/ssr-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export const SSRModule = (config: string | InjectableConfig & { name: string })
let name: string
if (typeof config === 'string') {
if (configSets.has(config)) {
throw new Error(`Duplicated Module name: ${config}`)
reportDuplicated(config)
}
name = config
configSets.add(config)
} else if (config && typeof config.name === 'string') {
if (configSets.has(config.name)) {
throw new Error(`Duplicated Module name: ${config.name}`)
reportDuplicated(config.name)
}
configSets.add(config.name)
name = config.name
Expand All @@ -33,3 +33,11 @@ export const SSRModule = (config: string | InjectableConfig & { name: string })
return Injectable(injectableConfig)(target)
}
}

function reportDuplicated(moduleName: string) {
if (process.env.NODE_ENV === 'production') {
throw new Error(`Duplicated Module name: ${moduleName}`)
}
// avoid to throw error after HMR
console.warn(`Duplicated Module name: ${moduleName}`)
}
5 changes: 5 additions & 0 deletions test/specs/ssr.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ describe('SSR specs:', () => {
beforeAll(() => {
// @ts-ignore
process.env.ENABLE_AYANAMI_SSR = 'true'
process.env.NODE_ENV = 'production'
})

afterAll(() => {
// @ts-ignore
process.env.ENABLE_AYANAMI_SSR = 'false'
delete process.env.NODE_ENV
})

it('should throw if module name not given', () => {
Expand Down Expand Up @@ -172,6 +174,9 @@ describe('SSR specs:', () => {
expect(generateException1).toThrow()
expect(generateException2).toThrow()
expect(generateException3).toThrow()
process.env.NODE_ENV = 'development'
expect(generateException1).not.toThrow()
expect(generateException2).not.toThrow()
})

it('should run ssr effects', async () => {
Expand Down

0 comments on commit 331b940

Please sign in to comment.