Skip to content

Commit

Permalink
fix: not throw error in SSR environment
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed May 14, 2024
1 parent b894be7 commit 7334ab3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/wzhudev/squirrel/master/src/schema/package.schema.json",
"name": "@wendellhu/redi",
"version": "0.15.0",
"version": "0.15.1",
"description": "A dependency library for TypeScript and JavaScript, along with a binding for React.",
"scripts": {
"test": "vitest run",
Expand Down
6 changes: 4 additions & 2 deletions src/publicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ export { RediError } from './error'
const globalObject: any =
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof window !== 'undefined' && window) ||
// @ts-ignore
(typeof global !== 'undefined' && global)

const __REDI_GLOBAL_LOCK__ = 'REDI_GLOBAL_LOCK'
const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null

if (globalObject[__REDI_GLOBAL_LOCK__]) {
console.error(`[redi]: You are loading scripts of redi more than once! This may cause undesired behavior in your application.
if (!isNode) {
console.error(`[redi]: You are loading scripts of redi more than once! This may cause undesired behavior in your application.
Maybe your dependencies added redi as its dependency and bundled redi to its dist files. Or you import different versions of redi.
For more info please visit our website: https://redi.wendell.fun/en-US/docs/debug#import-scripts-of-redi-more-than-once`)
}
} else {
globalObject[__REDI_GLOBAL_LOCK__] = true
}
22 changes: 7 additions & 15 deletions src/react-bindings/reactContext.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import * as React from 'react'
import { Injector, RediError } from '@wendellhu/redi'
import { Injector } from '@wendellhu/redi'

declare global {
interface Window {
RediContextCreated: string | null
}
}

const RediContextCreated = '__RediContextCreated__'
const __REDI_CONTEXT_LOCK__ = 'REDI_CONTEXT_LOCK'
const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null

const globalObject: any =
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof window !== 'undefined' && window) ||
// @ts-ignore
(typeof global !== 'undefined' && global)

if (!globalObject.RediContextCreated) {
globalObject.RediContextCreated = RediContextCreated
} else {
throw new RediError(
'"RediContext" is already created. You may import "RediContext" from different paths. Use "import { RediContext } from \'@wendellhu/redi/react-bindings\'; instead."'
)
if (!globalObject[__REDI_CONTEXT_LOCK__]) {
globalObject[__REDI_CONTEXT_LOCK__] = true
} else if (!isNode) {
console.error('[redi]: "RediContext" is already created. You may import "RediContext" from different paths. Use "import { RediContext } from \'@wendellhu/redi/react-bindings\'; instead."')
}

export interface IRediContext {
Expand Down

0 comments on commit 7334ab3

Please sign in to comment.