17

The pushState method accepts a state object. Firefox documents say the maximum size of this object is 640kb. Is it defined in the specs what the smallest maximum size a browser can implement is? Can I reasonably expect major browsers to provide me with at least 100kb?

EDIT: I tested it out with Chrome, and it was still working for state objects over 1MB.

Kyle
  • 21,377
  • 37
  • 113
  • 200
  • 1
    Hey @Kyle, you should mark an answer as accepted or respond to ask for whatever extra information you need to get an acceptable answer. – Maverick Jul 26 '14 at 04:56

5 Answers5

18

The specification doesn't set out a limit, however the various browser do have their own limits.

Firefox's is well documented and as you said, it's 640kB ("as much RAM as anybody will ever need").

I couldn't find Chrome or Internet Explorer's listed anywhere, but some quick testing shows:

Chrome working at least up to 10MB (and possibly further),

IE hitting the limit at 1MB (in IE11, which is all I have handy).

So, to summarise for the people of the future: history.state object size limit is: 640kB for Firefox, 1MB for Internet Explorer 11 and at least 10Mb for Chrome.

EDIT: Versions tested: IE: 11, Chrome: 33, Firefox: Irrelevant as they document the max size on MDN for you :).

Maverick
  • 4,449
  • 4
  • 36
  • 46
  • 1
    The 640K reference is to this: "640K ought to be enough for anybody -- Bill Gates, 1981" "So what happened, Bill?" "Do you realize the pain the industry went through while the IBM PC was limited to 640K? The machine was going to be 512K at one point, and we kept pushing it up. I never said that statement — I said the opposite of that. "Gates talks" (20 August 2001) U.S. News & World Report" – Gregory Magarshak Aug 12 '14 at 17:06
  • Could you tell whether the limits were affected by the number of objects, as quoting from a quote of the answer above "User agents may limit the number of state objects added to the session history per page."? what did things look like when the limit was reached? an error message? a crash? I guess new experiments are due by now, but thanks for this account.... – matanster Oct 18 '15 at 20:44
  • 1
    The limit for Firefox is not 640 kB it's 640 kb. 640 thousand characters: "we impose a size limit of 640k characters on the serialized representation of a state object" (https://developer.mozilla.org/en-US/docs/Web/API/History_API) – Sean Kendle Jan 26 '17 at 15:05
  • 3
    Is this per entry in your history? Or is 640k the max size of the sum of all your state objects across every page in the current website's history? – faceyspacey.com Oct 11 '17 at 05:40
  • The documentation on mozilla.org is ambiguous. In the History API documentation, it does indeed say 640KB is the limit. In the History.pushState documentation, it says the limit is 2MB. – Paul Brannan Jun 14 '21 at 21:45
  • The largest that firefox would accept for me was just under 2MB (using: history.replaceState('x'.repeat(2**21-16), ''); ) – Paul Brannan Jun 14 '21 at 21:50
9

No. The normative document here is http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#dom-history-pushstate and it doesn't even mention a limit for the data size. A different limit is suggested however:

User agents may limit the number of state objects added to the session history per page.

As you can see on this example the specification generally avoids mentioning any hard limits and leaves them at the discretion of browser makers. So even if the spec is revised at some point in future to consider the possibility of data size limits, it is unlikely to give you a real number. Instead it will be "big enough for common use cases".

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
0

only see the MDN tells that FireFox impose a size limit to 640K, don't know other browsers. https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history

YuC
  • 1,787
  • 3
  • 19
  • 22
0

Painstakingly, I have a page that is exceeding the character limit on IE11. I did a substring operation to get an exact character count since I couldn't find it anywhere. The answer is that (at least on IE11) 524282 characters are allowed to be passed to the pushState/replaceState.
I handled that via the following code:

function pushState(data, title, url) {
  if (data.length > 524282) {
      //can't push the data to the History API--pass null
      history.pushState(null, title, url);
      history.replaceState(null, title, url);
  }
  else {
    history.pushState(data, title, url);
    history.replaceState(data, title, url);
  }
    document.title = title;
}

I call beforeNavigate to save any current position information or state changes made by the user before loading the new content via an ajax request.

function beforeNavigate(){
    if ($("#container").html().length <= 524282) {
        //save current state to history before navigating via ajax
        history.replaceState($("#container").html(), document.title, window.location.pathname);
    }
}

Handle pushing the back and forward buttons by listening for popstate. If we passed a null value for data, then e.state will return null, and we need to load the stored url via an ajax request.

window.addEventListener('popstate', function (e) {
    if (e.state!=null) {
        $("#container").html(e.state);
    }
    else {
        //load the partialpage into the container(a full html page is not returned by the ajax query for my site)
        $.ajax({
            url: location.href,
            type: "GET",
            success: function (data) {
                $('#container').html(data);
            }
         });
    }
});
C. Graham
  • 328
  • 2
  • 10
0

Firefox now imposes a limit of 16MiB on history.state: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState#parameters

Chrome and other browsers still do not document this, see other answers for detail.

Peter Warrington
  • 654
  • 10
  • 32