WIP: AI: #485 タグ広場 V1 /posts/changes の非 admin アクセス時 503 を修正
#489
@@ -110,8 +110,17 @@ export default forwardRef<HTMLAnchorElement, Props> (({
|
||||
setOverlay (false)
|
||||
})
|
||||
|
||||
if (!(ok) && cancelOnError)
|
||||
if (!ok)
|
||||
{
|
||||
if (!cancelOnError)
|
||||
{
|
||||
if (replace)
|
||||
location.replace (path)
|
||||
else
|
||||
location.assign (path)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
navigate (path, { replace, ...(state && { state }) })
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { QueryClient } from '@tanstack/react-query'
|
||||
import { fireEvent, screen, waitFor } from '@testing-library/react'
|
||||
import { Route, Routes, useLocation } from 'react-router-dom'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import PrefetchLink from '@/components/PrefetchLink'
|
||||
import * as prefetchers from '@/lib/prefetchers'
|
||||
@@ -23,6 +23,10 @@ const Location = () => {
|
||||
|
||||
|
||||
describe ('Help prefetch integration', () => {
|
||||
afterEach (() => {
|
||||
vi.unstubAllGlobals ()
|
||||
})
|
||||
|
||||
beforeEach (() => {
|
||||
helpFixture.reset ()
|
||||
settings.linkPreload = 'intent'
|
||||
@@ -115,12 +119,38 @@ describe ('Help prefetch integration', () => {
|
||||
expect (run).toHaveBeenCalledTimes (mode === 'intent' ? 3 : 1)
|
||||
})
|
||||
|
||||
it ('continues navigation after prefetch failure', async () => {
|
||||
it.each ([false, true]) ('uses document navigation after failure with replace=%s',
|
||||
async replacement => {
|
||||
const assign = vi.fn ()
|
||||
const replace = vi.fn ()
|
||||
vi.stubGlobal ('location', { origin: window.location.origin, assign, replace })
|
||||
vi.spyOn (console, 'error').mockImplementation (() => {})
|
||||
vi.spyOn (prefetchers, 'prefetchForURL').mockRejectedValue (new Error ('offline'))
|
||||
renderWithProviders (<><Location/><PrefetchLink to="/help/guide">Help</PrefetchLink></>)
|
||||
renderWithProviders (<>
|
||||
<Location/>
|
||||
<PrefetchLink to="/help/guide" replace={replacement}>Help</PrefetchLink>
|
||||
</>)
|
||||
fireEvent.click (screen.getByRole ('link', { name: 'Help' }))
|
||||
await waitFor (() => expect (screen.getByTestId ('location'))
|
||||
.toHaveTextContent ('/help/guide'))
|
||||
await waitFor (() => expect (replacement ? replace : assign)
|
||||
.toHaveBeenCalledWith ('/ja/help/guide'))
|
||||
expect (replacement ? assign : replace).not.toHaveBeenCalled ()
|
||||
expect (screen.getByTestId ('location')).toHaveTextContent ('/')
|
||||
})
|
||||
|
||||
it ('keeps the current page when cancelOnError is requested', async () => {
|
||||
const assign = vi.fn ()
|
||||
const replace = vi.fn ()
|
||||
vi.stubGlobal ('location', { origin: window.location.origin, assign, replace })
|
||||
vi.spyOn (console, 'error').mockImplementation (() => {})
|
||||
vi.spyOn (prefetchers, 'prefetchForURL').mockRejectedValue (new Error ('offline'))
|
||||
renderWithProviders (<>
|
||||
<Location/>
|
||||
<PrefetchLink to="/help/guide" cancelOnError>Help</PrefetchLink>
|
||||
</>)
|
||||
fireEvent.click (screen.getByRole ('link', { name: 'Help' }))
|
||||
await waitFor (() => expect (console.error).toHaveBeenCalled ())
|
||||
expect (assign).not.toHaveBeenCalled ()
|
||||
expect (replace).not.toHaveBeenCalled ()
|
||||
expect (screen.getByTestId ('location')).toHaveTextContent ('/')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -116,6 +116,17 @@ describe ('prefetchForURL', () => {
|
||||
.toEqual ({ versions: [], count: 0 })
|
||||
})
|
||||
|
||||
it ('propagates history prefetch failure to the navigation caller', async () => {
|
||||
const client = qc ()
|
||||
const error = new Error ('unavailable')
|
||||
postsApi.fetchPostChanges.mockRejectedValueOnce (error)
|
||||
|
||||
await expect (prefetchForURL (client, '/ja/posts/changes?id=12'))
|
||||
.rejects.toBe (error)
|
||||
expect (client.getQueryData (postsKeys.changes (
|
||||
{ post: '12', page: 1, limit: 20 }, 'ja'))).toBeUndefined ()
|
||||
})
|
||||
|
||||
it ('preserves an external tag filter when prefetching one post history', async () => {
|
||||
const client = qc ()
|
||||
const params = { post: '12', externalTag: '7', page: 1, limit: 20 }
|
||||
|
||||
@@ -213,7 +213,7 @@ const prefetchPostChanges: Prefetcher = async (qc, url, locale) => {
|
||||
queryFn: () => fetchTag (tag, locale) })
|
||||
}
|
||||
|
||||
await qc.prefetchQuery ({
|
||||
await qc.fetchQuery ({
|
||||
queryKey: postsKeys.changes ({ ...(id && { post: id }),
|
||||
...(tag && { tag }),
|
||||
...(externalTag && { externalTag }),
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
import { QueryClient, dehydrate } from '@tanstack/react-query'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { localesKeys, postsKeys, sessionKeys } from '@/lib/queryKeys'
|
||||
|
||||
const bootstrap = vi.hoisted (() => ({
|
||||
hydrateRoot: vi.fn (), createRoot: vi.fn (), session: vi.fn (),
|
||||
reload: vi.fn (), preload: vi.fn () }))
|
||||
|
||||
vi.mock ('react-dom/client', () => ({
|
||||
hydrateRoot: bootstrap.hydrateRoot, createRoot: bootstrap.createRoot }))
|
||||
vi.mock ('@/App', () => ({ default: () => null,
|
||||
preloadPageRoute: bootstrap.preload, resolvePageRoute: () => null }))
|
||||
vi.mock ('@/lib/users', () => ({ initialiseUserSession: bootstrap.session }))
|
||||
vi.mock ('@/lib/i18n', () => ({ createUII18n: () => ({ }) }))
|
||||
vi.mock ('@/lib/locales', () => ({ currentViewingLocale: () => 'ja' }))
|
||||
vi.mock ('@/lib/settings', () => ({
|
||||
applyClientAnimationMode: vi.fn (), getClientAnimationMode: () => 'normal' }))
|
||||
|
||||
const user = { id: 1, role: 'guest', name: 'Guest', inheritanceCode: 'code' }
|
||||
const historyKey = postsKeys.changes ({ page: 1, limit: 20 }, 'ja')
|
||||
const history = { versions: [], count: 0 }
|
||||
|
||||
|
||||
const prepareDocument = (authority: { id: number; role: string } | null) => {
|
||||
const client = new QueryClient ()
|
||||
client.setQueryData (localesKeys.index, { locales: [{ code: 'ja' }] })
|
||||
client.setQueryData (historyKey, history)
|
||||
if (authority != null)
|
||||
client.setQueryData (sessionKeys.current, authority)
|
||||
document.body.innerHTML = '<div id="root">SSR history</div>'
|
||||
+ '<script id="ssr-state" type="application/json"></script>'
|
||||
document.getElementById ('ssr-state')!.textContent = JSON.stringify ({
|
||||
state: dehydrate (client), status: 200 })
|
||||
}
|
||||
|
||||
|
||||
describe ('SSR session reconciliation before hydration', () => {
|
||||
beforeEach (() => {
|
||||
vi.resetModules ()
|
||||
vi.clearAllMocks ()
|
||||
sessionStorage.clear ()
|
||||
bootstrap.session.mockReset ()
|
||||
bootstrap.session.mockResolvedValue (user)
|
||||
bootstrap.preload.mockResolvedValue (undefined)
|
||||
vi.stubGlobal ('window', {
|
||||
location: { href: 'http://localhost:3000/ja/posts/changes',
|
||||
reload: bootstrap.reload },
|
||||
addEventListener: vi.fn (), removeEventListener: vi.fn () })
|
||||
})
|
||||
|
||||
afterEach (() => {
|
||||
vi.unstubAllGlobals ()
|
||||
})
|
||||
|
||||
it.each (['guest', 'member', 'admin']) (
|
||||
'retains SSR history when the confirmed %s identity matches', async role => {
|
||||
prepareDocument ({ id: user.id, role })
|
||||
bootstrap.session.mockResolvedValue ({ ...user, role })
|
||||
await import ('@/main')
|
||||
await vi.waitFor (() => expect (bootstrap.hydrateRoot).toHaveBeenCalledOnce ())
|
||||
const client = bootstrap.hydrateRoot.mock.calls[0][1]
|
||||
.props.children.props.client as QueryClient
|
||||
expect (client.getQueryData (historyKey)).toEqual (history)
|
||||
expect (client.getQueryData (sessionKeys.current)).toEqual ({ ...user, role })
|
||||
expect (bootstrap.reload).not.toHaveBeenCalled ()
|
||||
expect (bootstrap.createRoot).not.toHaveBeenCalled ()
|
||||
})
|
||||
|
||||
it.each ([null, { id: 2, role: 'guest' }, { id: 1, role: 'admin' }]) (
|
||||
'requests fresh SSR for a different identity: %s', async authority => {
|
||||
prepareDocument (authority)
|
||||
await import ('@/main')
|
||||
await vi.waitFor (() => expect (bootstrap.reload).toHaveBeenCalledOnce ())
|
||||
expect (bootstrap.hydrateRoot).not.toHaveBeenCalled ()
|
||||
expect (bootstrap.createRoot).not.toHaveBeenCalled ()
|
||||
expect (document.getElementById ('root')!.textContent).toBe ('SSR history')
|
||||
})
|
||||
|
||||
it ('waits for session confirmation before hydrating', async () => {
|
||||
prepareDocument (user)
|
||||
let confirmSession!: (value: typeof user) => void
|
||||
const session = new Promise<typeof user> (resolve => { confirmSession = resolve })
|
||||
bootstrap.session.mockReturnValue (session)
|
||||
await import ('@/main')
|
||||
expect (bootstrap.hydrateRoot).not.toHaveBeenCalled ()
|
||||
confirmSession (user)
|
||||
await vi.waitFor (() => expect (bootstrap.hydrateRoot).toHaveBeenCalledOnce ())
|
||||
expect (bootstrap.reload).not.toHaveBeenCalled ()
|
||||
})
|
||||
|
||||
it ('does not loop when the browser cannot retain the confirmed session', async () => {
|
||||
prepareDocument (null)
|
||||
await import ('@/main')
|
||||
await vi.waitFor (() => expect (bootstrap.reload).toHaveBeenCalledOnce ())
|
||||
vi.resetModules ()
|
||||
vi.spyOn (console, 'error').mockImplementation (() => {})
|
||||
await import ('@/main')
|
||||
await vi.waitFor (() => expect (console.error).toHaveBeenCalled ())
|
||||
expect (bootstrap.reload).toHaveBeenCalledOnce ()
|
||||
expect (bootstrap.hydrateRoot).not.toHaveBeenCalled ()
|
||||
})
|
||||
})
|
||||
+16
-1
@@ -7,11 +7,14 @@ import '@/index.css'
|
||||
import App, { preloadPageRoute, resolvePageRoute } from '@/App'
|
||||
import { createUII18n } from '@/lib/i18n'
|
||||
import { currentViewingLocale } from '@/lib/locales'
|
||||
import { localesKeys } from '@/lib/queryKeys'
|
||||
import { localesKeys, sessionKeys } from '@/lib/queryKeys'
|
||||
import { applyClientAnimationMode, getClientAnimationMode } from '@/lib/settings'
|
||||
import { initialiseUserSession } from '@/lib/users'
|
||||
|
||||
import type { DehydratedState } from '@tanstack/react-query'
|
||||
|
||||
import type { User } from '@/types'
|
||||
|
||||
const helmetContext = { }
|
||||
|
||||
const client = new QueryClient ({
|
||||
@@ -70,6 +73,18 @@ const start = () => {
|
||||
}
|
||||
if (client.getQueryData (localesKeys.index) == null)
|
||||
throw new Error ('Missing SSR locales')
|
||||
const authority = client.getQueryData<User> (sessionKeys.current)
|
||||
const user = await initialiseUserSession ()
|
||||
if (user.id !== authority?.id || user.role !== authority?.role)
|
||||
{
|
||||
// Re-render under the confirmed Cookie before hydrating another identity's data.
|
||||
if (sessionStorage.getItem (storageKey) === identity)
|
||||
throw new Error ('SSR session identity did not converge')
|
||||
sessionStorage.setItem (storageKey, identity)
|
||||
window.location.reload ()
|
||||
return
|
||||
}
|
||||
client.setQueryData (sessionKeys.current, user)
|
||||
const url = new URL (window.location.href)
|
||||
const match = resolvePageRoute (url.pathname)
|
||||
const prepare = match?.route.handle?.prepare
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする