# How to create sitemaps for dynamic pages

**URL:** <https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [April 24, 2020, 7:35pm UTC](https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593 "2020-04-24T19:35:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabiomolinar](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/fabiomolinar/32/15968_2.png) [@fabiomolinar](https://forum.wixstudio.com/u/fabiomolinar)\
**Post date:** [April 24, 2020, 7:35pm UTC](https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593/1 "2020-04-24T19:35:30Z")

</div>

Looking at Wix Corvid API, I had the impression I could use the _routers.js_ file to create a sitemap for my dynamic pages. This is what I did (my dynamic pages have the _goals_ prefix).

```javascript
import {WixRouterSitemapEntry} from "wix-router";

```

```javascript
export function clothes_SiteMap(sitemapRequest) {

```

```javascript
	// Creates the sitemaps for the clothes prefix.

```

```javascript
	let siteMapEntries = [];

```

```javascript
	let fixedPaths = {

```

```javascript
		"Women": {

```

```javascript
			pageName: "Women clothes",

```

```javascript
			title: "Women clothes",

```

```javascript
			url: "women",

```

```javascript
			changeFrequency: "weekly",

```

```javascript
			priority: 0.5

```

```javascript
		},

```

```javascript
		"Men": {

```

```javascript
			pageName: "Men clothes",

```

```javascript
			title: "Men clothes",

```

```javascript
			url: "men",

```

```javascript
			changeFrequency: "weekly",

```

```javascript
			priority: 0.5

```

```javascript
		}

```

```javascript
	}

```

```javascript
	for (let key in fixedPaths){

```

```javascript
		let entry = new WixRouterSitemapEntry(key);

```

```javascript
		entry.pageName = fixedPaths[key].pageName;

```

```javascript
		entry.title = fixedPaths[key].title;

```

```javascript
		entry.url = "/clothes/" + fixedPaths[key].url;

```

```javascript
		entry.changeFrequency = fixedPaths[key].changeFrequency;

```

```javascript
		entry.priority = fixedPaths[key].priority;

```

```javascript
		siteMapEntries.push(entry);

```

```javascript
	}

```

```javascript
	return siteMapEntries;

```

```javascript
}

```

But when I visit [mywebsite.com&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;mywebsite Resources and Information.](http://mywebsite.com/clothes/sitemap.xml), instead of getting the two entries I created using the code above, I only get the standard sitemap.xml file that I would get from [mywebsite.com&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;mywebsite Resources and Information.](http://mywebsite.com/sitemap.xml). In other words, instead of getting a sitemap.xml with my entries, I got the standard one with all the other entries from my site.  
Was anyone able to create sitemaps for your dynamic pages?

---

<div class="post-metadata">

**Author:** ![fabiomolinar](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/fabiomolinar/32/15968_2.png) [@fabiomolinar](https://forum.wixstudio.com/u/fabiomolinar)\
**Post date:** [April 24, 2020, 7:48pm UTC](https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593/2 "2020-04-24T19:48:53Z")

</div>

I have tried using the afterSitemap hook instead as described [here](https://www.wix.com/corvid/reference/wix-router.html#afterSitemap) in the documentation, but to no avail. Still not working.

---

<div class="post-metadata">

**Author:** ![fabiomolinar](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/fabiomolinar/32/15968_2.png) [@fabiomolinar](https://forum.wixstudio.com/u/fabiomolinar)\
**Post date:** [April 24, 2020, 8:00pm UTC](https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593/3 "2020-04-24T20:00:43Z")

</div>

I get it now. The _routers.js_ file is used only by pages that are created using the _router_ feature. It doesn’t apply to dynamic pages.

---

<div class="post-metadata">

**Author:** ![fabiomolinar](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/fabiomolinar/32/15968_2.png) [@fabiomolinar](https://forum.wixstudio.com/u/fabiomolinar)\
**Post date:** [April 25, 2020, 5:15pm UTC](https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593/4 "2020-04-25T17:15:24Z")

</div>

I almost answered my question myself. I was doing it wrong though. I was trying to create a sitemap using a router type page function for a dynamic page. Since I created the dynamic page first, the router function I was trying to create wasn’t working as I wanted. The solution was simple and the code is almost identical to what I wrote in my question.

The only catch is that instead of creating a router function with the same prefix as the one used by my dynamic pages, I had to create a dummy router page (which I called _sitemap_ ) and then I used its sitemap function to create my custom sitemaps.

---

<div class="post-metadata">

**Author:** ![fabiomolinar](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/fabiomolinar/32/15968_2.png) [@fabiomolinar](https://forum.wixstudio.com/u/fabiomolinar)\
**Post date:** [April 26, 2020, 7:29pm UTC](https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593/5 "2020-04-26T19:29:44Z")

</div>

To make it even more clear, I have placed below a summarized version of the code I am using to create my own sitemap for my dynamic pages using route pages.

In my case, I had three types of dynamic pages:

- Two that are in fact static (they only have the connection to the database) and have the following prefixes: /kolekcja/damska and /kolekcja/meska

- One that is fully dynamic and take the product slug in its url: /kolekcja/{slug}

The hard part here was to create the sitemap for the dynamic pages related to the products, since there is a limit to the amount of products the ‘wix-data’ queries can return (100 products). Hence, for the sitemap function, instead of returning the sitemap list, we need to return a function that will chain multiple promises together to retrieve all products from the database.

Now, although my dynamic pages prefixes is “kolekcja”, I had to create a set of route pages with a different prefix; for that I used the “sitemap” prefix. I am not using any of the actual route pages, hence the page that gets created is a dummy page that I am not using and any request to that prefix is redirected to the main page (you can see that below on the route function).

```javascript
import { redirect, WixRouterSitemapEntry } from "wix-router";

```

```javascript
import wixData from 'wix-data';

```

```javascript

```

```javascript
export function sitemap_Router(request) {

```

```javascript
	// Used to redirect any request to this prefix to the main page.

```

```javascript
	return redirect("/");

```

```javascript
}

```

```javascript

```

```javascript
export function sitemap_SiteMap(sitemapRequest) {

```

```javascript
	let siteMapEntries = [];

```

```javascript
	// Fixed paths

```

```javascript
	let fixedPaths = {

```

```javascript
		"Damska": {

```

```javascript
			pageName: "Kolekcja Damska",

```

```javascript
			title: "Kolekcja Damska",

```

```javascript
			url: "damska",

```

```javascript
			changeFrequency: "monthly",

```

```javascript
			priority: 0.5

```

```javascript
		},

```

```javascript
		"Męska": {

```

```javascript
			pageName: "Kolekcja Męska",

```

```javascript
			title: "Kolekcja Męska",

```

```javascript
			url: "meska",

```

```javascript
			changeFrequency: "monthly",

```

```javascript
			priority: 0.5

```

```javascript
		}

```

```javascript
	}

```

```javascript
	for (let key in fixedPaths) {

```

```javascript
		let entry = new WixRouterSitemapEntry(key);

```

```javascript
		entry.pageName = fixedPaths[key].pageName;

```

```javascript
		entry.title = fixedPaths[key].title;

```

```javascript
		entry.url = "/kolekcja/" + fixedPaths[key].url;

```

```javascript
		entry.changeFrequency = fixedPaths[key].changeFrequency;

```

```javascript
		entry.priority = fixedPaths[key].priority;

```

```javascript
		siteMapEntries.push(entry);

```

```javascript
	}

```

```javascript
	// Dynamic paths

```

```javascript
	let query = wixData.query("Stores/Products").limit(100).include("collections");

```

```javascript
	let queryOptions = {

```

```javascript
		"suppressAuth": true,

```

```javascript
		"suppressHooks": true

```

```javascript
	}

```

```javascript
	let results = query.find(queryOptions);

```

```javascript
	return results.then((products) => {

```

```javascript
		return getData(products, siteMapEntries);

```

```javascript
	}).catch((error) => {

```

```javascript
		console.log(error);

```

```javascript
	});

```

```javascript
}

```

```javascript

```

```javascript
function getData(resultsPromise, entries = []) {

```

```javascript
	// Function to asynchronously and recursevely retrieve the

```

```javascript
	// products from the database.

```

```javascript
	let converted = itemsToSitemap(resultsPromise);

```

```javascript
	if (converted.length !== 0){

```

```javascript
		entries = entries.concat(converted);

```

```javascript
	}

```

```javascript
	let hasNext = resultsPromise.hasNext();

```

```javascript
	if (!hasNext){

```

```javascript
		return entries;

```

```javascript
	} else {

```

```javascript
		let nextPromise = resultsPromise.next();

```

```javascript
		return nextPromise.then((products) => {

```

```javascript
			return getData(products, entries);

```

```javascript
		}).catch((error) => {

```

```javascript
			console.log(error);

```

```javascript
		});

```

```javascript
	}

```

```javascript
}

```

```javascript

```

```javascript
function itemsToSitemap(results) {

```

```javascript
	// Converts a product item to a sitemap entry

```

```javascript
	return filtered.map((item) => {

```

```javascript
		let entry = new WixRouterSitemapEntry(item.slug);

```

```javascript
		entry.pageName = "Okulary " + item.name;

```

```javascript
		entry.title = "Okulary " + item.name;

```

```javascript
		entry.url = "/kolekcja/" + item.slug;

```

```javascript
		entry.changeFrequency = "monthly";

```

```javascript
		entry.priority = 0.5;

```

```javascript
		return entry;

```

```javascript
	});

```

```javascript
}

```

Hope it helps others.

---

<div class="post-metadata">

**Author:** ![pat34285](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@pat34285](https://forum.wixstudio.com/u/pat34285)\
**Post date:** [June 30, 2022, 6:09pm UTC](https://forum.wixstudio.com/t/how-to-create-sitemaps-for-dynamic-pages/24593/6 "2022-06-30T18:09:43Z")

</div>

Were you ever able to get it to work?
