question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] ability to set localStorage/sessionStorage w/o loading a page from target domain

See original GitHub issue

We run Puppeteer on AWS Lambda to orchestrate multi-step, multi-page crawl sessions on our SaaS Fluxguard (e.g., login, go to dashboard, go to page C). Due to time constraints on Lambda, and other reasons, each page is handled by its own Lambda execution in sequence. We save all browser state (cookies, localStorage, webStorage) in an object store for reuse by subsequent page crawls.

The problem arises when we want to re-use saved local/sessionStorage on subsequent crawls. We cannot set local or session storage w/o first loading a page from the target site via, e.g.:

export const setLocalStorage = async (chromePage, newStorage = {}) =>
  await chromePage.evaluate(newStorage => {
    localStorage.clear();
    for (let key in newStorage) {
      localStorage.setItem(key, newStorage[key]);
    }
  }, newStorage);

We initially loaded the target page twice: first so that we could set storage, and second, once storage was set, to properly load the page w/ appropriate state. However, this is troublesome, as the first load, regardless of whether we disable Javascript/etc, will often pollute the cookie/storage space with new data. It’s also messy to have to load the page twice.

Currently, we try/catch loading “innocuous” pages of the target site, such as robots.txt and favicon.ico: we use these then to contextually set storage before loading the target page. This is “fine,” but not ideal and introduces its own problems.

It would be great if Puppeteer could set storage generally or for a specific domain without the need to load a page from that domain first.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bluepetercommented, Jan 11, 2019

@aslushnikov great recommendation! This approach is working nicely for us. And as you note it’s a lot more bulletproof than hitting favicon.ico or whatever instead. Closing!

1reaction
bluepetercommented, Jan 10, 2019

Thanks… we will try this approach and report back in this issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

LocalStorage, sessionStorage - The Modern JavaScript Tutorial
The localStorage is shared between all windows with the same origin, so if we set the data in one window, the change becomes...
Read more >
How to store objects in HTML5 localStorage/sessionStorage
setItem('testObject', JSON.stringify(testObject)); // Retrieve the object from storage var retrievedObject = localStorage.
Read more >
Client-side storage - Learn web development | MDN
One key feature of web storage is that the data persists between page loads (and even when the browser is shut down, in...
Read more >
Storage for the web
SessionStorage is tab specific, and scoped to the lifetime of the tab. It may be useful for storing small amounts of session specific ......
Read more >
JavaScript LocalStorage: a Complete Guide
It's generally divided into localStorage and sessionStorage, and the main difference between the two is how long the browser stores the data ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found