te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to clear Local Storage from client whenever i publish new code - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to clear Local Storage from client whenever i publish new code - Stack Overflow

programmeradmin4浏览0评论

I have build my application on Angular 4 and I am using local storage to store user session. What I want is whenever I publish my application, user local Storage should get reset so that data before release should not create any problem.

How do i do that?

I have build my application on Angular 4 and I am using local storage to store user session. What I want is whenever I publish my application, user local Storage should get reset so that data before release should not create any problem.

How do i do that?

Share Improve this question asked Feb 15, 2018 at 6:55 Praveen RawatPraveen Rawat 6581 gold badge12 silver badges28 bronze badges 7
  • Possible duplicate of Clearing localStorage in javascript? – VTodorov Commented Feb 15, 2018 at 6:57
  • in jQuery - stackoverflow./questions/10710674/… – NnN Commented Feb 15, 2018 at 6:58
  • @VTodorov I think, you didn't understand what he wants. – Ma Kobi Commented Feb 15, 2018 at 6:59
  • 1 @NnN jQuery has nothing to do with localStorage and none of the answers in the link has any reference to jQuery O.o – Andreas Commented Feb 15, 2018 at 6:59
  • you have to write the script with version update whenever new version is less than current clear local storage but he will make the user to log out, 1 st time, you cant do this on publishing code, it's client side so it will be updated when user open site – sunil Commented Feb 15, 2018 at 7:02
 |  Show 2 more ments

5 Answers 5

Reset to default 7

using versioning, keep an extra key at client side which will be having the last version. If version matches then it means no new code has been published, if version doesn't match which tells that new code has been passed, then just have a utility which will clear the local-storage.

Use app versioning.

Disclaimer: THIS SOLUTION IS NOT FOR WINDOWS USER!

The best practice is to automate the versioning so that you don't have to manually set the app version again and again.

The idea is to

  1. Get the current git SHA, if no older SHA found, store it in the localstorage.
  2. If older SHA found, pare the SHAs, if not equal, clear the storage, set the new SHA in localstorage.

This will give you the current short version of the current mit SHA.

git rev-parse --short HEAD

In your package.json

"scripts": {     
      "build": "REACT_APP_CURRENT_GIT_SHA=`git rev-parse --short HEAD` react-scripts build",     
      "start": "REACT_APP_CURRENT_GIT_SHA=`git rev-parse --short HEAD` react-scripts start" 
 }

in the code, we get this variable through

process.env.REACT_APP_CURRENT_GIT_SHA

Code:

const APP_VERSION = process.env.REACT_APP_CURRENT_GIT_SHA;

if (typeof localStorage.APP_VERSION === 'undefined' || localStorage.APP_VERSION === null) {
    localStorage.setItem('APP_VERSION', APP_VERSION);
}

if (localStorage.APP_VERSION != APP_VERSION) {
    localStorage.clear();
}`

You could store a version string in your application and in the local storage. On your app startup you check if the string in localStorage matches the string in your application. If not, clear the local storage.

config/app.php

return [
        'version'         => env('APP_VERSION', "1.0.0"),

In ***.blade.php

<script>
    var version_app = "{{ config('app.version') }}"
</script>

Add to **.js

if(typeof localStorage.version_app === 'undefined' || localStorage.version_app === null) {
        localStorage.setItem('version_app', version_app);
    }
if(localStorage.version_app != version_app){
            localStorage.clear();
    }

When you change the version, the script automatically deletes the localStorage data

MULTI-PLATFORM USE CASES and OPTIONAL STORAGE CLEARING (following Paras answer).

Since the script shell for npm is CMD.exe by default and since CMD.exe does not support mand substitution.

By mand substitution, I mean this syntax which takes the return value for a mand and uses it as an argument for another

REACT_APP_CURRENT_GIT_SHA=`git rev-parse --short HEAD`

The workaround that worked for me was to change the script shell for npm to bash by running this in powershell

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"

Note that you need to have git installed

And then using cross-env so I can use a single syntax for both linux and windows platforms

My package.json script ended up looking like this

{
    "start": "cross-env REACT_APP_CURRENT_GIT_SHA=$(git rev-parse --short HEAD) REACT_APP_CLEAR_STORAGE=${npm_config_clear_storage} react-scripts start",
    "build": "cross-env REACT_APP_CURRENT_GIT_SHA=$(git rev-parse --short HEAD) REACT_APP_CLEAR_STORAGE=${npm_config_clear_storage} react-scripts build"
}

I added REACT_APP_CLEAR_STORAGE=${npm_config_clear_storage} so I could manually specify when running the scripts if I wanted the localStorage to get cleared. (If you do not want this, do not add it)

Note that the extra argument works when the version (REACT_APP_CURRENT_GIT_SHA) changes only

My code now looks like this

const APP_VERSION = process.env.REACT_APP_CURRENT_GIT_SHA;
const CLEAR_STORAGE = !!process.env.REACT_APP_CLEAR_STORAGE;

if (localStorage.APP_VERSION != APP_VERSION) {
    if (CLEAR_STORAGE) {
        localStorage.clear();
    }
    localStorage.setItem("APP_VERSION", APP_VERSION);
}

And to run build and clear localStorage, I do npm run build --clear_storage

Meanwhile, to run build without clearing localStorage, I just do npm run build

发布评论

评论列表(0)

  1. 暂无评论