Commit 14842ac7 authored by Jimmy Lai's avatar Jimmy Lai

Cover flat export fallbacks and fix resume path

parent f26ce518
......@@ -3,6 +3,7 @@ import {
fetchOutputExportDataResponse,
fetchOutputExportFallbackResponse,
getOutputExportFallbackCandidates,
stripOutputExportDataSuffix,
} from './output-export-fallback'
describe('output export fallback helpers', () => {
......@@ -40,6 +41,23 @@ describe('output export fallback helpers', () => {
).toBe('https://example.com/blog/post/index.txt')
})
it('strips the export data suffix back to the fallback document path', () => {
expect(
stripOutputExportDataSuffix(
new URL('https://example.com/blog/post/index.txt')
).href
).toBe('https://example.com/blog/post')
expect(
stripOutputExportDataSuffix(new URL('https://example.com/blog/post.txt'))
.href
).toBe('https://example.com/blog/post.html')
expect(
stripOutputExportDataSuffix(new URL('https://example.com/index.txt')).href
).toBe('https://example.com/')
})
it('tries both flat and trailing-slash data files', async () => {
const fetchMock = jest.fn(async (input: RequestInfo | URL) => {
const url = String(input)
......
......@@ -28,9 +28,9 @@ export function addOutputExportDataSuffix(url: URL): URL {
export function stripOutputExportDataSuffix(url: URL): URL {
const nextUrl = new URL(url)
if (nextUrl.pathname.endsWith('/index.txt')) {
nextUrl.pathname = nextUrl.pathname.slice(0, -9)
nextUrl.pathname = nextUrl.pathname.slice(0, -10) || '/'
} else if (nextUrl.pathname.endsWith('.txt')) {
nextUrl.pathname = nextUrl.pathname.slice(0, -4)
nextUrl.pathname = `${nextUrl.pathname.slice(0, -4)}.html`
}
return nextUrl
}
......
......@@ -535,7 +535,7 @@ function createClientResumeScriptInsertionTransformStream(): TransformStream<
undefined // headers[NEXT_URL]
)
const searchStr = `${NEXT_RSC_UNION_QUERY}=${cacheBustingHeader}`
const NEXT_CLIENT_RESUME_SCRIPT = `<script>try{var u=sessionStorage.getItem('${originalUrlSessionKey}');var r=location.pathname;if(u){r=r.endsWith('/')?r+'index.txt':r+'.txt'}__NEXT_CLIENT_RESUME=fetch(r+'?${searchStr}',{credentials:'same-origin',headers:{'${RSC_HEADER}': '1','${NEXT_ROUTER_PREFETCH_HEADER}': '1','${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}': '${segmentPath}'}});if(u){window.${originalUrlGlobalKey}=u;history.replaceState(null,'',u);sessionStorage.removeItem('${originalUrlSessionKey}')}}catch{}</script>`
const NEXT_CLIENT_RESUME_SCRIPT = `<script>try{var u=sessionStorage.getItem('${originalUrlSessionKey}');var r=location.pathname;if(u){r=r.endsWith('.html')?r.slice(0,-5)+'.txt':r.endsWith('/')?r+'index.txt':r+'.txt'}__NEXT_CLIENT_RESUME=fetch(r+'?${searchStr}',{credentials:'same-origin',headers:{'${RSC_HEADER}': '1','${NEXT_ROUTER_PREFETCH_HEADER}': '1','${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}': '${segmentPath}'}});if(u){window.${originalUrlGlobalKey}=u;history.replaceState(null,'',u);sessionStorage.removeItem('${originalUrlSessionKey}')}}catch{}</script>`
let didAlreadyInsert = false
return new TransformStream({
......
import { join } from 'path'
import { nextTestSetup } from 'e2e-utils'
import { findPort, retry, stopApp } from 'next-test-utils'
import webdriver from 'next-webdriver'
import fs from 'fs-extra'
import express from 'express'
import http from 'http'
import { createReadStream } from 'fs'
const { next, skipped, isNextDev } = nextTestSetup({
files: join(__dirname, '..'),
skipDeployment: true,
skipStart: true,
disableAutoSkewProtection: true,
})
if (skipped) {
describe.skip('app dir - output export dynamic routes with Cache Components without trailing slashes', () => {})
} else {
const describeProduction = isNextDev ? describe.skip : describe
describeProduction(
'app dir - output export dynamic routes with Cache Components without trailing slashes',
() => {
let port: number
let stopOrKill: (() => Promise<void>) | undefined
beforeAll(async () => {
await next.patchFile('next.config.js', (content) =>
content.replace(
'trailingSlash: true,',
`trailingSlash: false,
cacheComponents: true,`
)
)
await next.patchFile(
'app/another/[slug]/slug-client.js',
`'use client'
import { useParams } from 'next/navigation'
export default function SlugClient() {
const params = useParams()
return <h1>{params.slug}</h1>
}
`
)
await next.patchFile(
'app/another/[slug]/page.js',
() => `import Link from 'next/link'
import { Suspense } from 'react'
import SlugClient from './slug-client'
export default function Page() {
return (
<main>
<Suspense fallback={<h1>Loading slug...</h1>}>
<SlugClient />
</Suspense>
<ul>
<li>
<Link href="/another">Visit another page</Link>
</li>
<li>
<Link href="/org/acme/chat/thread-flat">Visit org thread</Link>
</li>
</ul>
</main>
)
}
`
)
await next.patchFile(
'app/org/page.js',
`import Link from 'next/link'
export default function OrgIndexPage() {
return (
<main>
<h1>Org index</h1>
<ul>
<li>
<Link href="/org/acme/chat/thread-123">Visit org chat thread 123</Link>
</li>
</ul>
</main>
)
}
`
)
await next.patchFile(
'app/org/[org]/org-client.js',
`'use client'
import { useParams } from 'next/navigation'
export default function OrgClient() {
const params = useParams()
return <p id="org-name">Org {params.org}</p>
}
`
)
await next.patchFile(
'app/org/[org]/layout.js',
`import { Suspense } from 'react'
import OrgClient from './org-client'
export default function OrgLayout({ children }) {
return (
<main>
<Suspense fallback={<p id="org-name">Loading org...</p>}>
<OrgClient />
</Suspense>
{children}
</main>
)
}
`
)
await next.patchFile(
'app/org/[org]/chat/[thread]/thread-client.js',
`'use client'
import { useParams } from 'next/navigation'
export default function OrgThreadClient() {
const params = useParams()
return <h1>{params.org}:{params.thread}</h1>
}
`
)
await next.patchFile(
'app/org/[org]/chat/[thread]/page.js',
`import Link from 'next/link'
import { Suspense } from 'react'
import OrgThreadClient from './thread-client'
export default function OrgThreadPage() {
return (
<>
<Suspense fallback={<h1>Loading org thread...</h1>}>
<OrgThreadClient />
</Suspense>
<ul>
<li>
<Link href="/org">Visit org index</Link>
</li>
</ul>
</>
)
}
`
)
await next.deleteFile('app/api/json/route.js')
await next.deleteFile('app/api/txt/route.js')
await next.patchFile('app/another/page.js', (content) =>
content.replace(
` <li>
<Link href="/image-import">image import page</Link>
</li>`,
` <li>
<Link href="/another/third">another third page</Link>
</li>
<li>
<Link href="/image-import">image import page</Link>
</li>`
)
)
await next.build()
port = await findPort()
const app = express()
const server = http.createServer(app)
const outDir = join(next.testDir, 'out')
const fallbackHtml = join(outDir, '_fallback.html')
app.use(
express.static(outDir, {
extensions: ['html'],
redirect: false,
})
)
app.use((req, res) => {
createReadStream(fallbackHtml).pipe(res)
})
await new Promise<void>((resolve) => server.listen(port, resolve))
stopOrKill = () => stopApp(server)
})
afterAll(async () => {
if (stopOrKill) {
await stopOrKill()
}
await next.destroy()
})
it('writes flat fallback artifacts for trailingSlash false', async () => {
const outDir = join(next.testDir, 'out')
expect(await fs.pathExists(join(outDir, '_fallback.html'))).toBe(true)
expect(
await fs.pathExists(join(outDir, 'another', '__fallback.html'))
).toBe(true)
expect(
await fs.pathExists(join(outDir, 'another', '__fallback.txt'))
).toBe(true)
expect(await fs.pathExists(join(outDir, 'org', '__fallback.txt'))).toBe(
true
)
expect(
await fs.pathExists(
join(outDir, 'another', '__fallback', 'index.html')
)
).toBe(false)
})
it('renders an unenumerated slug on hard load and client navigation without adding a trailing slash', async () => {
const browser = await webdriver(port, '/another/third')
try {
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('third')
expect(await browser.eval('window.location.pathname')).toBe(
'/another/third'
)
})
await browser.elementByCss('a[href="/another"]').click()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('Another')
expect(await browser.eval('window.location.pathname')).toBe(
'/another'
)
})
await browser.elementByCss('a[href="/another/third"]').click()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('third')
expect(await browser.eval('window.location.pathname')).toBe(
'/another/third'
)
})
} finally {
await browser.close()
}
})
it('preserves search params and hash for nested fallback routes without trailing slashes', async () => {
const browser = await webdriver(
port,
'/org/umbrella/chat/thread-flat?view=full#messages'
)
try {
await retry(async () => {
expect(await browser.elementByCss('#org-name').text()).toBe(
'Org umbrella'
)
expect(await browser.elementByCss('h1').text()).toBe(
'umbrella:thread-flat'
)
expect(
await browser.eval(
'window.location.pathname + window.location.search + window.location.hash'
)
).toBe('/org/umbrella/chat/thread-flat?view=full#messages')
})
} finally {
await browser.close()
}
})
}
)
}
......@@ -790,6 +790,68 @@ export default function OrgThreadPage() {
}
})
it('preserves search params and hash across fallback hard loads', async () => {
const browser = await webdriver(
port,
'/another/query-param/?tab=notes#anchor'
)
try {
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('query-param')
expect(
await browser.eval(
'window.location.pathname + window.location.search + window.location.hash'
)
).toBe('/another/query-param/?tab=notes#anchor')
})
} finally {
await browser.close()
}
})
it('supports a longer history sequence across static and fallback routes', async () => {
const browser = await webdriver(port, '/another/history-one/')
try {
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('history-one')
})
await browser.elementByCss('a[href="/another/"]').click()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('Another')
})
await browser.elementByCss('a[href="/another/third/"]').click()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('third')
})
await browser.back()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('Another')
})
await browser.back()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('history-one')
})
await browser.forward()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('Another')
})
await browser.forward()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('third')
})
} finally {
await browser.close()
}
})
it('shows Suspense fallback UI during hard load of a fallback route', async () => {
const browser = await webdriver(port, '/another/suspense-test/')
......
......@@ -391,6 +391,42 @@ export default function OrgThreadPage() {
await browser.close()
}
})
it('preserves history across prerendered and fallback routes with search params', async () => {
const browser = await webdriver(port, '/another/first/?mode=known')
try {
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('first')
expect(await browser.eval('window.location.search')).toBe(
'?mode=known'
)
})
await browser.elementByCss('a[href="/another/third/"]').click()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('third')
expect(await browser.eval('window.location.pathname')).toBe(
'/another/third/'
)
})
await browser.back()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('first')
expect(await browser.eval('window.location.search')).toBe(
'?mode=known'
)
})
await browser.forward()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('third')
})
} finally {
await browser.close()
}
})
}
)
}
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