-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the html insertion html stream handling for ppr
- Loading branch information
Showing
6 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/production/app-dir/ppr-use-server-inserted-html/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react' | ||
|
||
export default async function Root({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
test/production/app-dir/ppr-use-server-inserted-html/app/partial-resume/client.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use client' | ||
|
||
import { useRef } from 'react' | ||
import { useServerInsertedHTML } from 'next/navigation' | ||
|
||
export function InsertHtml({ id, data }: { id: string; data: string }) { | ||
const insertRef = useRef(false) | ||
useServerInsertedHTML(() => { | ||
// only insert the style tag once | ||
if (insertRef.current) { | ||
return | ||
} | ||
insertRef.current = true | ||
const value = ( | ||
<style | ||
data-test-id={id} | ||
>{`[data-inserted-${data}] { content: ${data} }`}</style> | ||
) | ||
console.log(`testing-log-insertion:${data}`) | ||
return value | ||
}) | ||
|
||
return <div>Loaded: {data}</div> | ||
} |
22 changes: 22 additions & 0 deletions
22
test/production/app-dir/ppr-use-server-inserted-html/app/partial-resume/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React, { Suspense } from 'react' | ||
import { headers } from 'next/headers' | ||
import { InsertHtml } from './client' | ||
|
||
async function Dynamic() { | ||
await headers() | ||
|
||
return ( | ||
<div> | ||
<h3>dynamic</h3> | ||
<InsertHtml id={'inserted-html'} data={'dynamic-data'} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Dynamic /> | ||
</Suspense> | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
test/production/app-dir/ppr-use-server-inserted-html/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
experimental: { | ||
ppr: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
32 changes: 32 additions & 0 deletions
32
test/production/app-dir/ppr-use-server-inserted-html/ppr-use-server-inserted-html.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('ppr-use-server-inserted-html', () => { | ||
const { next, isNextStart } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
if (isNextStart) { | ||
it('should mark the route as ppr rendered', async () => { | ||
const prerenderManifest = JSON.parse( | ||
await next.readFile('.next/prerender-manifest.json') | ||
) | ||
expect(prerenderManifest.routes['/partial-resume'].renderingMode).toBe( | ||
'PARTIALLY_STATIC' | ||
) | ||
}) | ||
} | ||
|
||
it('should not log insertion in build', async () => { | ||
const output = next.cliOutput | ||
expect(output).not.toContain('testing-log-insertion:') | ||
}) | ||
|
||
it('should insert the html insertion into html body', async () => { | ||
const $ = await next.render$('/partial-resume') | ||
const output = next.cliOutput | ||
expect(output).toContain('testing-log-insertion:dynamic-data') | ||
|
||
expect($('head [data-test-id]').length).toBe(0) | ||
expect($('body [data-test-id]').length).toBe(1) | ||
}) | ||
}) |