After the user logs out, old data still remains in the browser, how to require the response header to be cleared.
After the user logs out, the browser still retains cache and storage. How do you require the clearing of headers, it relies on a clearing signal emitted by the server, not on the script in the page itself deleting. This signal is the Clear-Site-Data response header: the server includes it in the response of the logout interface, which is equivalent to giving the client the instruction of 'which types of browsing data to clear'. The browser then executes local cleaning item by item. It can only express two things: which types of data to clear and to what extent, the rest depends on whether the client cooperates.
Why do you need to clear the login page actively?
The login status is issued by the server: credentials are issued when the session is established, and the credentials are invalidated when the session is logged out. The browser records another set of accounts, page, script, and interface response will enter the cache, and business code may also write intermediate results into the local storage. These contents are not bound to the session and will not disappear automatically when the session expires.
Therefore, a misalignment occurs: the user has clicked logout, and the server has turned the page, with the old data still lying on the device. When the next person opens the site on the same machine and the same browser, even if they cannot get a new session, they may still see the content of the previous page from the residue. Actively clearing it is to align both sides.
Within the station, the login state is carried by JWT, the server-side logout and session expiration are on one side, and the browser's local cache and storage need to be cleared with this response header. As for these two points, no other expectations should be derived.
Which types of local data can be cleared
Instructions are given item by item, multiple items can be listed in one response, and the client only handles the listed items. Common items and the things they manage are as follows.
| Items to clear | General scope of action | Why is it needed when logging out |
|---|---|---|
| cache | The cache content saved by the browser for this site | Collect the residual page and interface response with the login context |
| cookies | Credentials under the entire registration domain, including all subdomains | Clear the login credentials scattered across multiple subdomains in one go |
| storage | Local storage corresponding to the source of the response | The intermediate data written into local storage by the script ends with it |
| executionContexts | The current relevant execution context | End the running script environment all at once, don't let old code continue to hold data |
If the list is too short, the end will not be clean, and if the list is too long, it will hurt the things the user intends to keep
Why only effective in encrypted connections
This ability is only available in a secure context, and when deployed, it means that the response must go through an encrypted connection, that is, HTTPS. If this header is added to a plaintext connection, the browser will not comply, and no cleaning action will occur.
The reason is not difficult to understand: it requires deleting local data, which has a significant impact, and browsers do not accept signals that may be tampered with or forged by intermediate links. The conclusion is to first confirm that the entire site uses an encrypted entry, and then discuss whether this header is effective; if it was added in the test but did not work, first check whether the access path is an encrypted connection.
Whether the clearing scope is to the source or to the registered domain
Different items have different landing points, which is the most confusing part.
The impact of cookies is the entire registration domain, including its subdomains. When multiple subsystems under the main domain share the same set of login credentials, a single command can cover them all, without the need to process each domain individually.
Storage clears the local storage corresponding to the response source. The source is determined by the protocol, host, and port. Changing the host or protocol is another source, which will not be cleaned together.
The difference here has real significance: To collect clean credentials, it relies on the scope of the registration domain; To accurately store locally, it needs the response with this prefix to appear at the actual source of data writing.
Why is it more worth adding this layer on public devices?
The residue on a private device mainly affects the user themselves. Public devices are different: front desk tablets, shared terminals in workshops, and machines used by multiple people in rotation. After the previous person logs out, the browser is not under their control, and the next person takes over the same local data directly. In such scenarios, the server's unilateral cancellation of the session does not clean up the traces of the previous person, and the cleaning at the client level fills the gap.
Also, be aware of its unreliability: the server can only send signals, whether it is cleared and cleared clean depends on the client, browsers that do not cooperate can completely ignore this instruction, and the degree of support for different items by different browsers is also different. Since September 2023, this capability has been generally available in browsers, but general availability does not mean that the performance of each item is consistent.
Therefore, it cannot replace the server-side session revocation. Invalidating credentials, revocation records, and cleaning up the state on the server side still need to be done on the server side, this level is the closing, not the itself of the security boundary.
Common questions
Ask: What are the consequences if the logout interface only invalidates the session without returning this header?Answer: The server side is clean, but the cache and local storage accumulated during the browser's visit are still left on the device. When someone else uses it, these remnants may be found by the next person.
Ask: Can you only issue it at the moment of logout?Answer: Yes, it performs a deletion action, the cost is borne by the client; placed on the interface for confirming the end of the session, the semantics are clear, and it will not clear the data the user is using in normal browsing.
Ask: If I clear the cookies, does it mean all local data is cleared?Answer: Not equal. Cookies cover the credentials under the registration domain, local storage belongs to another item, cache is also calculated separately, and it is effective to list them separately.