Unverified Commit c90e5f05 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Handle redirects in new router (#40396)

Co-authored-by: 's avatarSebastian Markbåge <sebastian@calyptus.eu>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 0fb3284d
...@@ -29,6 +29,14 @@ import { ...@@ -29,6 +29,14 @@ import {
} from './hooks-client-context' } from './hooks-client-context'
import { useReducerWithReduxDevtools } from './use-reducer-with-devtools' import { useReducerWithReduxDevtools } from './use-reducer-with-devtools'
function urlToUrlWithoutFlightParameters(url: string): URL {
const urlWithoutFlightParameters = new URL(url, location.origin)
urlWithoutFlightParameters.searchParams.delete('__flight__')
urlWithoutFlightParameters.searchParams.delete('__flight_router_state_tree__')
urlWithoutFlightParameters.searchParams.delete('__flight_prefetch__')
return urlWithoutFlightParameters
}
/** /**
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side. * Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
*/ */
...@@ -36,7 +44,7 @@ export async function fetchServerResponse( ...@@ -36,7 +44,7 @@ export async function fetchServerResponse(
url: URL, url: URL,
flightRouterState: FlightRouterState, flightRouterState: FlightRouterState,
prefetch?: true prefetch?: true
): Promise<[FlightData: FlightData]> { ): Promise<[FlightData: FlightData, canonicalUrlOverride: URL | undefined]> {
const flightUrl = new URL(url) const flightUrl = new URL(url)
const searchParams = flightUrl.searchParams const searchParams = flightUrl.searchParams
// Enable flight response // Enable flight response
...@@ -51,9 +59,13 @@ export async function fetchServerResponse( ...@@ -51,9 +59,13 @@ export async function fetchServerResponse(
} }
const res = await fetch(flightUrl.toString()) const res = await fetch(flightUrl.toString())
const canonicalUrl = res.redirected
? urlToUrlWithoutFlightParameters(res.url)
: undefined
// Handle the `fetch` readable stream that can be unwrapped by `React.use`. // Handle the `fetch` readable stream that can be unwrapped by `React.use`.
const flightData: FlightData = await createFromFetch(Promise.resolve(res)) const flightData: FlightData = await createFromFetch(Promise.resolve(res))
return [flightData] return [flightData, canonicalUrl]
} }
/** /**
...@@ -140,11 +152,16 @@ export default function AppRouter({ ...@@ -140,11 +152,16 @@ export default function AppRouter({
* Server response that only patches the cache and tree. * Server response that only patches the cache and tree.
*/ */
const changeByServerResponse = useCallback( const changeByServerResponse = useCallback(
(previousTree: FlightRouterState, flightData: FlightData) => { (
previousTree: FlightRouterState,
flightData: FlightData,
overrideCanonicalUrl: URL | undefined
) => {
dispatch({ dispatch({
type: ACTION_SERVER_PATCH, type: ACTION_SERVER_PATCH,
flightData, flightData,
previousTree, previousTree,
overrideCanonicalUrl,
cache: { cache: {
data: null, data: null,
subTreeData: null, subTreeData: null,
...@@ -192,19 +209,18 @@ export default function AppRouter({ ...@@ -192,19 +209,18 @@ export default function AppRouter({
try { try {
// TODO-APP: handle case where history.state is not the new router history entry // TODO-APP: handle case where history.state is not the new router history entry
const r = fetchServerResponse( const serverResponse = await fetchServerResponse(
url, url,
// initialTree is used when history.state.tree is missing because the history state is set in `useEffect` below, it being missing means this is the hydration case. // initialTree is used when history.state.tree is missing because the history state is set in `useEffect` below, it being missing means this is the hydration case.
window.history.state?.tree || initialTree, window.history.state?.tree || initialTree,
true true
) )
const [flightData] = await r
// @ts-ignore startTransition exists // @ts-ignore startTransition exists
React.startTransition(() => { React.startTransition(() => {
dispatch({ dispatch({
type: ACTION_PREFETCH, type: ACTION_PREFETCH,
url, url,
flightData, serverResponse,
}) })
}) })
} catch (err) { } catch (err) {
......
/**
* Used to cache in createInfinitePromise
*/
let infinitePromise: Promise<void>
/**
* Create a Promise that does not resolve. This is used to suspend when data is not available yet.
*/
export function createInfinitePromise() {
if (!infinitePromise) {
// Only create the Promise once
infinitePromise = new Promise((/* resolve */) => {
// This is used to debug when the rendering is never updated.
// setTimeout(() => {
// infinitePromise = new Error('Infinite promise')
// resolve()
// }, 5000)
})
}
return infinitePromise
}
...@@ -23,6 +23,8 @@ import { ...@@ -23,6 +23,8 @@ import {
TemplateContext, TemplateContext,
} from '../../shared/lib/app-router-context' } from '../../shared/lib/app-router-context'
import { fetchServerResponse } from './app-router.client' import { fetchServerResponse } from './app-router.client'
import { createInfinitePromise } from './infinite-promise'
// import { matchSegment } from './match-segments' // import { matchSegment } from './match-segments'
/** /**
...@@ -95,29 +97,6 @@ function walkAddRefetch( ...@@ -95,29 +97,6 @@ function walkAddRefetch(
return treeToRecreate return treeToRecreate
} }
/**
* Used to cache in createInfinitePromise
*/
let infinitePromise: Promise<void> | Error
/**
* Create a Promise that does not resolve. This is used to suspend when data is not available yet.
*/
function createInfinitePromise() {
if (!infinitePromise) {
// Only create the Promise once
infinitePromise = new Promise((/* resolve */) => {
// This is used to debug when the rendering is never updated.
// setTimeout(() => {
// infinitePromise = new Error('Infinite promise')
// resolve()
// }, 5000)
})
}
return infinitePromise
}
/** /**
* Check if the top of the HTMLElement is in the viewport. * Check if the top of the HTMLElement is in the viewport.
*/ */
...@@ -237,7 +216,7 @@ export function InnerLayoutRouter({ ...@@ -237,7 +216,7 @@ export function InnerLayoutRouter({
* Flight response data * Flight response data
*/ */
// When the data has not resolved yet `use` will suspend here. // When the data has not resolved yet `use` will suspend here.
const [flightData] = use(childNode.data) const [flightData, overrideCanonicalUrl] = use(childNode.data)
// Handle case when navigating to page in `pages` from `app` // Handle case when navigating to page in `pages` from `app`
if (typeof flightData === 'string') { if (typeof flightData === 'string') {
...@@ -245,52 +224,25 @@ export function InnerLayoutRouter({ ...@@ -245,52 +224,25 @@ export function InnerLayoutRouter({
return null return null
} }
/** // segmentPath from the server does not match the layout's segmentPath
* If the fast path was triggered. childNode.data = null
* The fast path is when the returned Flight data path matches the layout segment path, then we can write the data to the cache in render instead of dispatching an action.
*/ // setTimeout is used to start a new transition during render, this is an intentional hack around React.
let fastPath: boolean = false setTimeout(() => {
// @ts-ignore startTransition exists
// If there are multiple patches returned in the Flight data we need to dispatch to ensure a single render. React.startTransition(() => {
// if (flightData.length === 1) { // TODO-APP: handle redirect
// const flightDataPath = flightData[0] changeByServerResponse(fullTree, flightData, overrideCanonicalUrl)
// if (segmentPathMatches(flightDataPath, segmentPath)) {
// // Ensure data is set to null as subTreeData will be set in the cache now.
// childNode.data = null
// // Last item is the subtreeData
// // TODO-APP: routerTreePatch needs to be applied to the tree, handle it in render?
// const [, /* routerTreePatch */ subTreeData] = flightDataPath.slice(-2)
// // Add subTreeData into the cache
// childNode.subTreeData = subTreeData
// // This field is required for new items
// childNode.parallelRoutes = new Map()
// fastPath = true
// }
// }
// When the fast path is not used a new action is dispatched to update the tree and cache.
if (!fastPath) {
// segmentPath from the server does not match the layout's segmentPath
childNode.data = null
// setTimeout is used to start a new transition during render, this is an intentional hack around React.
setTimeout(() => {
// @ts-ignore startTransition exists
React.startTransition(() => {
// TODO-APP: handle redirect
changeByServerResponse(fullTree, flightData)
})
}) })
// Suspend infinitely as `changeByServerResponse` will cause a different part of the tree to be rendered. })
throw createInfinitePromise() // Suspend infinitely as `changeByServerResponse` will cause a different part of the tree to be rendered.
} use(createInfinitePromise())
} }
// If cache node has no subTreeData and no data request we have to infinitely suspend as the data will likely flow in from another place. // If cache node has no subTreeData and no data request we have to infinitely suspend as the data will likely flow in from another place.
// TODO-APP: double check users can't return null in a component that will kick in here. // TODO-APP: double check users can't return null in a component that will kick in here.
if (!childNode.subTreeData) { if (!childNode.subTreeData) {
throw createInfinitePromise() use(createInfinitePromise())
} }
const subtree = ( const subtree = (
......
import React, { experimental_use as use } from 'react'
import { AppRouterContext } from '../../shared/lib/app-router-context'
import { createInfinitePromise } from './infinite-promise'
export function redirect(url: string) {
const router = use(AppRouterContext)
setTimeout(() => {
// @ts-ignore startTransition exists
React.startTransition(() => {
router.replace(url, {})
})
})
// setTimeout is used to start a new transition during render, this is an intentional hack around React.
use(createInfinitePromise())
}
export const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT'
export function redirect(url: string) {
// eslint-disable-next-line no-throw-literal
throw {
url,
code: REDIRECT_ERROR_CODE,
}
}
...@@ -11,6 +11,10 @@ import { experimental_use as use } from 'react' ...@@ -11,6 +11,10 @@ import { experimental_use as use } from 'react'
import { matchSegment } from './match-segments' import { matchSegment } from './match-segments'
import { fetchServerResponse } from './app-router.client' import { fetchServerResponse } from './app-router.client'
function createHrefFromUrl(url: URL): string {
return url.pathname + url.search + url.hash
}
/** /**
* Invalidate cache one level down from the router state. * Invalidate cache one level down from the router state.
*/ */
...@@ -477,6 +481,7 @@ interface ReloadAction { ...@@ -477,6 +481,7 @@ interface ReloadAction {
mutable: { mutable: {
previousTree?: FlightRouterState previousTree?: FlightRouterState
patchedTree?: FlightRouterState patchedTree?: FlightRouterState
canonicalUrlOverride?: string
} }
} }
...@@ -513,6 +518,7 @@ interface NavigateAction { ...@@ -513,6 +518,7 @@ interface NavigateAction {
mutable: { mutable: {
previousTree?: FlightRouterState previousTree?: FlightRouterState
patchedTree?: FlightRouterState patchedTree?: FlightRouterState
canonicalUrlOverride?: string
useExistingCache?: true useExistingCache?: true
} }
} }
...@@ -540,16 +546,18 @@ interface ServerPatchAction { ...@@ -540,16 +546,18 @@ interface ServerPatchAction {
type: typeof ACTION_SERVER_PATCH type: typeof ACTION_SERVER_PATCH
flightData: FlightData flightData: FlightData
previousTree: FlightRouterState previousTree: FlightRouterState
overrideCanonicalUrl: URL | undefined
cache: CacheNode cache: CacheNode
mutable: { mutable: {
patchedTree?: FlightRouterState patchedTree?: FlightRouterState
canonicalUrlOverride?: string
} }
} }
interface PrefetchAction { interface PrefetchAction {
type: typeof ACTION_PREFETCH type: typeof ACTION_PREFETCH
url: URL url: URL
flightData: FlightData serverResponse: Awaited<ReturnType<typeof fetchServerResponse>>
} }
interface PushRef { interface PushRef {
...@@ -587,6 +595,7 @@ type AppRouterState = { ...@@ -587,6 +595,7 @@ type AppRouterState = {
{ {
flightSegmentPath: FlightSegmentPath flightSegmentPath: FlightSegmentPath
treePatch: FlightRouterState treePatch: FlightRouterState
canonicalUrlOverride: URL | undefined
} }
> >
/** /**
...@@ -620,8 +629,8 @@ export function reducer( ...@@ -620,8 +629,8 @@ export function reducer(
case ACTION_NAVIGATE: { case ACTION_NAVIGATE: {
const { url, navigateType, cache, mutable, forceOptimisticNavigation } = const { url, navigateType, cache, mutable, forceOptimisticNavigation } =
action action
const { pathname, search, hash } = url const { pathname, search } = url
const href = pathname + search + hash const href = createHrefFromUrl(url)
const pendingPush = navigateType === 'push' const pendingPush = navigateType === 'push'
// Handle concurrent rendering / strict mode case where the cache and tree were already populated. // Handle concurrent rendering / strict mode case where the cache and tree were already populated.
...@@ -631,7 +640,9 @@ export function reducer( ...@@ -631,7 +640,9 @@ export function reducer(
) { ) {
return { return {
// Set href. // Set href.
canonicalUrl: href, canonicalUrl: mutable.canonicalUrlOverride
? mutable.canonicalUrlOverride
: href,
// TODO-APP: verify mpaNavigation not being set is correct here. // TODO-APP: verify mpaNavigation not being set is correct here.
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
// All navigation requires scroll and focus management to trigger. // All navigation requires scroll and focus management to trigger.
...@@ -647,7 +658,8 @@ export function reducer( ...@@ -647,7 +658,8 @@ export function reducer(
const prefetchValues = state.prefetchCache.get(href) const prefetchValues = state.prefetchCache.get(href)
if (prefetchValues) { if (prefetchValues) {
// The one before last item is the router state tree patch // The one before last item is the router state tree patch
const { flightSegmentPath, treePatch } = prefetchValues const { flightSegmentPath, treePatch, canonicalUrlOverride } =
prefetchValues
// Create new tree based on the flightSegmentPath and router state patch // Create new tree based on the flightSegmentPath and router state patch
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
...@@ -685,9 +697,19 @@ export function reducer( ...@@ -685,9 +697,19 @@ export function reducer(
mutable.useExistingCache = true mutable.useExistingCache = true
} }
const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: undefined
if (canonicalUrlOverrideHref) {
mutable.canonicalUrlOverride = canonicalUrlOverrideHref
}
return { return {
// Set href. // Set href.
canonicalUrl: href, canonicalUrl: canonicalUrlOverrideHref
? canonicalUrlOverrideHref
: href,
// Set pendingPush. // Set pendingPush.
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
// All navigation requires scroll and focus management to trigger. // All navigation requires scroll and focus management to trigger.
...@@ -763,7 +785,7 @@ export function reducer( ...@@ -763,7 +785,7 @@ export function reducer(
} }
// Unwrap cache data with `use` to suspend here (in the reducer) until the fetch resolves. // Unwrap cache data with `use` to suspend here (in the reducer) until the fetch resolves.
const [flightData] = use(cache.data) const [flightData, canonicalUrlOverride] = use(cache.data)
// Handle case when navigating to page in `pages` from `app` // Handle case when navigating to page in `pages` from `app`
if (typeof flightData === 'string') { if (typeof flightData === 'string') {
...@@ -803,6 +825,12 @@ export function reducer( ...@@ -803,6 +825,12 @@ export function reducer(
throw new Error('SEGMENT MISMATCH') throw new Error('SEGMENT MISMATCH')
} }
const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: undefined
if (canonicalUrlOverrideHref) {
mutable.canonicalUrlOverride = canonicalUrlOverrideHref
}
mutable.previousTree = state.tree mutable.previousTree = state.tree
mutable.patchedTree = newTree mutable.patchedTree = newTree
...@@ -813,7 +841,9 @@ export function reducer( ...@@ -813,7 +841,9 @@ export function reducer(
return { return {
// Set href. // Set href.
canonicalUrl: href, canonicalUrl: canonicalUrlOverrideHref
? canonicalUrlOverrideHref
: href,
// Set pendingPush. // Set pendingPush.
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
// All navigation requires scroll and focus management to trigger. // All navigation requires scroll and focus management to trigger.
...@@ -826,7 +856,9 @@ export function reducer( ...@@ -826,7 +856,9 @@ export function reducer(
} }
} }
case ACTION_SERVER_PATCH: { case ACTION_SERVER_PATCH: {
const { flightData, previousTree, cache, mutable } = action const { flightData, previousTree, overrideCanonicalUrl, cache, mutable } =
action
// When a fetch is slow to resolve it could be that you navigated away while the request was happening or before the reducer runs. // When a fetch is slow to resolve it could be that you navigated away while the request was happening or before the reducer runs.
// In that case opt-out of applying the patch given that the data could be stale. // In that case opt-out of applying the patch given that the data could be stale.
if (JSON.stringify(previousTree) !== JSON.stringify(state.tree)) { if (JSON.stringify(previousTree) !== JSON.stringify(state.tree)) {
...@@ -840,7 +872,9 @@ export function reducer( ...@@ -840,7 +872,9 @@ export function reducer(
if (mutable.patchedTree) { if (mutable.patchedTree) {
return { return {
// Keep href as it was set during navigate / restore // Keep href as it was set during navigate / restore
canonicalUrl: state.canonicalUrl, canonicalUrl: mutable.canonicalUrlOverride
? mutable.canonicalUrlOverride
: state.canonicalUrl,
// Keep pushRef as server-patch only causes cache/tree update. // Keep pushRef as server-patch only causes cache/tree update.
pushRef: state.pushRef, pushRef: state.pushRef,
// Keep focusAndScrollRef as server-patch only causes cache/tree update. // Keep focusAndScrollRef as server-patch only causes cache/tree update.
...@@ -887,6 +921,14 @@ export function reducer( ...@@ -887,6 +921,14 @@ export function reducer(
throw new Error('SEGMENT MISMATCH') throw new Error('SEGMENT MISMATCH')
} }
const canonicalUrlOverrideHref = overrideCanonicalUrl
? createHrefFromUrl(overrideCanonicalUrl)
: undefined
if (canonicalUrlOverrideHref) {
mutable.canonicalUrlOverride = canonicalUrlOverrideHref
}
mutable.patchedTree = newTree mutable.patchedTree = newTree
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
...@@ -895,7 +937,9 @@ export function reducer( ...@@ -895,7 +937,9 @@ export function reducer(
return { return {
// Keep href as it was set during navigate / restore // Keep href as it was set during navigate / restore
canonicalUrl: state.canonicalUrl, canonicalUrl: canonicalUrlOverrideHref
? canonicalUrlOverrideHref
: state.canonicalUrl,
// Keep pushRef as server-patch only causes cache/tree update. // Keep pushRef as server-patch only causes cache/tree update.
pushRef: state.pushRef, pushRef: state.pushRef,
// Keep focusAndScrollRef as server-patch only causes cache/tree update. // Keep focusAndScrollRef as server-patch only causes cache/tree update.
...@@ -909,7 +953,7 @@ export function reducer( ...@@ -909,7 +953,7 @@ export function reducer(
} }
case ACTION_RESTORE: { case ACTION_RESTORE: {
const { url, tree } = action const { url, tree } = action
const href = url.pathname + url.search + url.hash const href = createHrefFromUrl(url)
return { return {
// Set canonical url // Set canonical url
...@@ -933,7 +977,9 @@ export function reducer( ...@@ -933,7 +977,9 @@ export function reducer(
) { ) {
return { return {
// Set href. // Set href.
canonicalUrl: href, canonicalUrl: mutable.canonicalUrlOverride
? mutable.canonicalUrlOverride
: href,
// set pendingPush (always false in this case). // set pendingPush (always false in this case).
pushRef: state.pushRef, pushRef: state.pushRef,
// Apply focus and scroll. // Apply focus and scroll.
...@@ -954,7 +1000,7 @@ export function reducer( ...@@ -954,7 +1000,7 @@ export function reducer(
'refetch', 'refetch',
]) ])
} }
const [flightData] = use(cache.data) const [flightData, canonicalUrlOverride] = use(cache.data)
// Handle case when navigating to page in `pages` from `app` // Handle case when navigating to page in `pages` from `app`
if (typeof flightData === 'string') { if (typeof flightData === 'string') {
...@@ -994,6 +1040,14 @@ export function reducer( ...@@ -994,6 +1040,14 @@ export function reducer(
throw new Error('SEGMENT MISMATCH') throw new Error('SEGMENT MISMATCH')
} }
const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: undefined
if (canonicalUrlOverride) {
mutable.canonicalUrlOverride = canonicalUrlOverrideHref
}
mutable.previousTree = state.tree mutable.previousTree = state.tree
mutable.patchedTree = newTree mutable.patchedTree = newTree
...@@ -1002,7 +1056,9 @@ export function reducer( ...@@ -1002,7 +1056,9 @@ export function reducer(
return { return {
// Set href, this doesn't reuse the state.canonicalUrl as because of concurrent rendering the href might change between dispatching and applying. // Set href, this doesn't reuse the state.canonicalUrl as because of concurrent rendering the href might change between dispatching and applying.
canonicalUrl: href, canonicalUrl: canonicalUrlOverrideHref
? canonicalUrlOverrideHref
: href,
// set pendingPush (always false in this case). // set pendingPush (always false in this case).
pushRef: state.pushRef, pushRef: state.pushRef,
// TODO-APP: might need to disable this for Fast Refresh. // TODO-APP: might need to disable this for Fast Refresh.
...@@ -1015,15 +1071,15 @@ export function reducer( ...@@ -1015,15 +1071,15 @@ export function reducer(
} }
} }
case ACTION_PREFETCH: { case ACTION_PREFETCH: {
const { url, flightData } = action const { url, serverResponse } = action
const [flightData, canonicalUrlOverride] = serverResponse
// TODO-APP: Implement prefetch for hard navigation // TODO-APP: Implement prefetch for hard navigation
if (typeof flightData === 'string') { if (typeof flightData === 'string') {
return state return state
} }
const { pathname, search, hash } = url const href = createHrefFromUrl(url)
const href = pathname + search + hash
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths. // TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths.
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
...@@ -1042,6 +1098,7 @@ export function reducer( ...@@ -1042,6 +1098,7 @@ export function reducer(
// Path without the last segment, router state, and the subTreeData // Path without the last segment, router state, and the subTreeData
flightSegmentPath: flightDataPath.slice(0, -2), flightSegmentPath: flightDataPath.slice(0, -2),
treePatch, treePatch,
canonicalUrlOverride,
}) })
return state return state
......
...@@ -27,6 +27,7 @@ import { ...@@ -27,6 +27,7 @@ import {
import { FlushEffectsContext } from '../shared/lib/flush-effects' import { FlushEffectsContext } from '../shared/lib/flush-effects'
import { stripInternalQueries } from './internal-utils' import { stripInternalQueries } from './internal-utils'
import type { ComponentsType } from '../build/webpack/loaders/next-app-loader' import type { ComponentsType } from '../build/webpack/loaders/next-app-loader'
import { REDIRECT_ERROR_CODE } from '../client/components/redirect'
// this needs to be required lazily so that `next-server` can set // this needs to be required lazily so that `next-server` can set
// the env before we require // the env before we require
...@@ -48,7 +49,7 @@ export type RenderOptsPartial = { ...@@ -48,7 +49,7 @@ export type RenderOptsPartial = {
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
/** /**
* Flight Response is always set to application/octet-stream to ensure it does not * Flight Response is always set to application/octet-stream to ensure it does not get interpreted as HTML.
*/ */
class FlightRenderResult extends RenderResult { class FlightRenderResult extends RenderResult {
constructor(response: string | ReadableStream<Uint8Array>) { constructor(response: string | ReadableStream<Uint8Array>) {
...@@ -65,15 +66,38 @@ function interopDefault(mod: any) { ...@@ -65,15 +66,38 @@ function interopDefault(mod: any) {
// tolerate dynamic server errors during prerendering so console // tolerate dynamic server errors during prerendering so console
// isn't spammed with unactionable errors // isn't spammed with unactionable errors
function onError(err: any) { /**
const { DynamicServerError } = * Create error handler for renderers.
require('../client/components/hooks-server-context') as typeof import('../client/components/hooks-server-context') */
function createErrorHandler(
/**
* Used for debugging
*/
_source: string
) {
return (err: any) => {
if (
// Use error message instead of type because HTML renderer uses Flight data which is serialized so it's not the same object instance.
err.message &&
!err.message.includes('Dynamic server usage') &&
// TODO-APP: Handle redirect throw
err.code !== REDIRECT_ERROR_CODE
) {
// Used for debugging error source
// console.error(_source, err)
console.error(err)
}
if (!(err instanceof DynamicServerError)) { return null
console.error(err)
} }
} }
const serverComponentsErrorHandler = createErrorHandler(
'serverComponentsRenderer'
)
const flightDataRendererErrorHandler = createErrorHandler('flightDataRenderer')
const htmlRendererErrorHandler = createErrorHandler('htmlRenderer')
let isFetchPatched = false let isFetchPatched = false
// we patch fetch to collect cache information used for // we patch fetch to collect cache information used for
...@@ -241,7 +265,7 @@ function createServerComponentRenderer( ...@@ -241,7 +265,7 @@ function createServerComponentRenderer(
serverComponentManifest, serverComponentManifest,
{ {
context: serverContexts, context: serverContexts,
onError, onError: serverComponentsErrorHandler,
} }
) )
} }
...@@ -515,7 +539,7 @@ export async function renderToHTMLOrFlight( ...@@ -515,7 +539,7 @@ export async function renderToHTMLOrFlight(
const flightData: FlightData = pathname + (search ? `?${search}` : '') const flightData: FlightData = pathname + (search ? `?${search}` : '')
return new FlightRenderResult( return new FlightRenderResult(
renderToReadableStream(flightData, serverComponentManifest, { renderToReadableStream(flightData, serverComponentManifest, {
onError, onError: flightDataRendererErrorHandler,
}).pipeThrough(createBufferedTransformStream()) }).pipeThrough(createBufferedTransformStream())
) )
} }
...@@ -1008,7 +1032,7 @@ export async function renderToHTMLOrFlight( ...@@ -1008,7 +1032,7 @@ export async function renderToHTMLOrFlight(
serverComponentManifest, serverComponentManifest,
{ {
context: serverContexts, context: serverContexts,
onError, onError: flightDataRendererErrorHandler,
} }
).pipeThrough(createBufferedTransformStream()) ).pipeThrough(createBufferedTransformStream())
...@@ -1121,6 +1145,7 @@ export async function renderToHTMLOrFlight( ...@@ -1121,6 +1145,7 @@ export async function renderToHTMLOrFlight(
ReactDOMServer, ReactDOMServer,
element: content, element: content,
streamOptions: { streamOptions: {
onError: htmlRendererErrorHandler,
nonce, nonce,
// Include hydration scripts in the HTML // Include hydration scripts in the HTML
bootstrapScripts: subresourceIntegrityManifest bootstrapScripts: subresourceIntegrityManifest
...@@ -1140,7 +1165,11 @@ export async function renderToHTMLOrFlight( ...@@ -1140,7 +1165,11 @@ export async function renderToHTMLOrFlight(
flushEffectHandler, flushEffectHandler,
flushEffectsToHead: true, flushEffectsToHead: true,
}) })
} catch (err) { } catch (err: any) {
if (err.code === REDIRECT_ERROR_CODE) {
throw err
}
// TODO-APP: show error overlay in development. `element` should probably be wrapped in AppRouter for this case. // TODO-APP: show error overlay in development. `element` should probably be wrapped in AppRouter for this case.
const renderStream = await renderToInitialStream({ const renderStream = await renderToInitialStream({
ReactDOMServer, ReactDOMServer,
...@@ -1173,9 +1202,8 @@ export async function renderToHTMLOrFlight( ...@@ -1173,9 +1202,8 @@ export async function renderToHTMLOrFlight(
} }
} }
const readable = await bodyResult()
if (generateStaticHTML) { if (generateStaticHTML) {
const readable = await bodyResult()
let staticHtml = Buffer.from( let staticHtml = Buffer.from(
(await readable.getReader().read()).value || '' (await readable.getReader().read()).value || ''
).toString() ).toString()
...@@ -1190,5 +1218,20 @@ export async function renderToHTMLOrFlight( ...@@ -1190,5 +1218,20 @@ export async function renderToHTMLOrFlight(
return new RenderResult(staticHtml) return new RenderResult(staticHtml)
} }
return new RenderResult(readable)
try {
return new RenderResult(await bodyResult())
} catch (err: any) {
if (err.code === REDIRECT_ERROR_CODE) {
;(renderOpts as any).pageData = {
pageProps: {
__N_REDIRECT: err.url,
__N_REDIRECT_STATUS: 307,
},
}
;(renderOpts as any).isRedirect = true
return RenderResult.fromStatic('')
}
throw err
}
} }
...@@ -61,7 +61,8 @@ export const GlobalLayoutRouterContext = React.createContext<{ ...@@ -61,7 +61,8 @@ export const GlobalLayoutRouterContext = React.createContext<{
tree: FlightRouterState tree: FlightRouterState
changeByServerResponse: ( changeByServerResponse: (
previousTree: FlightRouterState, previousTree: FlightRouterState,
flightData: FlightData flightData: FlightData,
overrideCanonicalUrl: URL | undefined
) => void ) => void
focusAndScrollRef: FocusAndScrollRef focusAndScrollRef: FocusAndScrollRef
}>(null as any) }>(null as any)
......
...@@ -41,7 +41,7 @@ declare module 'react' { ...@@ -41,7 +41,7 @@ declare module 'react' {
} }
// TODO-APP: check if this is the right type. // TODO-APP: check if this is the right type.
function experimental_use<T>(promise: Promise<T>): Awaited<T> function experimental_use<T>(promise: Promise<T> | React.Context<T>): T
} }
export type Redirect = export type Redirect =
......
import { useLayoutSegments } from 'next/dist/client/components/hooks-client' 'client'
// TODO-APP: enable once test is not skipped.
// import { useLayoutSegments } from 'next/dist/client/components/hooks-client'
export default function Page() { export default function Page() {
// This should throw an error. // This should throw an error.
useLayoutSegments() // useLayoutSegments()
return null return null
} }
import { useParams } from 'next/dist/client/components/hooks-client' // TODO-APP: enable when implemented.
// import { useParams } from 'next/dist/client/components/hooks-client'
export default function Page() { export default function Page() {
// This should throw an error. // This should throw an error.
useParams() // useParams()
return null return null
} }
import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client' 'client'
// TODO-APP: enable once test is not skipped.
// import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client'
export default function Page() { export default function Page() {
// This should throw an error. // This should throw an error.
useSelectedLayoutSegment() // useSelectedLayoutSegment()
return null return null
} }
'client'
import { redirect } from 'next/dist/client/components/redirect-client'
import React from 'react'
export default function Page() {
const [shouldRedirect, setShouldRedirect] = React.useState(false)
if (shouldRedirect) {
redirect('/redirect/result')
}
return (
<button
onClick={() => React.startTransition(() => setShouldRedirect(true))}
>
Redirect!
</button>
)
}
'client'
import { redirect } from 'next/dist/client/components/redirect'
export default function ClientComp() {
redirect('/redirect/result')
return <></>
}
import ClientComp from './client-component'
import { useHeaders } from 'next/dist/client/components/hooks-server'
export default function Page() {
// Opt-in to SSR.
useHeaders()
return <ClientComp />
}
import Link from 'next/link'
export default function Page() {
return (
<>
<Link href="/redirect/a">
<a id="redirect-a">To Dashboard through /redirect/a</a>
</Link>
</>
)
}
import Link from 'next/link'
export default function Page() {
return (
<>
<Link href="/redirect-middleware-to-dashboard">
<a id="redirect-middleware">To Dashboard through /redirect/a</a>
</Link>
</>
)
}
export default function Page() {
return <h1 id="result-page">Result Page</h1>
}
import { redirect } from 'next/dist/client/components/redirect'
export default function Page() {
redirect('/redirect/result')
return <></>
}
...@@ -10,6 +10,10 @@ export function middleware(request) { ...@@ -10,6 +10,10 @@ export function middleware(request) {
return NextResponse.rewrite(new URL('/dashboard', request.url)) return NextResponse.rewrite(new URL('/dashboard', request.url))
} }
if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
if (request.nextUrl.pathname.startsWith('/internal/test')) { if (request.nextUrl.pathname.startsWith('/internal/test')) {
const method = request.nextUrl.pathname.endsWith('rewrite') const method = request.nextUrl.pathname.endsWith('rewrite')
? 'rewrite' ? 'rewrite'
......
...@@ -18,4 +18,13 @@ module.exports = { ...@@ -18,4 +18,13 @@ module.exports = {
], ],
} }
}, },
redirects: () => {
return [
{
source: '/redirect/a',
destination: '/dashboard',
permanent: false,
},
]
},
} }
...@@ -29,9 +29,6 @@ describe('app dir', () => { ...@@ -29,9 +29,6 @@ describe('app dir', () => {
'react-dom': 'experimental', 'react-dom': 'experimental',
}, },
skipStart: true, skipStart: true,
env: {
VERCEL_ANALYTICS_ID: 'fake-analytics-id',
},
}) })
await next.start() await next.start()
...@@ -623,7 +620,7 @@ describe('app dir', () => { ...@@ -623,7 +620,7 @@ describe('app dir', () => {
}) })
// TODO-APP: investigate hydration not kicking in on some runs // TODO-APP: investigate hydration not kicking in on some runs
it.skip('should serve client-side', async () => { it('should serve client-side', async () => {
const browser = await webdriver(next.url, '/client-component-route') const browser = await webdriver(next.url, '/client-component-route')
// After hydration count should be 1 // After hydration count should be 1
...@@ -643,8 +640,7 @@ describe('app dir', () => { ...@@ -643,8 +640,7 @@ describe('app dir', () => {
expect($('p').text()).toBe('hello from app/client-nested') expect($('p').text()).toBe('hello from app/client-nested')
}) })
// TODO-APP: investigate hydration not kicking in on some runs it('should include it client-side', async () => {
it.skip('should include it client-side', async () => {
const browser = await webdriver(next.url, '/client-nested') const browser = await webdriver(next.url, '/client-nested')
// After hydration count should be 1 // After hydration count should be 1
...@@ -1448,40 +1444,6 @@ describe('app dir', () => { ...@@ -1448,40 +1444,6 @@ describe('app dir', () => {
}) })
}) })
// Analytics events are only sent in production
;(isDev ? describe.skip : describe)('Vercel analytics', () => {
it('should send web vitals to Vercel analytics', async () => {
let eventsCount = 0
let countEvents = false
const browser = await webdriver(next.url, '/client-nested', {
beforePageLoad(page) {
page.route(
'https://vitals.vercel-insights.com/v1/vitals',
(route) => {
if (countEvents) {
eventsCount += 1
}
route.fulfill()
}
)
},
})
// Start counting analytics events
countEvents = true
// Refresh will trigger CLS and LCP. When page loads FCP and TTFB will trigger:
await browser.refresh()
// After interaction LCP and FID will trigger
await browser.elementByCss('button').click()
// Make sure all registered events in performance-relayer has fired
await check(() => eventsCount, /6/)
})
})
describe('known bugs', () => { describe('known bugs', () => {
it('should not share flight data between requests', async () => { it('should not share flight data between requests', async () => {
const fetches = await Promise.all( const fetches = await Promise.all(
...@@ -1496,6 +1458,82 @@ describe('app dir', () => { ...@@ -1496,6 +1458,82 @@ describe('app dir', () => {
} }
}) })
}) })
describe('redirect', () => {
describe('components', () => {
it.skip('should redirect in a server component', async () => {
const browser = await webdriver(next.url, '/redirect/servercomponent')
await browser.waitForElementByCss('#result-page')
expect(await browser.elementByCss('#result-page').text()).toBe(
'Result Page'
)
})
it('should redirect in a client component', async () => {
const browser = await webdriver(next.url, '/redirect/clientcomponent')
await browser.waitForElementByCss('#result-page')
expect(await browser.elementByCss('#result-page').text()).toBe(
'Result Page'
)
})
it('should redirect client-side', async () => {
const browser = await webdriver(next.url, '/redirect/client-side')
await browser
.elementByCss('button')
.click()
.waitForElementByCss('#result-page')
expect(await browser.elementByCss('#result-page').text()).toBe(
'Result Page'
)
})
})
describe('next.config.js redirects', () => {
it('should redirect from next.config.js', async () => {
const browser = await webdriver(next.url, '/redirect/a')
expect(await browser.elementByCss('h1').text()).toBe('Dashboard')
expect(await browser.url()).toBe(next.url + '/dashboard')
})
it('should redirect from next.config.js with link navigation', async () => {
const browser = await webdriver(
next.url,
'/redirect/next-config-redirect'
)
await browser
.elementByCss('#redirect-a')
.click()
.waitForElementByCss('h1')
expect(await browser.elementByCss('h1').text()).toBe('Dashboard')
expect(await browser.url()).toBe(next.url + '/dashboard')
})
})
describe('middleware redirects', () => {
it('should redirect from middleware', async () => {
const browser = await webdriver(
next.url,
'/redirect-middleware-to-dashboard'
)
expect(await browser.elementByCss('h1').text()).toBe('Dashboard')
expect(await browser.url()).toBe(next.url + '/dashboard')
})
it('should redirect from middleware with link navigation', async () => {
const browser = await webdriver(
next.url,
'/redirect/next-middleware-redirect'
)
await browser
.elementByCss('#redirect-middleware')
.click()
.waitForElementByCss('h1')
expect(await browser.elementByCss('h1').text()).toBe('Dashboard')
expect(await browser.url()).toBe(next.url + '/dashboard')
})
})
})
} }
runTests() runTests()
......
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { check } from 'next-test-utils'
import path from 'path'
import webdriver from 'next-webdriver'
describe('vercel analytics', () => {
const isDev = (global as any).isNextDev
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
function runTests({ assetPrefix }: { assetPrefix?: boolean }) {
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app')),
dependencies: {
react: 'experimental',
'react-dom': 'experimental',
},
skipStart: true,
env: {
VERCEL_ANALYTICS_ID: 'fake-analytics-id',
},
})
if (assetPrefix) {
const content = await next.readFile('next.config.js')
await next.patchFile(
'next.config.js',
content
.replace('// assetPrefix', 'assetPrefix')
.replace('// beforeFiles', 'beforeFiles')
)
}
await next.start()
})
afterAll(() => next.destroy())
// Analytics events are only sent in production
;(isDev ? describe.skip : describe)('Vercel analytics', () => {
it('should send web vitals to Vercel analytics', async () => {
let eventsCount = 0
let countEvents = false
const browser = await webdriver(next.url, '/client-nested', {
beforePageLoad(page) {
page.route(
'https://vitals.vercel-insights.com/v1/vitals',
(route) => {
if (countEvents) {
eventsCount += 1
}
route.fulfill()
}
)
},
})
// Start counting analytics events
countEvents = true
// Refresh will trigger CLS and LCP. When page loads FCP and TTFB will trigger:
await browser.refresh()
// After interaction LCP and FID will trigger
await browser.elementByCss('button').click()
// Make sure all registered events in performance-relayer has fired
await check(() => eventsCount, /6/)
})
})
}
describe('without assetPrefix', () => {
runTests({})
})
describe('with assetPrefix', () => {
runTests({ assetPrefix: true })
})
})
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