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(
),
isStaticGeneration: generateStaticHTML,
getServerInsertedHTML,
serverInsertedHTMLToHead: true,
validateRootLayout,
})
} catch (err) {
......@@ -2109,7 +2108,6 @@ async function renderToStream(
basePath: renderOpts.basePath,
tracingMetadata: tracingMetadata,
}),
serverInsertedHTMLToHead: true,
validateRootLayout,
})
} catch (finalErr: any) {
......@@ -3503,7 +3501,6 @@ async function prerenderToStream(
),
isStaticGeneration: true,
getServerInsertedHTML,
serverInsertedHTMLToHead: true,
validateRootLayout,
}),
dynamicAccess: consumeDynamicAccess(
......@@ -3829,7 +3826,6 @@ async function prerenderToStream(
),
isStaticGeneration: true,
getServerInsertedHTML,
serverInsertedHTMLToHead: true,
}),
// TODO: Should this include the SSR pass?
collectedRevalidate: prerenderLegacyStore.revalidate,
......@@ -4004,7 +4000,6 @@ async function prerenderToStream(
basePath: renderOpts.basePath,
tracingMetadata: tracingMetadata,
}),
serverInsertedHTMLToHead: true,
validateRootLayout,
}),
dynamicAccess: null,
......
......@@ -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({
ReactDOMServer,
element,
......@@ -532,8 +517,7 @@ function chainTransformers<T>(
export type ContinueStreamOptions = {
inlinedDataStream: ReadableStream<Uint8Array> | undefined
isStaticGeneration: boolean
getServerInsertedHTML: (() => Promise<string>) | undefined
serverInsertedHTMLToHead: boolean
getServerInsertedHTML: () => Promise<string>
validateRootLayout?: boolean
/**
* Suffix to inject after the buffered data, but before the close tags.
......@@ -548,7 +532,6 @@ export async function continueFizzStream(
inlinedDataStream,
isStaticGeneration,
getServerInsertedHTML,
serverInsertedHTMLToHead,
validateRootLayout,
}: ContinueStreamOptions
): Promise<ReadableStream<Uint8Array>> {
......@@ -565,11 +548,6 @@ export async function continueFizzStream(
// Buffer everything to avoid flushing too frequently
createBufferedTransformStream(),
// Insert generated tags to head
getServerInsertedHTML && !serverInsertedHTMLToHead
? createInsertedHTMLStream(getServerInsertedHTML)
: null,
// Insert suffix content
suffixUnclosed != null && suffixUnclosed.length > 0
? createDeferredSuffixStream(suffixUnclosed)
......@@ -587,9 +565,7 @@ export async function continueFizzStream(
// Special head insertions
// 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.
getServerInsertedHTML && serverInsertedHTMLToHead
? createHeadInsertionTransformStream(getServerInsertedHTML)
: null,
createHeadInsertionTransformStream(getServerInsertedHTML),
])
}
......
import React from 'react'
import RootStyleRegistry from './root-style-registry'
export default function AppLayout({ children }) {
return (
......@@ -7,9 +6,7 @@ export default function AppLayout({ children }) {
<head>
<title>RSC</title>
</head>
<body>
<RootStyleRegistry>{children}</RootStyleRegistry>
</body>
<body>{children}</body>
</html>
)
}
......@@ -22,10 +22,6 @@ async function resolveStreamResponse(response: any, onData?: any) {
describe('app dir - rsc basics', () => {
const { next, isNextDev, isNextStart, isTurbopack } = nextTestSetup({
files: __dirname,
dependencies: {
'styled-components': 'latest',
'server-only': 'latest',
},
resolutions: {
'@babel/core': '7.22.18',
'@babel/parser': '7.22.16',
......@@ -368,77 +364,6 @@ describe('app dir - rsc basics', () => {
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 () => {
const browser = await next.browser('/edge/dynamic')
const indexUrl = await browser.url()
......
......@@ -2,7 +2,26 @@
import { Suspense } from 'react'
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`
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