-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[metadata] add option of configuring ua of async metadata (#74594)
### What Introduce an experimental config `experimental.htmlLimitedBots` (type `Regex`) which can used for controlling the metadata streaming behavior for different bots, if the bots user agent is matching the regex, it will serve blocking metadata forcedly. This gives users some control to opt-out streaming metadata for some clients or bots which cannot handle it. Output another manifest `response-config-manfiest.json` containing the customized UA, which is potentially used for the platform serving Next.js to control the returned response for certain UA. But it's mostly for PPR case which we'll tackle later. Closes NDX-635 Closes NDX-599
- Loading branch information
Showing
18 changed files
with
228 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../../shared/lib/router/utils/is-bot' | ||
|
||
export function shouldServeStreamingMetadata( | ||
userAgent: string, | ||
{ | ||
streamingMetadata, | ||
htmlLimitedBots, | ||
}: { | ||
streamingMetadata: boolean | ||
htmlLimitedBots: string | undefined | ||
} | ||
): boolean { | ||
if (!streamingMetadata) { | ||
return false | ||
} | ||
|
||
const blockingMetadataUARegex = new RegExp( | ||
htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING, | ||
'i' | ||
) | ||
return !blockingMetadataUARegex.test(userAgent) | ||
} |
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
43 changes: 43 additions & 0 deletions
43
test/e2e/app-dir/metadata-streaming/metadata-streaming-customized-rule.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,43 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('app-dir - metadata-streaming-customized-rule', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
overrideFiles: { | ||
'next.config.js': ` | ||
module.exports = { | ||
experimental: { | ||
streamingMetadata: true, | ||
htmlLimitedBots: /Minibot/i, | ||
} | ||
} | ||
`, | ||
}, | ||
}) | ||
|
||
it('should send the blocking response for html limited bots', async () => { | ||
const $ = await next.render$( | ||
'/', | ||
undefined, // no query | ||
{ | ||
headers: { | ||
'user-agent': 'Minibot', | ||
}, | ||
} | ||
) | ||
expect(await $('title').text()).toBe('index page') | ||
}) | ||
|
||
it('should send streaming response for headless browser bots', async () => { | ||
const $ = await next.render$( | ||
'/', | ||
undefined, // no query | ||
{ | ||
headers: { | ||
'user-agent': 'Weebot', | ||
}, | ||
} | ||
) | ||
expect(await $('title').length).toBe(0) | ||
}) | ||
}) |
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
8 changes: 8 additions & 0 deletions
8
test/production/app-dir/metadata-streaming-config/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,8 @@ | ||
import { ReactNode } from 'react' | ||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/production/app-dir/metadata-streaming-config/app/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,3 @@ | ||
export default function Page() { | ||
return <p>hello world</p> | ||
} |
40 changes: 40 additions & 0 deletions
40
...production/app-dir/metadata-streaming-config/metadata-streaming-config-customized.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,40 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('app-dir - metadata-streaming-config-customized', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
overrideFiles: { | ||
'next.config.js': ` | ||
module.exports = { | ||
experimental: { | ||
streamingMetadata: true, | ||
htmlLimitedBots: /MyBot/i, | ||
} | ||
} | ||
`, | ||
}, | ||
}) | ||
|
||
it('should have the default streaming metadata config output in routes-manifest.json', async () => { | ||
const requiredServerFiles = JSON.parse( | ||
await next.readFile('.next/required-server-files.json') | ||
) | ||
expect(requiredServerFiles.files).toContain( | ||
'.next/response-config-manifest.json' | ||
) | ||
expect( | ||
requiredServerFiles.config.experimental.htmlLimitedBots | ||
).toMatchInlineSnapshot(`"MyBot"`) | ||
|
||
const responseConfigManifest = JSON.parse( | ||
await next.readFile('.next/response-config-manifest.json') | ||
) | ||
|
||
expect(responseConfigManifest).toMatchInlineSnapshot(` | ||
{ | ||
"htmlLimitedBots": "MyBot", | ||
"version": 0, | ||
} | ||
`) | ||
}) | ||
}) |
Oops, something went wrong.