-
Notifications
You must be signed in to change notification settings - Fork 3.7k
perf: up to 3x initial load time improvements across plugins and s3 storage #16518
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
Open
paulpopus
wants to merge
3
commits into
main
Choose a base branch
from
perf/init-time-bundle-stage-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -130,105 +130,115 @@ export const seoPlugin = | |
|
|
||
| return collection | ||
| }) || [], | ||
| endpoints: [ | ||
| ...(config.endpoints ?? []), | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
| endpoints: (() => { | ||
| // Built once per plugin init: avoid an O(N) array scan per request × 4 endpoints. | ||
| // Closes over the plugin's input `config` snapshot — same lifetime as the prior | ||
| // `.find()` calls below, so behavior is preserved. | ||
| const collectionConfigBySlug = new Map<string, NonNullable<Config['collections']>[number]>() | ||
| for (const c of config.collections ?? []) { | ||
|
paulpopus marked this conversation as resolved.
Outdated
|
||
| collectionConfigBySlug.set(c.slug, c) | ||
| } | ||
| const globalConfigBySlug = new Map<string, NonNullable<Config['globals']>[number]>() | ||
| for (const g of config.globals ?? []) { | ||
| globalConfigBySlug.set(g.slug, g) | ||
| } | ||
| const getCollectionConfig = (slug: string | undefined) => | ||
| slug ? collectionConfigBySlug.get(slug) : undefined | ||
| const getGlobalConfig = (slug: string | undefined) => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary code / function call overhead, |
||
| slug ? globalConfigBySlug.get(slug) : undefined | ||
|
|
||
| const reqData = data ?? req.data | ||
| return [ | ||
| ...(config.endpoints ?? []), | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
|
|
||
| const result = pluginConfig.generateTitle | ||
| ? await pluginConfig.generateTitle({ | ||
| ...data, | ||
| collectionConfig: config.collections?.find( | ||
| (c) => c.slug === reqData.collectionSlug, | ||
| ), | ||
| globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateTitle>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| const reqData = data ?? req.data | ||
|
|
||
| const result = pluginConfig.generateTitle | ||
| ? await pluginConfig.generateTitle({ | ||
| ...data, | ||
| collectionConfig: getCollectionConfig(reqData.collectionSlug), | ||
| globalConfig: getGlobalConfig(reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateTitle>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-title', | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-title', | ||
| }, | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
|
|
||
| const reqData = data ?? req.data | ||
| const reqData = data ?? req.data | ||
|
|
||
| const result = pluginConfig.generateDescription | ||
| ? await pluginConfig.generateDescription({ | ||
| ...data, | ||
| collectionConfig: config.collections?.find( | ||
| (c) => c.slug === reqData.collectionSlug, | ||
| ), | ||
| globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateDescription>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| const result = pluginConfig.generateDescription | ||
| ? await pluginConfig.generateDescription({ | ||
| ...data, | ||
| collectionConfig: getCollectionConfig(reqData.collectionSlug), | ||
| globalConfig: getGlobalConfig(reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateDescription>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-description', | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-description', | ||
| }, | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
|
|
||
| const reqData = data ?? req.data | ||
| const reqData = data ?? req.data | ||
|
|
||
| const result = pluginConfig.generateURL | ||
| ? await pluginConfig.generateURL({ | ||
| ...data, | ||
| collectionConfig: config.collections?.find( | ||
| (c) => c.slug === reqData.collectionSlug, | ||
| ), | ||
| globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateURL>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| const result = pluginConfig.generateURL | ||
| ? await pluginConfig.generateURL({ | ||
| ...data, | ||
| collectionConfig: getCollectionConfig(reqData.collectionSlug), | ||
| globalConfig: getGlobalConfig(reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateURL>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-url', | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-url', | ||
| }, | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
| { | ||
| handler: async (req) => { | ||
| const data: Omit< | ||
| Parameters<GenerateTitle>[0], | ||
| 'collectionConfig' | 'globalConfig' | 'req' | ||
| > = await req.json?.() | ||
|
|
||
| const reqData = data ?? req.data | ||
| const reqData = data ?? req.data | ||
|
|
||
| const result = pluginConfig.generateImage | ||
| ? await pluginConfig.generateImage({ | ||
| ...data, | ||
| collectionConfig: config.collections?.find( | ||
| (c) => c.slug === reqData.collectionSlug, | ||
| ), | ||
| globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateImage>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| const result = pluginConfig.generateImage | ||
| ? await pluginConfig.generateImage({ | ||
| ...data, | ||
| collectionConfig: getCollectionConfig(reqData.collectionSlug), | ||
| globalConfig: getGlobalConfig(reqData.globalSlug), | ||
| req, | ||
| } satisfies Parameters<GenerateImage>[0]) | ||
| : '' | ||
| return new Response(JSON.stringify({ result }), { status: 200 }) | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-image', | ||
| }, | ||
| method: 'post', | ||
| path: '/plugin-seo/generate-image', | ||
| }, | ||
| ], | ||
| ] | ||
| })(), | ||
| globals: | ||
| config.globals?.map((global) => { | ||
| const { slug } = global | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it's just gonna be creating the 2 maps, can we move that in the plugin function body instead of creating and calling this inline function?