@webqit/webflo 0.10.0 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "vanila-javascript"
13
13
  ],
14
14
  "homepage": "https://webqit.io/tooling/webflo",
15
- "version": "0.10.0",
15
+ "version": "0.10.3",
16
16
  "license": "MIT",
17
17
  "repository": {
18
18
  "type": "git",
@@ -88,7 +88,7 @@ export default class Runtime {
88
88
  // Needed to allow window.document.location
89
89
  // to update to window.location
90
90
  window.setTimeout(() => {
91
- this.go(Url.copy(window.document.location), { src: window.document.location, srcType: 'history', });
91
+ this.go(Url.copy(window.document.location), {}, { src: window.document.location, srcType: 'history', });
92
92
  }, 0);
93
93
  });
94
94
 
@@ -186,19 +186,21 @@ export default class Runtime {
186
186
  * @return Response
187
187
  */
188
188
  async go(url, init = {}, detail = {}) {
189
- if (this._abortController) {
190
- this._abortController.abort();
191
- }
192
- this._abortController = new AbortController();
193
- this._xRedirectCode = 200;
194
- // ------------
195
189
  url = typeof url === 'string' ? new whatwag.URL(url) : url;
196
190
  init = { referrer: this.location.href, ...init };
197
191
  // ------------
192
+ // Put his forward before instantiating a request and aborting previous
193
+ // Same-page hash-links clicks on chrome recurse here from histroy popstate
198
194
  if (detail.srcType !== 'init' && (_before(url.href, '#') === _before(init.referrer, '#') && (init.method || 'GET').toUpperCase() === 'GET')) {
199
195
  return;
200
196
  }
201
197
  // ------------
198
+ if (this._abortController) {
199
+ this._abortController.abort();
200
+ }
201
+ this._abortController = new AbortController();
202
+ this._xRedirectCode = 200;
203
+ // ------------
202
204
  if (['link', 'form'].includes(detail.srcType)) {
203
205
  Observer.set(detail.src, 'active', true);
204
206
  Observer.set(detail.submitter || {}, 'active', true);
@@ -72,14 +72,25 @@ export default class Worker {
72
72
 
73
73
  // -------------
74
74
  // ONFETCH
75
- self.addEventListener('fetch', async evt => {
75
+ self.addEventListener('fetch', event => {
76
76
  // URL schemes that might arrive here but not supported; e.g.: chrome-extension://
77
- if (!evt.request.url.startsWith('http')) return;
78
- const deriveInit = req => [
79
- 'method', 'headers', 'body', 'mode', 'credentials', 'cache', 'redirect', 'referrer', 'integrity',
80
- ].reduce((init, prop) => ({ [prop]: req[prop], ...init }), {});
81
- const requestInit = deriveInit(evt.request);
82
- evt.respondWith(this.go(evt.request.url, requestInit, { event: evt }));
77
+ if (!event.request.url.startsWith('http')) return;
78
+ event.respondWith((async (req, evt) => {
79
+ const requestInit = [
80
+ 'method', 'headers', 'mode', 'credentials', 'cache', 'redirect', 'referrer', 'integrity',
81
+ ].reduce((init, prop) => ({ [prop]: req[prop], ...init }), {});
82
+ if (!['GET', 'HEAD'].includes(req.method)) {
83
+ requestInit.body = await req.text();
84
+ }
85
+ // Now, the following is key:
86
+ // The browser likes to use "force-cache" for "navigate" requests, when, e.g: re-entering your site with the back button
87
+ // Problem here, force-cache forces out JSON not HTML as per webflo's design.
88
+ // So, we detect this scenerio and avoid it.
89
+ if (req.cache === 'force-cache'/* && req.mode === 'navigate' - even webflo client init call also comes with that... needs investigation */) {
90
+ requestInit.cache = 'default';
91
+ }
92
+ return this.go(req.url, requestInit, { event: evt });
93
+ })(event.request, event));
83
94
  });
84
95
 
85
96
  // ---------------
@@ -107,16 +118,15 @@ export default class Worker {
107
118
  init = { referrer: this.location.href, ...init };
108
119
  // ------------
109
120
  // The request object
110
- let request = this.generateRequest(url.href, init);
111
- if (detail.event instanceof self.Request) {
112
- request = detail.event.request;
121
+ let request = await this.generateRequest(url.href, init);
122
+ if (detail.event) {
113
123
  Object.defineProperty(detail.event, 'request', { value: request });
114
124
  }
115
125
  // The navigation event
116
126
  let httpEvent = new HttpEvent(request, detail, (id = null, persistent = false) => this.getSession(httpEvent, id, persistent));
117
127
  httpEvent.port.listen(message => {
118
128
  if (message.$type === 'handler:hints' && message.session) {
119
- // TODO: Sync sesseion data from client
129
+ // TODO: Sync session data from client
120
130
  return Promise.resolve();
121
131
  }
122
132
  });
@@ -134,15 +144,7 @@ export default class Worker {
134
144
 
135
145
  // Generates request object
136
146
  generateRequest(href, init) {
137
- // Now, the following is key:
138
- // The browser likes to use "force-cache" for "navigate" requests
139
- // when, for example, the back button was used.
140
- // Thus the origin server would still not be contacted by the self.fetch() below, leading to inconsistencies in responses.
141
- // So, we detect this scenerio and avoid it.
142
- if (init.mode === 'navigate' && init.cache === 'force-cache') {
143
- init = { ...init, cache: 'default' };
144
- }
145
- let request = new Request(href, init);
147
+ let request = new Request(href, init);
146
148
  return request;
147
149
  }
148
150
 
@@ -94,7 +94,7 @@ const xHttpMessage = (whatwagHttpMessage, Headers, FormData) => {
94
94
  this.bodyAttrs.jsonfied = new Promise(async (resolve, reject) => {
95
95
  var messageInstance = this, jsonfied, contentType = messageInstance.headers.get('content-type') || '';
96
96
  var type = contentType === 'application/json' || this.bodyAttrs.json ? 'json' : (
97
- contentType === 'application/x-www-form-urlencoded' || contentType.startsWith('multipart/') || this.bodyAttrs.formData ? 'formData' : (
97
+ contentType === 'application/x-www-form-urlencoded' || contentType.startsWith('multipart/form-data') || this.bodyAttrs.formData ? 'formData' : (
98
98
  contentType === 'text/plain' ? 'plain' : 'other'
99
99
  )
100
100
  );
@@ -62,6 +62,13 @@ const xRequest = (whatwagRequest, Headers, FormData, Blob) => class extends xHtt
62
62
  return 'referrer' in this.attrs ? this.attrs.referrer : super.referrer;
63
63
  }
64
64
 
65
+ static compat(request, url = null) {
66
+ if (request instanceof whatwagRequest) {
67
+ return Object.setPrototypeOf(request, new this);
68
+ }
69
+ return new this(url, request);
70
+ }
71
+
65
72
  };
66
73
 
67
74
  export default xRequest;