Disable debug logs in production

console.debug logs cause some performance issues because the browser
is forced to save the logged objects just in case the user opens the
debugging tools.

They can be force-enabled back by adding ?debug=1 to the URL.

Only console.debug is disabled, console.log and other levels are a lot
less verbose and still enabled by default.
This commit is contained in:
Simon Ser 2021-12-01 11:40:59 +01:00
parent 07c9cdebb6
commit 93ba0e6443
3 changed files with 23 additions and 2 deletions

View file

@ -84,6 +84,7 @@ gamja settings can be overridden using URL query parameters:
- `nick`: nickname - `nick`: nickname
- `channels`: comma-separated list of channels to join (`#` needs to be escaped) - `channels`: comma-separated list of channels to join (`#` needs to be escaped)
- `open`: [IRC URL] to open - `open`: [IRC URL] to open
- `debug`: if set to 1, debug mode is enabled
Alternatively, the channels can be set with the URL fragment (ie, by just Alternatively, the channels can be set with the URL fragment (ie, by just
appending the channel name to the gamja URL). appending the channel name to the gamja URL).

View file

@ -48,6 +48,15 @@ const configPromise = fetch("./config.json")
const CHATHISTORY_MAX_SIZE = 4000; const CHATHISTORY_MAX_SIZE = 4000;
function isProduction() {
// NODE_ENV is set by the Parcel build system
try {
return process.env.NODE_ENV === "production";
} catch (err) {
return false;
}
}
function parseQueryString() { function parseQueryString() {
let query = window.location.search.substring(1); let query = window.location.search.substring(1);
let params = {}; let params = {};
@ -162,6 +171,7 @@ export default class App extends Component {
memberList: false, memberList: false,
}, },
}; };
debug = !isProduction();
config = { ...baseConfig }; config = { ...baseConfig };
clients = new Map(); clients = new Map();
endOfHistory = new Map(); endOfHistory = new Map();
@ -279,6 +289,9 @@ export default class App extends Component {
if (typeof queryParams.open === "string") { if (typeof queryParams.open === "string") {
this.autoOpenURL = irc.parseURL(queryParams.open); this.autoOpenURL = irc.parseURL(queryParams.open);
} }
if (queryParams.debug === "1") {
this.debug = true;
}
if (window.location.hash) { if (window.location.hash) {
autojoin = window.location.hash.split(","); autojoin = window.location.hash.split(",");
@ -579,6 +592,8 @@ export default class App extends Component {
this.setState({ connectParams: params }); this.setState({ connectParams: params });
let client = new Client(fillConnectParams(params)); let client = new Client(fillConnectParams(params));
client.debug = this.debug;
this.clients.set(serverID, client); this.clients.set(serverID, client);
this.setServerState(serverID, { status: client.status }); this.setServerState(serverID, { status: client.status });

View file

@ -76,6 +76,7 @@ export default class Client extends EventTarget {
saslExternal: false, saslExternal: false,
bouncerNetwork: null, bouncerNetwork: null,
}; };
debug = false;
batches = new Map(); batches = new Map();
autoReconnect = true; autoReconnect = true;
reconnectTimeoutID = null; reconnectTimeoutID = null;
@ -225,7 +226,9 @@ export default class Client extends EventTarget {
} }
let msg = irc.parseMessage(event.data); let msg = irc.parseMessage(event.data);
if (this.debug) {
console.debug("Received:", msg); console.debug("Received:", msg);
}
// If the prefix is missing, assume it's coming from the server on the // If the prefix is missing, assume it's coming from the server on the
// other end of the connection // other end of the connection
@ -653,8 +656,10 @@ export default class Client extends EventTarget {
throw new Error("Failed to send IRC message " + msg.command + ": socket is closed"); throw new Error("Failed to send IRC message " + msg.command + ": socket is closed");
} }
this.ws.send(irc.formatMessage(msg)); this.ws.send(irc.formatMessage(msg));
if (this.debug) {
console.debug("Sent:", msg); console.debug("Sent:", msg);
} }
}
setCaseMapping(name) { setCaseMapping(name) {
this.cm = irc.CaseMapping.byName(name); this.cm = irc.CaseMapping.byName(name);