Unverified Commit 9d4239a1 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[segment explorer] handle display loading (#80698)

parent 04a900f7
...@@ -256,4 +256,8 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css` ...@@ -256,4 +256,8 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css`
background-color: var(--color-amber-300); background-color: var(--color-amber-300);
color: var(--color-amber-900); color: var(--color-amber-900);
} }
.segment-explorer-file-label--loading {
background-color: var(--color-green-300);
color: var(--color-green-900);
}
` `
...@@ -593,8 +593,20 @@ async function createComponentTreeInternal({ ...@@ -593,8 +593,20 @@ async function createComponentTreeInternal({
parallelRouteCacheNodeSeedData[parallelRouteKey] = flightData parallelRouteCacheNodeSeedData[parallelRouteKey] = flightData
} }
const loadingData: LoadingModuleData = Loading let loadingElement = Loading ? <Loading key="l" /> : null
? [<Loading key="l" />, loadingStyles, loadingScripts] if (isSegmentViewEnabled && loadingElement) {
const loadingFilePath = getConventionPathByType(tree, dir, 'loading')
if (loadingFilePath) {
loadingElement = (
<SegmentViewNode type="loading" pagePath={loadingFilePath}>
{loadingElement}
</SegmentViewNode>
)
}
}
const loadingData: LoadingModuleData = loadingElement
? [loadingElement, loadingStyles, loadingScripts]
: null : null
// When the segment does not have a layout or page we still have to add the layout router to ensure the path holds the loading component // When the segment does not have a layout or page we still have to add the layout router to ensure the path holds the loading component
......
'use client'
import { Fragment } from 'react'
import { useSearchParams } from 'next/navigation'
export default function SearchLayout({
children,
}: {
children: React.ReactNode
}) {
let searchParams = useSearchParams()
return <Fragment key={searchParams?.get('q')}>{children}</Fragment>
}
export default function Loading() {
return <p id="loading">Loading...</p>
}
import { Search } from './search'
type AnySearchParams = { [key: string]: string | Array<string> | undefined }
export default async function Page({
searchParams,
}: {
searchParams: Promise<AnySearchParams>
}) {
const search = await searchParams
if (Object.keys(search).length > 0) {
await new Promise((resolve) => setTimeout(resolve, 30 * 1000))
}
return (
<main id="page-content">
<Search />
<p id="search-value">Search Value: {search.q ?? 'None'}</p>
</main>
)
}
'use client'
import { useRouter } from 'next/navigation'
export function Search() {
let router = useRouter()
function search(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault()
let input = event.currentTarget.q.value
let params = new URLSearchParams([['q', input]])
router.push(`/search?${params}`)
}
return (
<form onSubmit={search}>
<input placeholder="Search..." name="q" className="border" />
<button>Submit</button>
</form>
)
}
...@@ -160,4 +160,23 @@ describe('segment-explorer', () => { ...@@ -160,4 +160,23 @@ describe('segment-explorer', () => {
unauthorized.tsx" unauthorized.tsx"
`) `)
}) })
it('should show the loading boundary when it is present', async () => {
const browser = await next.browser('/search')
const input = await browser.elementByCss('input[name="q"]')
await input.fill('abc')
await browser.elementByCss('button').click() // submit the form
await retry(async () => {
expect(await browser.elementByCss('#loading').text()).toBe('Loading...')
})
expect(await getSegmentExplorerContent(browser)).toMatchInlineSnapshot(`
"app/
layout.tsx
search/
layout.tsx
loading.tsx"
`)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment