Bläddra i källkod

localStorage -> sessionStorage

陈帅 7 år sedan
förälder
incheckning
36eeb6c169
4 ändrade filer med 58 tillägg och 56 borttagningar
  1. 28 0
      .stylelintrc.json
  2. 3 3
      src/pages/Account/Center/Center.js
  3. 27 25
      src/utils/request.js
  4. 0 28
      stylelint.config.js

+ 28 - 0
.stylelintrc.json

@@ -0,0 +1,28 @@
+{
+  "extends": ["stylelint-config-standard", "stylelint-config-prettier"],
+  "rules": {
+    "selector-pseudo-class-no-unknown": null,
+    "shorthand-property-no-redundant-values": null,
+    "at-rule-empty-line-before": null,
+    "at-rule-name-space-after": null,
+    "comment-empty-line-before": null,
+    "declaration-bang-space-before": null,
+    "declaration-empty-line-before": null,
+    "function-comma-newline-after": null,
+    "function-name-case": null,
+    "function-parentheses-newline-inside": null,
+    "function-max-empty-lines": null,
+    "function-whitespace-after": null,
+    "number-leading-zero": null,
+    "number-no-trailing-zeros": null,
+    "rule-empty-line-before": null,
+    "selector-combinator-space-after": null,
+    "selector-descendant-combinator-no-non-space": null,
+    "selector-list-comma-newline-after": null,
+    "selector-pseudo-element-colon-notation": null,
+    "unit-no-unknown": null,
+    "no-descending-specificity": null,
+    "value-list-max-empty-lines": null,
+    "no-missing-end-of-source-newline": null
+  }
+}

+ 3 - 3
src/pages/Account/Center/Center.js

@@ -148,9 +148,9 @@ export default class Center extends PureComponent {
                   <Divider dashed />
                   <div className={styles.tags}>
                     <div className={styles.tagsTitle}>标签</div>
-                    {currentUser.tags
-                      .concat(newTags)
-                      .map(item => <Tag key={item.key}>{item.label}</Tag>)}
+                    {currentUser.tags.concat(newTags).map(item => (
+                      <Tag key={item.key}>{item.label}</Tag>
+                    ))}
                     {inputVisible && (
                       <Input
                         ref={this.saveInputRef}

+ 27 - 25
src/utils/request.js

@@ -19,7 +19,7 @@ const codeMessage = {
   503: '服务不可用,服务器暂时过载或维护。',
   504: '网关超时。',
 };
-function checkStatus(response) {
+const checkStatus = response => {
   if (response.status >= 200 && response.status < 300) {
     return response;
   }
@@ -32,7 +32,26 @@ function checkStatus(response) {
   error.name = response.status;
   error.response = response;
   throw error;
-}
+};
+
+const cachedSave = (response, hashcode) => {
+  /**
+   * Clone a response data and store it in sessionStorage
+   * Does not support data other than json, Cache only json
+   */
+  let contentType = response.headers.get('Content-Type');
+  if (contentType && contentType.match(/application\/json/i)) {
+    // All data is saved as text
+    response
+      .clone()
+      .text()
+      .then(content => {
+        sessionStorage.setItem(hashcode, content);
+        sessionStorage.setItem(hashcode + ':timestamp', Date.now());
+      });
+  }
+  return response;
+};
 
 /**
  * Requests a URL, returning a promise.
@@ -47,7 +66,7 @@ export default function request(url, options = {}) {
    * Produce fingerprints based on url and parameters
    * Maybe url has the same parameters
    */
-  const fingerprint = url + options.body ? JSON.stringify(options.body) : '';
+  const fingerprint = url + (options.body ? JSON.stringify(options.body) : '');
   const hashcode = hash
     .sha256()
     .update(fingerprint)
@@ -77,8 +96,8 @@ export default function request(url, options = {}) {
       };
     }
   }
-  let cached = localStorage.getItem(hashcode);
-  let whenCached = localStorage.getItem(hashcode + ':timestamp');
+  let cached = sessionStorage.getItem(hashcode);
+  let whenCached = sessionStorage.getItem(hashcode + ':timestamp');
   const expirys = options.expirys || 60;
   if (cached !== null && whenCached !== null && expirys !== false) {
     let age = (Date.now() - whenCached) / 1000;
@@ -86,30 +105,13 @@ export default function request(url, options = {}) {
       let response = new Response(new Blob([cached]));
       return response.json();
     } else {
-      localStorage.removeItem(hashcode);
-      localStorage.removeItem(hashcode + ':timestamp');
+      sessionStorage.removeItem(hashcode);
+      sessionStorage.removeItem(hashcode + ':timestamp');
     }
   }
   return fetch(url, newOptions)
     .then(checkStatus)
-    .then(response => {
-      /**
-       * Clone a response data and store it in localStorage
-       * Does not support data other than json, Cache only json
-       */
-      let contentType = response.headers.get('Content-Type');
-      if (contentType && contentType.match(/application\/json/i)) {
-        // All data is saved as text
-        response
-          .clone()
-          .text()
-          .then(content => {
-            localStorage.setItem(hashcode, content);
-            localStorage.setItem(hashcode + ':timestamp', Date.now());
-          });
-      }
-      return response;
-    })
+    .then(cachedSave)
     .then(response => {
       // DELETE and 204 do not return data by default
       // using .json will report an error.

+ 0 - 28
stylelint.config.js

@@ -1,28 +0,0 @@
-export default {
-  extends: ['stylelint-config-standard', 'stylelint-config-prettier'],
-  rules: {
-    'selector-pseudo-class-no-unknown': null,
-    'shorthand-property-no-redundant-values': null,
-    'at-rule-empty-line-before': null,
-    'at-rule-name-space-after': null,
-    'comment-empty-line-before': null,
-    'declaration-bang-space-before': null,
-    'declaration-empty-line-before': null,
-    'function-comma-newline-after': null,
-    'function-name-case': null,
-    'function-parentheses-newline-inside': null,
-    'function-max-empty-lines': null,
-    'function-whitespace-after': null,
-    'number-leading-zero': null,
-    'number-no-trailing-zeros': null,
-    'rule-empty-line-before': null,
-    'selector-combinator-space-after': null,
-    'selector-descendant-combinator-no-non-space': null,
-    'selector-list-comma-newline-after': null,
-    'selector-pseudo-element-colon-notation': null,
-    'unit-no-unknown': null,
-    'no-descending-specificity': null,
-    'value-list-max-empty-lines': null,
-    'no-missing-end-of-source-newline': null,
-  },
-};