Quest for Codev2.0.0
/
SIGN IN
next.js/test/e2e/app-dir/instant-validation-build/app/(default)/search-params/invalid-undeclared-search-param-caught/page.tsx
page.tsx42 lines1.1 KB
import { ensureThrows } from '../../../../ensure-error'

export const unstable_instant = { samples: [{ searchParams: { q: 'test' } }] }
export const unstable_prefetch = 'force-runtime'

export default async function Page({
  searchParams,
}: {
  searchParams: Promise<{ q?: string; undeclared?: string }>
}) {
  return (
    <main>
      <p>
        This page reads a searchParam that is not declared in the sample, so it
        should fail validation with an exhaustiveness error. It catches the
        error thrown by the searchParam access, but validation should still
        fail.
      </p>
      <SearchResult searchParams={searchParams} />
    </main>
  )
}

async function SearchResult({
  searchParams,
}: {
  searchParams: Promise<{ q?: string; undeclared?: string }>
}) {
  const sp = await searchParams

  try {
    ensureThrows(
      () => sp.undeclared,
      `Expected accessing an undeclared search param to throw`
    )
  } catch (err) {
    // We swallow the error. It should still be reported and fail the validation.
  }

  return <div id="search-result">query: {sp.q}</div>
}