DEV Community

Cover image for How to make a browser console log wait until after a page reload
Ty Mick
Ty Mick

Posted on • Edited on • Originally published at tymick.me

How to make a browser console log wait until after a page reload

Ever wanted to create a browser console log that persists after a page reloads? Sure, that isn't a problem if you enable the "preserve log" option in your developer console, but hear me out.

Say you have a JavaScript framework with a development server that usually hot-reloads when you update files while the server is running. In the few cases where hot reloading isn't possible and the page has to fully reload, you want to warn your users and explain why this is happening. It'd be nice to just log a warning to the console with console.warn(), but the moment it shows up in the console, wouldn't you know, the page reloads, and the browser clears it. Even if your user is eagle-eyed enough to notice your warning flash on the screen for a fraction of a second, it certainly isn't there long enough to actually read.

You could say that if any of your users don't know where the "preserve log" button is then that's their problem, but you're kinder than that. You'd rather help them fall into the Pit of Success.

As it happens, that's the very issue I ran into the other day when strolling through my favorite open source project, Next.js.

In my naïveté, I first tried just moving the console.warn() to the line after window.location.reload() and crossing my fingers, but that didn't help. I tried googling things like "console log after page reload", but that only gave me instructions for how to turn on "preserve log", which I already knew how to do.

Here's what worked. Where the console.warn() statement stood before, I instead saved the text content of the warning to the window's sessionStorage in a key called "consoleWarnAfterReload":

sessionStorage.setItem(
  "consoleWarnAfterReload",
  "Dear me, the page had to fully reload!"
);
Enter fullscreen mode Exit fullscreen mode

Then, near the top of a file involved in the loading of a new page, I added a few lines that check that same sessionStorage key. If the key exists, it logs the contents of the warning to the console and then removes the item from sessionStorage:

if (sessionStorage && sessionStorage.getItem("consoleWarnAfterReload")) {
  console.warn(sessionStorage.getItem("consoleWarnAfterReload"));
  sessionStorage.removeItem("consoleWarnAfterReload");
}
Enter fullscreen mode Exit fullscreen mode

That did the trick!


Know another way to make console logs wait until after a page reloads? Let me know in the discussion below—I'd love to hear about it!

Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

You could say that if any of your users don't know where the "preserve log" button is then that's their problem, but you're kinder than that

I get what you're saying about making things easier on your users, but if your users are looking at the javascript console as part of their work, I think we're already into "power user" territory.

What you're describing doing, with holding a variable across page loads in a session, is usually called "flash messages" or something similar, and it's used on the back-end as well to do things like present success messages even after the POSTed form has redirected to another page via header location response.

If you're depending on people reading the log, you could potentially improve the UX by beginning each page load with an informational message saying something like:

This is the log for Awesome Project.
Please ensure that your have "preserve log" turned on in your console settings. For more information, please consult our documentation

My concern is that if you're using the console to tell people things, then they presumably need to see other console messages which will be lost without "preserve log". On the other hand, they'll now need to clear it manually or it'll get overwhelming!

I can't think of any real-life process where people would use the log for out-of-band information that couldn't be displayed in a different way.

Collapse
 
tymick profile image
Ty Mick

That makes sense. And it looks like the Next.js team thought of something even better, actually: adding an informative overlay to the page in the dev server when the first full reload occurs.