Skip to content

Commit

Permalink
[Logger] Fix SSR (localStorage)
Browse files Browse the repository at this point in the history
  • Loading branch information
techniq committed Sep 4, 2023
1 parent e090cd5 commit 935291a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-swans-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

[Logger] Fix SSR (due to localStorage)
14 changes: 9 additions & 5 deletions src/lib/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { browser } from './env';

const logLevels = ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR'] as const;
type LogLevel = (typeof logLevels)[number];

Expand Down Expand Up @@ -37,11 +39,13 @@ export default class Logger {
}

log(level: LogLevel, ...message: any) {
const enabledLoggers =
localStorage
.getItem('logger')
?.split(',')
.map((x) => x.split(':') as [string, LogLevel?]) ?? [];
// TODO: Consider checking `env` for SSR support?
const enabledLoggers = browser
? localStorage
.getItem('logger')
?.split(',')
.map((x) => x.split(':') as [string, LogLevel?]) ?? []
: [];

const enabledLogger = enabledLoggers.find((x) => x[0] === this.name);

Expand Down

1 comment on commit 935291a

@vercel
Copy link

@vercel vercel bot commented on 935291a Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.