browser-initor.tsx 879 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use client'
  2. class StorageMock {
  3. data: Record<string, string>
  4. constructor() {
  5. this.data = {} as Record<string, string>
  6. }
  7. setItem(name: string, value: string) {
  8. this.data[name] = value
  9. }
  10. getItem(name: string) {
  11. return this.data[name] || null
  12. }
  13. removeItem(name: string) {
  14. delete this.data[name]
  15. }
  16. clear() {
  17. this.data = {}
  18. }
  19. }
  20. let localStorage, sessionStorage
  21. try {
  22. localStorage = globalThis.localStorage
  23. sessionStorage = globalThis.sessionStorage
  24. }
  25. catch (e) {
  26. localStorage = new StorageMock()
  27. sessionStorage = new StorageMock()
  28. }
  29. Object.defineProperty(globalThis, 'localStorage', {
  30. value: localStorage,
  31. })
  32. Object.defineProperty(globalThis, 'sessionStorage', {
  33. value: sessionStorage,
  34. })
  35. const BrowserInitor = ({
  36. children,
  37. }: { children: React.ReactElement }) => {
  38. return children
  39. }
  40. export default BrowserInitor