Unverified Commit 461f0be2 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] Remove deadcode and extract separate test suite (#75528)

parent 3e279c3c
...@@ -1963,7 +1963,6 @@ async function renderToStream( ...@@ -1963,7 +1963,6 @@ async function renderToStream(
), ),
isStaticGeneration: generateStaticHTML, isStaticGeneration: generateStaticHTML,
getServerInsertedHTML, getServerInsertedHTML,
serverInsertedHTMLToHead: true,
validateRootLayout, validateRootLayout,
}) })
} catch (err) { } catch (err) {
...@@ -2109,7 +2108,6 @@ async function renderToStream( ...@@ -2109,7 +2108,6 @@ async function renderToStream(
basePath: renderOpts.basePath, basePath: renderOpts.basePath,
tracingMetadata: tracingMetadata, tracingMetadata: tracingMetadata,
}), }),
serverInsertedHTMLToHead: true,
validateRootLayout, validateRootLayout,
}) })
} catch (finalErr: any) { } catch (finalErr: any) {
...@@ -3503,7 +3501,6 @@ async function prerenderToStream( ...@@ -3503,7 +3501,6 @@ async function prerenderToStream(
), ),
isStaticGeneration: true, isStaticGeneration: true,
getServerInsertedHTML, getServerInsertedHTML,
serverInsertedHTMLToHead: true,
validateRootLayout, validateRootLayout,
}), }),
dynamicAccess: consumeDynamicAccess( dynamicAccess: consumeDynamicAccess(
...@@ -3829,7 +3826,6 @@ async function prerenderToStream( ...@@ -3829,7 +3826,6 @@ async function prerenderToStream(
), ),
isStaticGeneration: true, isStaticGeneration: true,
getServerInsertedHTML, getServerInsertedHTML,
serverInsertedHTMLToHead: true,
}), }),
// TODO: Should this include the SSR pass? // TODO: Should this include the SSR pass?
collectedRevalidate: prerenderLegacyStore.revalidate, collectedRevalidate: prerenderLegacyStore.revalidate,
...@@ -4004,7 +4000,6 @@ async function prerenderToStream( ...@@ -4004,7 +4000,6 @@ async function prerenderToStream(
basePath: renderOpts.basePath, basePath: renderOpts.basePath,
tracingMetadata: tracingMetadata, tracingMetadata: tracingMetadata,
}), }),
serverInsertedHTMLToHead: true,
validateRootLayout, validateRootLayout,
}), }),
dynamicAccess: null, dynamicAccess: null,
......
...@@ -178,21 +178,6 @@ export function createBufferedTransformStream(): TransformStream< ...@@ -178,21 +178,6 @@ export function createBufferedTransformStream(): TransformStream<
}) })
} }
function createInsertedHTMLStream(
getServerInsertedHTML: () => Promise<string>
): TransformStream<Uint8Array, Uint8Array> {
return new TransformStream({
transform: async (chunk, controller) => {
const html = await getServerInsertedHTML()
if (html) {
controller.enqueue(encoder.encode(html))
}
controller.enqueue(chunk)
},
})
}
export function renderToInitialFizzStream({ export function renderToInitialFizzStream({
ReactDOMServer, ReactDOMServer,
element, element,
...@@ -532,8 +517,7 @@ function chainTransformers<T>( ...@@ -532,8 +517,7 @@ function chainTransformers<T>(
export type ContinueStreamOptions = { export type ContinueStreamOptions = {
inlinedDataStream: ReadableStream<Uint8Array> | undefined inlinedDataStream: ReadableStream<Uint8Array> | undefined
isStaticGeneration: boolean isStaticGeneration: boolean
getServerInsertedHTML: (() => Promise<string>) | undefined getServerInsertedHTML: () => Promise<string>
serverInsertedHTMLToHead: boolean
validateRootLayout?: boolean validateRootLayout?: boolean
/** /**
* Suffix to inject after the buffered data, but before the close tags. * Suffix to inject after the buffered data, but before the close tags.
...@@ -548,7 +532,6 @@ export async function continueFizzStream( ...@@ -548,7 +532,6 @@ export async function continueFizzStream(
inlinedDataStream, inlinedDataStream,
isStaticGeneration, isStaticGeneration,
getServerInsertedHTML, getServerInsertedHTML,
serverInsertedHTMLToHead,
validateRootLayout, validateRootLayout,
}: ContinueStreamOptions }: ContinueStreamOptions
): Promise<ReadableStream<Uint8Array>> { ): Promise<ReadableStream<Uint8Array>> {
...@@ -565,11 +548,6 @@ export async function continueFizzStream( ...@@ -565,11 +548,6 @@ export async function continueFizzStream(
// Buffer everything to avoid flushing too frequently // Buffer everything to avoid flushing too frequently
createBufferedTransformStream(), createBufferedTransformStream(),
// Insert generated tags to head
getServerInsertedHTML && !serverInsertedHTMLToHead
? createInsertedHTMLStream(getServerInsertedHTML)
: null,
// Insert suffix content // Insert suffix content
suffixUnclosed != null && suffixUnclosed.length > 0 suffixUnclosed != null && suffixUnclosed.length > 0
? createDeferredSuffixStream(suffixUnclosed) ? createDeferredSuffixStream(suffixUnclosed)
...@@ -587,9 +565,7 @@ export async function continueFizzStream( ...@@ -587,9 +565,7 @@ export async function continueFizzStream(
// Special head insertions // Special head insertions
// TODO-APP: Insert server side html to end of head in app layout rendering, to avoid // TODO-APP: Insert server side html to end of head in app layout rendering, to avoid
// hydration errors. Remove this once it's ready to be handled by react itself. // hydration errors. Remove this once it's ready to be handled by react itself.
getServerInsertedHTML && serverInsertedHTMLToHead createHeadInsertionTransformStream(getServerInsertedHTML),
? createHeadInsertionTransformStream(getServerInsertedHTML)
: null,
]) ])
} }
......
import React from 'react' import React from 'react'
import RootStyleRegistry from './root-style-registry'
export default function AppLayout({ children }) { export default function AppLayout({ children }) {
return ( return (
...@@ -7,9 +6,7 @@ export default function AppLayout({ children }) { ...@@ -7,9 +6,7 @@ export default function AppLayout({ children }) {
<head> <head>
<title>RSC</title> <title>RSC</title>
</head> </head>
<body> <body>{children}</body>
<RootStyleRegistry>{children}</RootStyleRegistry>
</body>
</html> </html>
) )
} }
...@@ -22,10 +22,6 @@ async function resolveStreamResponse(response: any, onData?: any) { ...@@ -22,10 +22,6 @@ async function resolveStreamResponse(response: any, onData?: any) {
describe('app dir - rsc basics', () => { describe('app dir - rsc basics', () => {
const { next, isNextDev, isNextStart, isTurbopack } = nextTestSetup({ const { next, isNextDev, isNextStart, isTurbopack } = nextTestSetup({
files: __dirname, files: __dirname,
dependencies: {
'styled-components': 'latest',
'server-only': 'latest',
},
resolutions: { resolutions: {
'@babel/core': '7.22.18', '@babel/core': '7.22.18',
'@babel/parser': '7.22.16', '@babel/parser': '7.22.16',
...@@ -368,77 +364,6 @@ describe('app dir - rsc basics', () => { ...@@ -368,77 +364,6 @@ describe('app dir - rsc basics', () => {
expect(content).toContain('bar.server.js:') expect(content).toContain('bar.server.js:')
}) })
it('should render initial styles of css-in-js in nodejs SSR correctly', async () => {
const $ = await next.render$('/css-in-js')
const head = $('head').html()
// from styled-jsx
expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style
expect(head).toMatch(/{color:(\s*)(?:hotpink|#ff69b4);?}/) // styled-jsx/css
// from styled-components
expect(head).toMatch(/{color:(\s*)(?:blue|#00f);?}/)
})
it('should render initial styles of css-in-js in edge SSR correctly', async () => {
const $ = await next.render$('/css-in-js/edge')
const head = $('head').html()
// from styled-jsx
expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style
expect(head).toMatch(/{color:(\s*)(?:hotpink|#ff69b4);?}/) // styled-jsx/css
// from styled-components
expect(head).toMatch(/{color:(\s*)(?:blue|#00f);?}/)
})
it('should render css-in-js suspense boundary correctly', async () => {
await next.fetch('/css-in-js/suspense').then(async (response) => {
const results = []
await resolveStreamResponse(response, (chunk: string) => {
const isSuspenseyDataResolved =
/<style[^<>]*>(\s)*.+{padding:2px;(\s)*color:orange;}/.test(chunk)
if (isSuspenseyDataResolved) results.push('data')
// check if rsc refresh script for suspense show up, the test content could change with react version
const hasRCScript = /\$RC=function/.test(chunk)
if (hasRCScript) results.push('refresh-script')
const isFallbackResolved = chunk.includes('$test-fallback-sentinel')
if (isFallbackResolved) results.push('fallback')
})
expect(results).toEqual(['fallback', 'data', 'refresh-script'])
})
// // TODO-APP: fix streaming/suspense within browser for test suite
// const browser = await next.browser( '/css-in-js', { waitHydration: false })
// const footer = await browser.elementByCss('#footer')
// expect(await footer.text()).toBe('wait for fallback')
// expect(
// await browser.eval(
// `window.getComputedStyle(document.querySelector('#footer')).borderColor`
// )
// ).toBe('rgb(255, 165, 0)')
// // Suspense is not rendered yet
// expect(
// await browser.eval(
// `document.querySelector('#footer-inner')`
// )
// ).toBe('null')
// // Wait for suspense boundary
// await check(
// () => browser.elementByCss('#footer').text(),
// 'wait for footer'
// )
// expect(
// await browser.eval(
// `window.getComputedStyle(document.querySelector('#footer-inner')).color`
// )
// ).toBe('rgb(255, 165, 0)')
})
it('should stick to the url without trailing /page suffix', async () => { it('should stick to the url without trailing /page suffix', async () => {
const browser = await next.browser('/edge/dynamic') const browser = await next.browser('/edge/dynamic')
const indexUrl = await browser.url() const indexUrl = await browser.url()
......
...@@ -2,7 +2,26 @@ ...@@ -2,7 +2,26 @@
import { Suspense } from 'react' import { Suspense } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { createDataFetcher } from '../../../lib/data'
export function createDataFetcher(data, { timeout = 0, expire = 10 }) {
let result
let promise
return function Data() {
if (result) return result
if (!promise)
promise = new Promise((resolve) => {
setTimeout(() => {
result = data
setTimeout(() => {
result = undefined
promise = undefined
}, expire)
resolve()
}, timeout)
})
throw promise
}
}
const Footer = styled.div` const Footer = styled.div`
border: 1px solid orange; border: 1px solid orange;
......
import RootStyleRegistry from './root-style-registry'
export default function Root({ children }) {
return (
<html>
<body>
<RootStyleRegistry>{children}</RootStyleRegistry>
</body>
</html>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
async function resolveStreamResponse(response: any, onData?: any) {
let result = ''
onData = onData || (() => {})
for await (const chunk of response.body) {
result += chunk.toString()
onData(chunk.toString(), result)
}
return result
}
describe('use-server-inserted-html', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
'styled-components': 'latest',
'server-only': 'latest',
},
})
it('should render initial styles of css-in-js in nodejs SSR correctly', async () => {
const $ = await next.render$('/css-in-js')
const head = $('head').html()
// from styled-jsx
expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style
expect(head).toMatch(/{color:(\s*)(?:hotpink|#ff69b4);?}/) // styled-jsx/css
// from styled-components
expect(head).toMatch(/{color:(\s*)(?:blue|#00f);?}/)
})
it('should render initial styles of css-in-js in edge SSR correctly', async () => {
const $ = await next.render$('/css-in-js/edge')
const head = $('head').html()
// from styled-jsx
expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style
expect(head).toMatch(/{color:(\s*)(?:hotpink|#ff69b4);?}/) // styled-jsx/css
// from styled-components
expect(head).toMatch(/{color:(\s*)(?:blue|#00f);?}/)
})
it('should render css-in-js suspense boundary correctly', async () => {
await next.fetch('/css-in-js/suspense').then(async (response) => {
const results = []
await resolveStreamResponse(response, (chunk: string) => {
const isSuspenseyDataResolved =
/<style[^<>]*>(\s)*.+{padding:2px;(\s)*color:orange;}/.test(chunk)
if (isSuspenseyDataResolved) results.push('data')
// check if rsc refresh script for suspense show up, the test content could change with react version
const hasRCScript = /\$RC=function/.test(chunk)
if (hasRCScript) results.push('refresh-script')
const isFallbackResolved = chunk.includes('$test-fallback-sentinel')
if (isFallbackResolved) results.push('fallback')
})
expect(results).toEqual(['fallback', 'data', 'refresh-script'])
})
// // TODO-APP: fix streaming/suspense within browser for test suite
// const browser = await next.browser( '/css-in-js', { waitHydration: false })
// const footer = await browser.elementByCss('#footer')
// expect(await footer.text()).toBe('wait for fallback')
// expect(
// await browser.eval(
// `window.getComputedStyle(document.querySelector('#footer')).borderColor`
// )
// ).toBe('rgb(255, 165, 0)')
// // Suspense is not rendered yet
// expect(
// await browser.eval(
// `document.querySelector('#footer-inner')`
// )
// ).toBe('null')
// // Wait for suspense boundary
// await check(
// () => browser.elementByCss('#footer').text(),
// 'wait for footer'
// )
// expect(
// await browser.eval(
// `window.getComputedStyle(document.querySelector('#footer-inner')).color`
// )
// ).toBe('rgb(255, 165, 0)')
})
})
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