Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit late streaming meta tags #47207

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/next/src/server/app-render/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,35 @@ export async function renderToHTMLOrFlight(
)

let polyfillsFlushed = false
const getServerInsertedHTML = (): Promise<string> => {
let flushedErrorMetaTagsUntilIndex = 0
const getServerInsertedHTML = () => {
// Loop through all the errors that have been captured but not yet
// flushed.
const errorMetaTags = []
for (
;
flushedErrorMetaTagsUntilIndex < allCapturedErrors.length;
flushedErrorMetaTagsUntilIndex++
) {
const error = allCapturedErrors[flushedErrorMetaTagsUntilIndex]
if (isNotFoundError(error)) {
errorMetaTags.push(
<meta name="robots" content="noindex" key={error.digest} />
)
} else if (isRedirectError(error)) {
const redirectUrl = getURLFromRedirectError(error)
if (redirectUrl) {
errorMetaTags.push(
<meta
httpEquiv="refresh"
content={`0;url=${redirectUrl}`}
key={error.digest}
/>
)
}
}
}

const flushed = renderToString(
<>
{Array.from(serverInsertedHTMLCallbacks).map((callback) =>
Expand All @@ -1723,6 +1751,7 @@ export async function renderToHTMLOrFlight(
/>
)
})}
{errorMetaTags}
</>
)
polyfillsFlushed = true
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/app-dir/navigation/app/redirect/suspense/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Suspense } from 'react'
import { redirect } from 'next/navigation'

function createSuspenseyComponent(Component, { timeout = 0, expire = 10 }) {
let result
let promise
return function Data() {
if (result) return result
if (!promise)
promise = new Promise((resolve) => {
setTimeout(() => {
result = <Component />
setTimeout(() => {
result = undefined
promise = undefined
}, expire)
resolve()
}, timeout)
})
throw promise
}
}

function Redirect() {
redirect('/redirect/result')
return <></>
}

const SuspenseyRedirect = createSuspenseyComponent(Redirect, {
timeout: 300,
})

export default function () {
return (
<div className="suspense">
<Suspense fallback="fallback">
<SuspenseyRedirect />
</Suspense>
</div>
)
}
17 changes: 14 additions & 3 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ createNextDescribe(
).toBe('noindex')
})
it('should trigger not-found while streaming', async () => {
const initialHtml = await next.render('/not-found/suspense')
expect(initialHtml).not.toContain('noindex')

const browser = await next.browser('/not-found/suspense')
expect(
await browser.waitForElementByCss('#not-found-component').text()
Expand Down Expand Up @@ -261,5 +258,19 @@ createNextDescribe(
}
})
})

describe('SEO', () => {
it('should emit noindex meta tag for not found page when streaming', async () => {
const html = await next.render('/not-found/suspense')
expect(html).toContain('<meta name="robots" content="noindex"/>')
})

it('should emit refresh meta tag for redirect page when streaming', async () => {
const html = await next.render('/redirect/suspense')
expect(html).toContain(
'<meta http-equiv="refresh" content="0;url=/redirect/result"/>'
)
})
})
}
)