feat: 修复缓存
This commit is contained in:
parent
a30ad9dd89
commit
023398cbea
|
|
@ -101,7 +101,10 @@ addEventListener('fetch', function (event) {
|
||||||
|
|
||||||
// Opening the DevTools triggers the "only-if-cached" request
|
// Opening the DevTools triggers the "only-if-cached" request
|
||||||
// that cannot be handled by the worker. Bypass such requests.
|
// that cannot be handled by the worker. Bypass such requests.
|
||||||
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
|
if (
|
||||||
|
event.request.cache === 'only-if-cached' &&
|
||||||
|
event.request.mode !== 'same-origin'
|
||||||
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -153,7 +156,7 @@ async function handleRequest(event, requestId) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
responseClone.body ? [serializedRequest.body, responseClone.body] : []
|
responseClone.body ? [serializedRequest.body, responseClone.body] : [],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -217,7 +220,9 @@ async function getResponse(event, client, requestId) {
|
||||||
const acceptHeader = headers.get('accept')
|
const acceptHeader = headers.get('accept')
|
||||||
if (acceptHeader) {
|
if (acceptHeader) {
|
||||||
const values = acceptHeader.split(',').map((value) => value.trim())
|
const values = acceptHeader.split(',').map((value) => value.trim())
|
||||||
const filteredValues = values.filter((value) => value !== 'msw/passthrough')
|
const filteredValues = values.filter(
|
||||||
|
(value) => value !== 'msw/passthrough',
|
||||||
|
)
|
||||||
|
|
||||||
if (filteredValues.length > 0) {
|
if (filteredValues.length > 0) {
|
||||||
headers.set('accept', filteredValues.join(', '))
|
headers.set('accept', filteredValues.join(', '))
|
||||||
|
|
@ -253,7 +258,7 @@ async function getResponse(event, client, requestId) {
|
||||||
...serializedRequest,
|
...serializedRequest,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
[serializedRequest.body]
|
[serializedRequest.body],
|
||||||
)
|
)
|
||||||
|
|
||||||
switch (clientMessage.type) {
|
switch (clientMessage.type) {
|
||||||
|
|
@ -287,7 +292,10 @@ function sendToClient(client, message, transferrables = []) {
|
||||||
resolve(event.data)
|
resolve(event.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
client.postMessage(message, [channel.port2, ...transferrables.filter(Boolean)])
|
client.postMessage(message, [
|
||||||
|
channel.port2,
|
||||||
|
...transferrables.filter(Boolean),
|
||||||
|
])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,12 +52,16 @@ export function useLogout() {
|
||||||
toast.success('Log out successful!');
|
toast.success('Log out successful!');
|
||||||
// 清除所有查询缓存
|
// 清除所有查询缓存
|
||||||
queryClient.clear();
|
queryClient.clear();
|
||||||
|
// 清除 Next.js 路由缓存,确保中间件能读取到最新的 cookie 状态
|
||||||
|
router.refresh();
|
||||||
router.push('/');
|
router.push('/');
|
||||||
},
|
},
|
||||||
onError: (error: ApiError) => {
|
onError: (error: ApiError) => {
|
||||||
// 即使登出接口失败,也要清除本地token
|
// 即使登出接口失败,也要清除本地token
|
||||||
tokenManager.removeToken();
|
tokenManager.removeToken();
|
||||||
queryClient.clear();
|
queryClient.clear();
|
||||||
|
// 清除 Next.js 路由缓存,确保中间件能读取到最新的 cookie 状态
|
||||||
|
router.refresh();
|
||||||
// 跳转到登录页
|
// 跳转到登录页
|
||||||
router.push('/');
|
router.push('/');
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,12 @@ export const tokenManager = {
|
||||||
removeToken: (): void => {
|
removeToken: (): void => {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
console.log('remove token');
|
console.log('remove token');
|
||||||
Cookies.remove(TOKEN_COOKIE_NAME);
|
// 删除cookie时需要指定与设置时相同的选项
|
||||||
|
Cookies.remove(TOKEN_COOKIE_NAME, {
|
||||||
|
secure: process.env.NODE_ENV === 'production',
|
||||||
|
sameSite: 'lax',
|
||||||
|
path: '/',
|
||||||
|
});
|
||||||
// 同时清除可能存在的localStorage token
|
// 同时清除可能存在的localStorage token
|
||||||
window.localStorage.removeItem('token');
|
window.localStorage.removeItem('token');
|
||||||
// 注意:这里不清除设备ID
|
// 注意:这里不清除设备ID
|
||||||
|
|
@ -117,8 +122,13 @@ export const tokenManager = {
|
||||||
// 清除所有数据(包括设备ID)
|
// 清除所有数据(包括设备ID)
|
||||||
clearAll: (): void => {
|
clearAll: (): void => {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
Cookies.remove(TOKEN_COOKIE_NAME);
|
const cookieOptions = {
|
||||||
Cookies.remove(DEVICE_ID_COOKIE_NAME);
|
secure: process.env.NODE_ENV === 'production',
|
||||||
|
sameSite: 'lax' as const,
|
||||||
|
path: '/',
|
||||||
|
};
|
||||||
|
Cookies.remove(TOKEN_COOKIE_NAME, cookieOptions);
|
||||||
|
Cookies.remove(DEVICE_ID_COOKIE_NAME, cookieOptions);
|
||||||
window.localStorage.removeItem('token');
|
window.localStorage.removeItem('token');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue