Skip to content
This repository
Browse code

Proposal for resolving #39, "Pages infinitely reload on webkit".

I'm still unsettled by the spurious popstate that occurs on page
load, though. Not sure if that happens on all pages, or just Thimble's;
we should find out.
  • Loading branch information...
commit f3dbc7a947c9f1729bf811f698220f925db372e6 1 parent 17d2d23
Atul Varma authored May 26, 2012

Showing 1 changed file with 22 additions and 19 deletions. Show diff stats Hide diff stats

  1. 41  js/main.js
41  js/main.js
@@ -41,8 +41,23 @@ define("main", function(require) {
41 41
       publishURL = $("meta[name='publish-url']").attr("content"),
42 42
       pageToLoad = $("meta[name='remix-url']").attr("content"),
43 43
       Modals = require("fc/ui/modals"),
44  
-      TextUI = require("fc/ui/text");
45  
-  
  44
+      TextUI = require("fc/ui/text"),
  45
+      supportsPushState = window.history.pushState ? true : false,
  46
+      remixURLTemplate = null;
  47
+
  48
+  if (pageToLoad) {
  49
+    // A server is serving us as the custom edit URL for a web page.
  50
+    remixURLTemplate = location.protocol + "//" + location.host +
  51
+                       "{{VIEW_URL}}/edit";
  52
+  }
  53
+  // If a URL hash is specified, it should override anything provided by
  54
+  // a server.
  55
+  if (window.location.hash.slice(1))
  56
+    pageToLoad = window.location.hash.slice(1);
  57
+
  58
+  if (supportsPushState)
  59
+    window.history.replaceState({pageToLoad: pageToLoad}, "", location.href);
  60
+    
46 61
   var codeMirror = ParsingCodeMirror($("#source")[0], {
47 62
     mode: "text/html",
48 63
     theme: "jsbin",
@@ -73,18 +88,6 @@ define("main", function(require) {
73 88
     previewArea: $("#preview-holder")
74 89
   });
75 90
   var publisher = Publisher(publishURL);
76  
-  var remixURLTemplate = null;
77  
-  
78  
-  if (pageToLoad) {
79  
-    // A server is serving us as the custom edit URL for a web page.
80  
-    remixURLTemplate = location.protocol + "//" + location.host +
81  
-                       "{{VIEW_URL}}/edit";
82  
-  }
83  
-  // If a URL hash is specified, it should override anything provided by
84  
-  // a server.
85  
-  if (window.location.hash.slice(1))
86  
-    pageToLoad = window.location.hash.slice(1);
87  
-
88 91
   var publishUI = PublishUI({
89 92
     codeMirror: codeMirror,
90 93
     publisher: publisher,
@@ -111,11 +114,7 @@ define("main", function(require) {
111 114
   var textUI = TextUI({
112 115
     codeMirror: codeMirror
113 116
   });
114  
-  
115 117
   var parachute = Parachute(window, codeMirror, pageToLoad);
116  
-  
117  
-  // TODO: This is a very temporary workaround for #39.
118  
-  var supportsPushState = false;//window.history.pushState ? true : false;
119 118
 
120 119
   // make hints on/off actually work
121 120
   $("#hints-nav-item").click(function() {
@@ -157,7 +156,11 @@ define("main", function(require) {
157 156
       // without a full page reload, unfortunately, so just trigger a
158 157
       // reload if the user clicked the 'back' button after we pushed
159 158
       // a new URL to it.
160  
-      if (!event.state || event.state.pageToLoad != pageToLoad)
  159
+      //
  160
+      // Also, for some reason Webkit is sending a spurious popstate with
  161
+      // state == null on page load, so we want to check that it's
  162
+      // non-null first (see #39).
  163
+      if (event.state && event.state.pageToLoad != pageToLoad)
161 164
         window.location.reload();
162 165
     }, false);
163 166
   

0 notes on commit f3dbc7a

Please sign in to comment.
Something went wrong with that request. Please try again.