-
Notifications
You must be signed in to change notification settings - Fork 27.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[metadata] initial support of streaming metadata (#74619)
- Loading branch information
Showing
15 changed files
with
272 additions
and
30 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
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,44 @@ | ||
'use client' | ||
|
||
import { use } from 'react' | ||
import { useServerInsertedHTML } from '../../client/components/navigation' | ||
|
||
// This is a SSR-only version that will wait the promise of metadata to resolve | ||
// and | ||
function ServerInsertMetadata({ promise }: { promise: Promise<any> }) { | ||
let metadataToFlush: React.ReactNode = null | ||
// Only inserted once to avoid multi insertion on re-renders | ||
let inserted = false | ||
|
||
promise.then((resolvedMetadata) => { | ||
metadataToFlush = resolvedMetadata | ||
}) | ||
|
||
useServerInsertedHTML(() => { | ||
if (metadataToFlush && !inserted) { | ||
const flushing = metadataToFlush | ||
metadataToFlush = null | ||
return flushing | ||
} | ||
}) | ||
|
||
inserted = true | ||
|
||
return null | ||
} | ||
|
||
function BrowserResolvedMetadata({ promise }: { promise: Promise<any> }) { | ||
return use(promise) | ||
} | ||
|
||
export function AsyncMetadata({ promise }: { promise: Promise<any> }) { | ||
return ( | ||
<> | ||
{typeof window === 'undefined' ? ( | ||
<ServerInsertMetadata promise={promise} /> | ||
) : ( | ||
<BrowserResolvedMetadata promise={promise} /> | ||
)} | ||
</> | ||
) | ||
} |
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
// Bot crawler that will spin up a headless browser and execute JS | ||
const HEADLESS_BOT_UA_RE = | ||
const HEADLESS_BROWSER_BOT_UA_RE = | ||
/Googlebot|Google-PageRenderer|AdsBot-Google|googleweblight|Storebot-Google/i | ||
|
||
// This regex contains the bots that we need to do a blocking render for and can't safely stream the response | ||
// due to how they parse the DOM. For example, they might explicitly check for metadata in the `head` tag, so we can't stream metadata tags after the `head` was sent. | ||
const HTML_LIMITED_BOT_UA_RE = | ||
/Mediapartners-Google|Slurp|DuckDuckBot|baiduspider|yandex|sogou|bitlybot|tumblr|vkShare|quora link preview|redditbot|ia_archiver|Bingbot|BingPreview|applebot|facebookexternalhit|facebookcatalog|Twitterbot|LinkedInBot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview/i | ||
|
||
function isHeadlessBrowserBot(userAgent: string) { | ||
return HEADLESS_BOT_UA_RE.test(userAgent) | ||
function isHeadlessBrowserBotUA(userAgent: string) { | ||
return HEADLESS_BROWSER_BOT_UA_RE.test(userAgent) | ||
} | ||
|
||
function isHtmlLimitedBot(userAgent: string) { | ||
export function isHtmlLimitedBotUA(userAgent: string) { | ||
return HTML_LIMITED_BOT_UA_RE.test(userAgent) | ||
} | ||
|
||
export function isBot(userAgent: string): boolean { | ||
return isHeadlessBrowserBot(userAgent) || isHtmlLimitedBot(userAgent) | ||
return isHeadlessBrowserBotUA(userAgent) || isHtmlLimitedBotUA(userAgent) | ||
} |
Oops, something went wrong.