Skip to content

Commit

Permalink
fix(ssr): Fix the ability to use the logger with SSR frameworks
Browse files Browse the repository at this point in the history
Summary:
A change was introduced that attempted to call `window` during
the browser logger bootstrap, this is fine execpt SSR frameworks
dont have a concept of window during their initial rendering.  This
will now delay caching of the original console instance until after
the logger is enabled.

Semver: patch
  • Loading branch information
TerryMooreII committed Jun 28, 2022
1 parent 8cacfa0 commit 48cc673
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { process } from './buffer-manager';
import utils from './utils';
import { getStaticContext, getContext, getDynamicContext } from './context-manager';
import { getSessionId } from './session-manager';
import { LogMessage, LogDNALogLine } from './logdna';
import { LogMessage } from './logdna';

const captureMessage = ({ level = 'log', message, lineContext = {} }: LogMessage) => {
if (isSendingDisabled()) return;
Expand Down
1 change: 1 addition & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const init = (ingestionKey: string, opts: LogDNABrowserOptions = DEFAULT_CONFIG)
return;
}

utils.cacheConsole();
sampleRate = utils.generateSampleRateScore();
initPlugins(options);
addFlushEvents();
Expand Down
21 changes: 19 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,24 @@ const isBrowserStorageAvailable = (storage: 'localStorage' | 'sessionStorage') =

const isFunction = (fn?: Function) => typeof fn === 'function';

const { log, debug, error, warn, info } = window.console;
const originalConsole: any = { log, debug, error, warn, info };
const consoleMethods = ['log', 'error', 'debug', 'warn', 'info'];
let cachedConsole: any = consoleMethods.reduce((a: any, m: string) => ({ ...a, [m]: () => {} }), {});
const originalConsole: any = consoleMethods.reduce(
(a: any, m: string) => ({
...a,
[m]: (...args: any) => {
cachedConsole[m](...args);
},
}),
{},
);

// This will delay the caching of the original instance of the console
// until after logdna is enabled and initialized for use with SSR.
const cacheConsole = () => {
const { log, error, debug, warn, info } = window.console;
cachedConsole = { log, error, debug, warn, info };
};

export default {
validateHostname,
Expand All @@ -108,4 +124,5 @@ export default {
isBrowserStorageAvailable,
isFunction,
originalConsole,
cacheConsole,
};

0 comments on commit 48cc673

Please sign in to comment.