-
Notifications
You must be signed in to change notification settings - Fork 24
feat(vtex-proxy): add props to exclude and add sitemap entries #1549
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,23 +23,59 @@ export const VTEX_PATHS_THAT_REQUIRES_SAME_REFERER = ["/no-cache/AviseMe.aspx"]; | |
|
|
||
| const decoSiteMapUrl = "/sitemap/deco.xml"; | ||
|
|
||
| /** @title {{__title}} */ | ||
| export interface IncludeSiteMapEntry { | ||
| /** @title Title (CMS only) */ | ||
| __title?: string; | ||
| /** @title Path */ | ||
| path: string; | ||
| /** @title Handler */ | ||
| handler?: string; | ||
| /** @title Paths to exclude from the sitemap */ | ||
| excludePaths?: string[]; | ||
| } | ||
|
|
||
| const normalizeIncludeSiteMap = ( | ||
| includeSiteMap?: IncludeSiteMapEntry[], | ||
| ): IncludeSiteMapEntry[] => (includeSiteMap ?? []); | ||
|
|
||
| const includeEntriesToPaths = (entries: IncludeSiteMapEntry[]): string[] => | ||
| entries.map((e) => e.path); | ||
|
Comment on lines
+38
to
+43
Contributor
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. Normalize and dedupe sitemap paths before materializing them. As ♻️ Proposed fix const normalizeIncludeSiteMap = (
includeSiteMap?: IncludeSiteMapEntry[],
-): IncludeSiteMapEntry[] => (includeSiteMap ?? []);
+): IncludeSiteMapEntry[] => {
+ const byPath = new Map<string, IncludeSiteMapEntry>();
+ for (const entry of includeSiteMap ?? []) {
+ byPath.set(entry.path, entry);
+ }
+ return [...byPath.values()];
+};
+
+const dedupePaths = (paths: string[]) => [...new Set(paths)];
const includeEntriesToPaths = (entries: IncludeSiteMapEntry[]): string[] =>
entries.map((e) => e.path);
@@
const [include, routes] = generateDecoSiteMap
? [
- [
+ dedupePaths([
...includeEntriesToPaths(entries),
...(includeSiteMap ?? []),
decoSiteMapUrl,
- ],
+ ]),
[
...customSitemapRoutes,
{
@@
]
: [
- [...includeEntriesToPaths(entries), ...(includeSiteMap ?? [])],
+ dedupePaths([
+ ...includeEntriesToPaths(entries),
+ ...(includeSiteMap ?? []),
+ ]),
customSitemapRoutes,
];Also applies to: 131-155 🤖 Prompt for AI Agents |
||
|
|
||
| const includeEntriesToRoutes = (entries: IncludeSiteMapEntry[]): Route[] => | ||
| entries | ||
| .map(({ path, handler, excludePaths, ...rest }) => ({ | ||
| pathTemplate: path, | ||
| handler: { | ||
| value: { | ||
| excludePaths, | ||
| __resolveType: handler ?? "website/handlers/sitemap.ts", | ||
| ...rest, | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| const buildProxyRoutes = ( | ||
| { | ||
| publicUrl, | ||
| extraPaths, | ||
| includeSiteMap, | ||
| includeSiteMapWithHandler, | ||
| includePathToDecoSitemap, | ||
| generateDecoSiteMap, | ||
| excludePathsFromDecoSiteMap, | ||
| excludeSiteMapEntry, | ||
| includeScriptsToHead, | ||
| includeScriptsToBody, | ||
| }: { | ||
| publicUrl?: string; | ||
| extraPaths: string[]; | ||
| includeSiteMap?: string[]; | ||
| includeSiteMapWithHandler?: IncludeSiteMapEntry[]; | ||
| includePathToDecoSitemap?: string[]; | ||
| generateDecoSiteMap?: boolean; | ||
| excludePathsFromDecoSiteMap: string[]; | ||
| excludeSiteMapEntry?: string[]; | ||
| includeScriptsToHead?: { | ||
| includes?: Script[]; | ||
| }; | ||
|
|
@@ -53,6 +89,8 @@ const buildProxyRoutes = ( | |
| } | ||
|
|
||
| try { | ||
| const entries = normalizeIncludeSiteMap(includeSiteMapWithHandler); | ||
| const customSitemapRoutes = includeEntriesToRoutes(entries); | ||
| const hostname = (new URL( | ||
| publicUrl?.startsWith("http") ? publicUrl : `https://${publicUrl}`, | ||
| )).hostname; | ||
|
|
@@ -91,17 +129,30 @@ const buildProxyRoutes = ( | |
| ); | ||
|
|
||
| const [include, routes] = generateDecoSiteMap | ||
| ? [[...(includeSiteMap ?? []), decoSiteMapUrl], [{ | ||
| pathTemplate: decoSiteMapUrl, | ||
| handler: { | ||
| value: { | ||
| excludePaths: excludePathsFromDecoSiteMap, | ||
| includePaths: includePathToDecoSitemap, | ||
| __resolveType: "website/handlers/sitemap.ts", | ||
| ? [ | ||
| [ | ||
| ...includeEntriesToPaths(entries), | ||
| ...(includeSiteMap ?? []), | ||
| decoSiteMapUrl, | ||
| ], | ||
| [ | ||
| ...customSitemapRoutes, | ||
| { | ||
| pathTemplate: decoSiteMapUrl, | ||
| handler: { | ||
| value: { | ||
| excludePaths: excludePathsFromDecoSiteMap, | ||
| includePaths: includePathToDecoSitemap, | ||
| __resolveType: "website/handlers/sitemap.ts", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }]] | ||
| : [includeSiteMap, []]; | ||
| ], | ||
| ] | ||
| : [ | ||
| [...includeEntriesToPaths(entries), ...(includeSiteMap ?? [])], | ||
| customSitemapRoutes, | ||
| ]; | ||
|
|
||
| return [ | ||
| ...routes, | ||
|
|
@@ -110,6 +161,7 @@ const buildProxyRoutes = ( | |
| handler: { | ||
| value: { | ||
| include, | ||
| excludeSiteMapEntry, | ||
| __resolveType: "vtex/handlers/sitemap.ts", | ||
| }, | ||
| }, | ||
|
|
@@ -137,6 +189,11 @@ export interface Props { | |
| * @title Other site maps to include | ||
| */ | ||
| includeSiteMap?: string[]; | ||
| /** | ||
| * @title Other site maps to include | ||
| * @description URL path (e.g. "/sitemap/blog.xml") or object with path + handler (__resolveType) to register a route and add to the index. Use the object form for dynamic sitemaps (e.g. from Sanity). | ||
| */ | ||
| includeSiteMapWithHandler?: IncludeSiteMapEntry[]; | ||
|
Comment on lines
+192
to
+196
Contributor
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. The schema description still implies raw strings are valid here.
🤖 Prompt for AI Agents |
||
| /** | ||
| * @title Paths to include in the deco sitemap | ||
| */ | ||
|
|
@@ -149,6 +206,11 @@ export interface Props { | |
| * @title Exclude paths from /deco-sitemap.xml | ||
| */ | ||
| excludePathsFromDecoSiteMap?: string[]; | ||
| /** | ||
| * @title Sitemap entries to remove from the sitemap index | ||
| * @description Path or URL substrings; any <sitemap> in /sitemap.xml whose <loc> contains one of these will be removed. | ||
| */ | ||
| excludeSiteMapEntry?: string[]; | ||
| /** | ||
| * @title Scripts to include on Html head | ||
| */ | ||
|
|
@@ -171,9 +233,11 @@ function loader( | |
| { | ||
| extraPathsToProxy = [], | ||
| includeSiteMap = [], | ||
| includeSiteMapWithHandler = [], | ||
| includePathToDecoSitemap = [], | ||
| generateDecoSiteMap = true, | ||
| excludePathsFromDecoSiteMap = [], | ||
| excludeSiteMapEntry = [], | ||
| includeScriptsToHead = { includes: [] }, | ||
| includeScriptsToBody = { includes: [] }, | ||
| }: Props, | ||
|
|
@@ -183,7 +247,9 @@ function loader( | |
| return buildProxyRoutes({ | ||
| generateDecoSiteMap, | ||
| excludePathsFromDecoSiteMap, | ||
| excludeSiteMapEntry, | ||
| includeSiteMap, | ||
| includeSiteMapWithHandler, | ||
| includePathToDecoSitemap, | ||
| publicUrl: ctx.publicUrl, | ||
| extraPaths: extraPathsToProxy, | ||
|
|
||
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.
Exclude upstream entries before re-injecting custom ones.
Because
vtex/loaders/proxy.ts:131-155pushesincludeSiteMapWithHandlerpaths intoinclude, filtering afterincludeSiteMaps()removes the replacement entry too. Excluding/sitemap/foo.xmland re-adding the same path currently leaves no<sitemap>block for it in/sitemap.xml.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents