-
Notifications
You must be signed in to change notification settings - Fork 26.1k
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
generateSitemaps is broken in production #61969
Comments
For me this happens as well. It is fine until 9, but anything >10 is returning 1. Also, only digits work correctly. Therefore, anything except [1-9] as single character will not return as expected on production. It's while the dev platform works properly. |
@meh7an if you are looking for a solution in the meantime (not statically generated). You can create an import { headers } from 'next/headers';
import { buildAbsoluteProductURL } from '../../../../common/utils/urls';
import { fetchProducts } from '../../utils/fetchProducts';
import { generateSitemapItem } from '../../utils/generateSitemap';
function getPageNumberFromURL(): string {
const xUrl = headers().get('x-url');
if (!xUrl) {
throw new Error('x-url header is missing');
}
const currentPath = new URL(xUrl).pathname;
const currentPage = currentPath.split('/').pop()?.split('.')[0];
if (!currentPage) {
throw new Error('Page number is missing or invalid');
}
return currentPage;
}
export async function GET() {
try {
const pageNumber = getPageNumberFromURL();
const { data } = await fetchProducts(pageNumber);
if (!data.items.length) {
throw new Error('No items found for the given page number');
}
const sitemapXML = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${data.items
.map((item) => {
const priority =
Math.round((1 - (item.rank || 0) / data.totalCount) * 100) / 100;
return generateSitemapItem(
buildAbsoluteProductURL(item.slug),
'daily',
priority,
);
})
.join('')}
</urlset>`;
return new Response(sitemapXML, {
headers: { 'Content-Type': 'text/xml' },
});
} catch (error) {
console.error(error);
return new Response('Internal Server Error', { status: 500 });
}
} The sitemap is then accessible via We also use a middleware passing |
Thank you for the workaround. This is also helpful for using sitemapindex instead of urlset, since I find no way of auto-generating sitemapindex as well. |
Yep, also using this for the index like import { URLS } from '../../../common/configs/urls';
import { fetchProducts } from '../utils/fetchProducts';
import { fetchUsers } from '../utils/fetchUsers';
import { generateSitemapLink } from '../utils/generateSitemap';
const generateSitemapLink = (url: string) =>
`<sitemap><loc>${url}</loc></sitemap>`;
export async function GET() {
const { data: productsData } = await fetchProducts('1');
const { data: usersData } = await fetchUsers('1');
const sitemapIndexXML = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${generateSitemapLink(`${URLS.base}/sitemaps/sitemap.xml`)}
${Array.from({ length: productsData.pageCount }, (_, i) => i + 1)
.map((id) =>
generateSitemapLink(`${URLS.base}/sitemaps/games/${id}.xml`),
)
.join('')}
${Array.from({ length: usersData.pageCount }, (_, i) => i + 1)
.map((id) =>
generateSitemapLink(`${URLS.base}/sitemaps/users/${id}.xml`),
)
.join('')}
</sitemapindex>`;
return new Response(sitemapIndexXML, {
headers: { 'Content-Type': 'text/xml' },
});
} |
So here's how I managed to implement my sitemap system:
I increased products per page, so the count doesn't increase more than 9 to cause the bug for now, until a proper fix. I assume implementing sitemapindex, and id naming with no limitations as a fix for this issue. |
### What? generateSitemaps function returns a 404 for /sitemap/[id].xml in production ### Why? While finding the correct sitemap partition from the array, we check the param against the id. Which works in dev because id and param are both without trailing .xml. But it fails in production as param has a trailing .xml (/sitemap/[id] works in production because it falls back to dynamic loading and param and id are both without .xml) ### How? If we are in production environment, check the id with a trailing .xml because that's whats returned from generateStaticParams, an array of __metadata_id__ with trailing .xml Fixes #61969 --------- Co-authored-by: Jiachi Liu <inbox@huozhi.im>
This change is not included in 14.1.1 yet 🙏 please use canary to get benefits of the fix |
Can confirm this issue is fixed in |
generateSitemaps function returns a 404 for /sitemap/[id].xml in production While finding the correct sitemap partition from the array, we check the param against the id. Which works in dev because id and param are both without trailing .xml. But it fails in production as param has a trailing .xml (/sitemap/[id] works in production because it falls back to dynamic loading and param and id are both without .xml) If we are in production environment, check the id with a trailing .xml because that's whats returned from generateStaticParams, an array of __metadata_id__ with trailing .xml Fixes #61969 --------- Co-authored-by: Jiachi Liu <inbox@huozhi.im>
This fix is landed in 14.1.2 |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Link to the code that reproduces this issue
Reproduction ↗
create next app with an
/app/sitemap.ts
fileTo Reproduce
npm run build
, thennpm run start
Current vs. Expected behavior
When running
npm run build
on Canary:Notice:
console.log(id)
(in reproduction sitemap.ts ↗) missing in these logs.Then running
npm run start
:/sitemap/1.xml
returnsnot-found
When running
npm run dev
:/sitemap.xml/1
worksWhen running
npm run build
on next 14.1.0:The⚠️
console.log(id)
(in reproduction sitemap.ts ↗) works. Notice how it uses duplicate id values although generating different sitemaps!!When running
npm run start
, this creates working pages on:/sitemap/1.xml
, but because of the duplicate ids:/sitemap/5.xml
(for example) has the same contents as/sitemap/1.xml
I was initially generating the ids in
generateSitemaps()
from the API response, but noticed that even returning hard-coded ids (as in my reproduction) causes this issue.I also noticed I was able to access any
/sitemap/n.xml
and it having some data.Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112 Binaries: Node: 20.5.0 npm: 10.4.0 Yarn: 1.22.19 pnpm: 8.14.0 Relevant Packages: next: 14.1.1-canary.50 // Latest available version is detected (14.1.1-canary.50). eslint-config-next: 14.1.0 react: 18.2.0 react-dom: 18.2.0 typescript: 5.3.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router
Which stage(s) are affected? (Select all that apply)
next build (local)
Additional context
This issue seems to be related:
#60894
+PR: #61088
NEXT-2501
The text was updated successfully, but these errors were encountered: