-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
feat: add createSitemapItems hook #10083
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0065a3f
feat: add createSitemapItems hook
johnnyreilly fb069b1
fix: remove head and options from params
johnnyreilly 1830cf2
Update website/docs/api/plugins/plugin-sitemap.mdx
slorber d9816f9
simplify example
slorber c624788
refactor a bit types and logic
slorber 60eef87
add extra unit test covering createSitemapItems returning []
slorber d7bbfe0
remove website example
slorber 61ae607
empty
slorber 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 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 |
---|---|---|
|
@@ -44,11 +44,25 @@ Accepted fields: | |
| `priority` | `number \| null` | `0.5` | See [sitemap docs](https://www.sitemaps.org/protocol.html#xmlTagDefinitions) | | ||
| `ignorePatterns` | `string[]` | `[]` | A list of glob patterns; matching route paths will be filtered from the sitemap. Note that you may need to include the base URL in here. | | ||
| `filename` | `string` | `sitemap.xml` | The path to the created sitemap file, relative to the output directory. Useful if you have two plugin instances outputting two files. | | ||
| `createSitemapItems` | <code>[CreateSitemapItemsFn](#CreateSitemapItemsFn) \| undefined</code> | `undefined` | An optional function which can be used to transform and / or filter the items in the sitemap. | | ||
|
||
```mdx-code-block | ||
</APITable> | ||
``` | ||
|
||
### Types {#types} | ||
|
||
#### `CreateSitemapItemsFn` {#CreateSitemapItemsFn} | ||
|
||
```ts | ||
type CreateSitemapItemsFn = (params: { | ||
siteConfig: DocusaurusConfig; | ||
routes: RouteConfig[]; | ||
head: {[location: string]: HelmetServerState}; | ||
options: PluginOptions; | ||
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. wonder if this one is really useful I know createFeedItem takes the config (which IMHO is already not super useful), but it doesn't take the option so maybe we shouldn't add this here? |
||
}) => Promise<SitemapItem[]>; | ||
``` | ||
|
||
:::info | ||
|
||
This plugin also respects some site config: | ||
|
@@ -86,6 +100,14 @@ const config = { | |
priority: 0.5, | ||
ignorePatterns: ['/tags/**'], | ||
filename: 'sitemap.xml', | ||
createSitemapItems: async (params) => { | ||
const {defaultCreateSitemapItems, ...rest} = params; | ||
const sitemapItems = await defaultCreateSitemapItems(rest); | ||
const sitemapsWithoutPage = sitemapItems.filter( | ||
(sitemapItem) => !sitemapItem.url.includes('/page/'), | ||
); | ||
return sitemapsWithoutPage; | ||
}, | ||
}; | ||
``` | ||
|
||
|
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
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.
I know
postBuild
already receives this, but IMHO this was a mistake and we shouldn't expose this "messy" data structure as part of our public API.Unless it's really needed I'd prefer to remove it for now, or provide it with a clear Docusaurus public API.
Also React 19 has metadata apis now, so not sure it's a good time to provide such API. We can introduce it later once things get clearer.
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.
The reason the API is this shape is because it is modelled on the
createFeedItems
API which allows the caller to call the underlying default implementation and provides the necessary inputs to do so. We could have a different style of API here which doesn't work that way. Instead, perhaps, we could expose a "default" implementation which wraps the underlying dependencies of the actual implementation and just exposes a parameterless function that could be called. That would likely feel cleaner.What do you think? The downside of this is inconsistency. The
createFeedItems
andcreateSitemapItems
APIs will be similarly named but slightly different to use from one another. Not necessarily a big issue - but worth consideringThere 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.
I'm not sure we talk about the same thing. What annoys me most is this
{[location: string]: HelmetServerState};
And annoying code like this:
I'd prefer if we didn't expose
HelmetServerState
to the user. I don't thinkcreateFeedItems
exposes it so far.